8000 Added an option to return immediately from run() so you could process… · yan-code1/ffmpeg-python@ce33461 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce33461

Browse files
committed
Added an option to return immediately from run() so you could process the output of ffmpeg while it's processing
1 parent b6f150c commit ce33461

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ import ffmpeg
3232
)
3333
```
3434

35+
You could also use pipes to direct the output of ffmpeg to a socket, a file or for processing in python
36+
```python
37+
import ffmpeg
38+
out, err, subproc = (
39+
ffmpeg
40+
.input('rtsp://%s:8554/default')
41+
.output('-', format='h264')
42+
# wait=False means run() returns immediately and does not wait for ffmpeg process to end
43+
.run(capture_stdout=True, wait=False)
44+
)
45+
46+
packet_size = 4096
47+
while subproc.poll() is None:
48+
out_packet = out.read(packet_size)
49+
try:
50+
tcp_socket.send(out_packet)
51+
except socket.error as e:
52+
return
53+
```
54+
55+
3556
## Complex filter graphs
3657
FFmpeg is extremely powerful, but its command-line interface gets really complicated really quickly - especially when working with signal graphs and doing anything more than trivial.
3758

ffmpeg/_run.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def compile(stream_spec, cmd='ffmpeg', overwrite_output=False):
186186
@output_operator()
187187
def run(
188188
stream_spec, cmd='ffmpeg', capture_stdout=False, capture_stderr=False, input=None,
189-
quiet=False, overwrite_output=False):
189+
quiet=False, overwrite_output=False, wait=True):
190190
"""Ivoke ffmpeg for the supplied node graph.
191191
192192
Args:
@@ -196,21 +196,29 @@ def run(
196196
quiet: shorthand for setting ``capture_stdout`` and ``capture_stderr``.
197197
input: text to be sent to stdin (to be used with ``pipe:``
198198
ffmpeg inputs)
199+
wait: wait until the ffmpeg process is done before returning. If you're using pipes, you might
200+
want this False
199201
**kwargs: keyword-arguments passed to ``get_args()`` (e.g.
200202
``overwrite_output=True``).
201203
202-
Returns: (out, err) tuple containing captured stdout and stderr data.
204+
Returns: If wait is True (default), this function returns (out, err) tuple containing captured stdout and stderr data.
205+
If wait is False, this function returns (out, err, popen) tuple containing the subprocess handle as well
203206
"""
204207
args = compile(stream_spec, cmd, overwrite_output=overwrite_output)
205208
stdin_stream = subprocess.PIPE if input else None
206209
stdout_stream = subprocess.PIPE if capture_stdout or quiet else None
207210
stderr_stream = subprocess.PIPE if capture_stderr or quiet else None
208211
p = subprocess.Popen(args, stdin=stdin_stream, stdout=stdout_stream, stderr=stderr_stream)
209-
out, err = p.communicate(input)
210-
retcode = p.poll()
211-
if retcode:
212-
raise Error('ffmpeg', out, err)
213-
return out, err
212+
if wait:
213+
out, err = p.communicate(input)
214+
retcode = p.poll()
215+
if retcode:
216+
raise Error('ffmpeg', out, err)
217+
return out, err
218+
else:
219+
out = p.stdout
220+
err = p.stderr
221+
return out, err, p
214222

215223

216224
__all__ = [

0 commit comments

Comments
 (0)
0