With the following code, I am able to pipe frames of a video to FFMPEG using Python, Numpy and the FFMPEG binaries:
from __future__ import print_function
import subprocess
import numpy as np
import sys
npshape = [480, 480]
cmd_out = ['ffmpeg',
'-y', # (optional) overwrite output file if it exists
'-f', 'rawvideo',
'-vcodec','rawvideo',
'-s', '%dx%d'%(npshape[1], npshape[0]), # size of one frame
'-pix_fmt', 'rgb24',
'-r', '24', # frames per second
'-i', '-', # The input comes from a pipe
'-an', # Tells FFMPEG not to expect any audio
'-vcodec', 'mpeg4',
'output.mp4']
fout = subprocess.Popen(cmd_out, stdin=subprocess.PIPE, stderr=subprocess.PIPE).stdin
for i in range(24*40):
if i%(24)==0:
print('%d'%(i/24), end=' ')
sys.stdout.flush()
fout.write((np.random.random(npshape[0]*npshape[1]*3)*128).astype('uint8').tostring())
fout.close()
This works fine if I write anything less than 37 seconds worth of frames, but if I try to write anything more, the code just hangs. What is the underlying cause for this behaviour? How can I fix it?
A highly probable culprit is the disgustingly stinky subprocess.Popen
line. Not only you ignore its return value - which you must never do, in order to ensure the subprocess' completion by certain point and/or check its exit code - you also make stderr
a pipe but never read it - so the process must be hanging when its buffer fills.
This should fix it:
p = subprocess.Popen(cmd_out, stdin=subprocess.PIPE)
fout = p.stdin
<...>
fout.close()
p.wait()
if p.returncode !=0: raise subprocess.CalledProcessError(p.returncode,cmd_out)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With