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?