Skip to content
Advertisement

why does my ffmpeg process wait forever without stdin parameter to subprocess.run?

I am using the python subprocess module to call ffmpeg like this:

def ffmpeg_wait(self, fname, args):
    ffmpeg = subprocess.run([
        'ffmpeg', '-v', 'quiet', '-i', fname, *args],
        stdin=subprocess.DEVNULL,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE,
        universal_newlines=True)
    print('terminated')
    (out,err) = (ffmpeg.stdout, ffmpeg.stderr)
    return out + err

This works fine. An example call:

self.ffmpeg_wait(fname,
    ['-r', str(sps), '-s', '320x240', '-f', 'image2', 'tmp/%06d.png'])

What I want to know is why it doesn’t work when I omit the stdin parameter. In this case the ffmpeg process waits forever iff I start my program as a background process in the shell. Running in the foreground works.

I guess this has something to do with how the shell handles stdio channels and how they are inherited by subprocesses, but I cannot figure it out. I tried (unsuccessfully) to re-establish this behaviour in the shell (using redirection).

Any explanation or documentation pointers?

PS: ffprobe does not do this. It always works even without stdin parameter. So it’s also something in ffmpeg which triggers this behaviour.

Advertisement

Answer

If you don’t tell it somehow to ignore stdin, it expects the image itself (not just a file name) to come from stdin, as if you sat there and typed in the image byte by byte. This is essential to “piping” commands in *nix systems, but it means that many commands will appear to hang if you don’t explicitly direct them to some input stream other than stdin.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement