7

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?

1
  • 1
    A tell: stderr=subprocess.PIPE, then ignoring it altogether is a recipe for disaster. Commented Dec 4, 2016 at 23:19

1 Answer 1

9

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 a 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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it works! This will from now on be my standard way of dealing with Python pipes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.