How to make ffmpeg write its output to a named pip

2019-04-16 17:57发布

问题:

I know i can make ffmpeg put its output to stdout and stderr using pipe:1 and pipe:2, respectively, as output_file parameter. (Docs)

But what about named pipes, can i make it write to one?

If not, is there a way to redirect the data in stdout to a named pipe in Linux? (something like ffmpeg <parameters> | pipe123)

This question is a follow-up of this question.

回答1:

You could create a named pipe first and have ffmpeg write to it using the following approach:

ffmpeg output to named pipe:

# mkfifo outpipe

# ffmpeg -i input_file.avi -f avi pipe:1 > outpipe
FFmpeg version 0.6.5, Copyright (c) 2000-2010 the FFmpeg developers
  built on Jan 29 2012 17:52:15 with gcc 4.4.5 20110214 (Red Hat 4.4.5-6)
...
[avi @ 0x1959670]non-interleaved AVI
Input #0, avi, from 'input_file.avi':
  Duration: 00:00:34.00, start: 0.000000, bitrate: 1433 kb/s
    Stream #0.0: Video: cinepak, yuv420p, 320x240, 15 tbr, 15 tbn, 15 tbc
    Stream #0.1: Audio: pcm_u8, 22050 Hz, 1 channels, u8, 176 kb/s
Output #0, avi, to 'pipe:1':
  Metadata:
    ISFT            : Lavf52.64.2
    Stream #0.0: Video: mpeg4, yuv420p, 320x240, q=2-31, 200 kb/s, 15 tbn, 15 tbc
    Stream #0.1: Audio: mp2, 22050 Hz, 1 channels, s16, 64 kb/s
Stream mapping:
  Stream #0.0 -> #0.0
  Stream #0.1 -> #0.1
Press [q] to stop encoding
frame=  510 fps=  0 q=11.5 Lsize=    1292kB time=33.96 bitrate= 311.7kbits/s
video:1016kB audio:265kB global headers:0kB muxing overhead 0.835379%

reading outpipe named pipe (Python example):

# python -c "import os; fifo_read = open('outpipe', 'r', 0); print fifo_read.read().splitlines()[0]"
RIFFAVI LIST<hdrlavih8j...
...

-- ab1



回答2:

@ab77 answer is spot on except for one major thing that I encountered myself. I do not know if this happens on Linux or other *nix'es but on Mac OS X if you do not put the appropriate extension on the pipe name then it will not pipe. So if the output format is mpeg then the pipe shold be name outpipe.mpg for instance.