How to Pipe FFmpeg Output to Another FFmpeg Instance
Piping the output of one FFmpeg process directly into another allows you to chain complex transcoding tasks together without writing temporary files to your hard drive, saving both time and disk space. This guide provides a straightforward explanation of how to configure the sending and receiving FFmpeg instances using standard command-line pipes, along with the critical formatting requirements needed to make this process work seamlessly.
To pipe FFmpeg output to another FFmpeg instance, you must configure
the first instance to write to standard output (stdout) and
the second instance to read from standard input (stdin).
This is achieved using the pipe operator (|) of your
operating system’s command line.
Here is the basic command template:
ffmpeg -i input.mp4 [encoding_options] -f mpegts - | ffmpeg -i - [encoding_options] output.mp4Key Requirements for Piping FFmpeg
For this pipeline to work, you must adhere to two critical rules regarding container formats and stream handling.
1. Use a Streamable Container Format
Standard video containers like MP4 or MKV require a “seekable” output because they write metadata headers at the end of the file processing. Since standard pipes are non-seekable streams, you must use a container format designed for streaming for the intermediate transfer.
Excellent choices for the intermediate format include: *
MPEG-TS (-f mpegts): Ideal for standard
compressed video and audio streams. * NUT
(-f nut): An FFmpeg-specific container that
supports almost any codec combination. * Raw Video/Audio
(-f rawvideo or -f f32le): Best for
passing uncompressed frames, though this requires high CPU and memory
bandwidth.
2. Configure the Standard Input and Output
- The Sender: Replace the output file path in the
first command with a single hyphen (
-). You must also explicitly define the format using the-fflag (e.g.,-f mpegts -). - The Receiver: Replace the input file path in the
second command with
pipe:0or a single hyphen (-i -). This instructs FFmpeg to read the incoming stream from standard input.
Practical Example
In this example, the first FFmpeg instance decodes an input file, resizes it to 720p, encodes it to H.264, and pipes it using the MPEG-TS format. The second instance receives the stream, transcodes the audio to AAC, and saves the final output as an MP4 file:
ffmpeg -i input.mkv -vf scale=1280:720 -c:v libx264 -f mpegts - | ffmpeg -i - -c:a aac output.mp4By utilizing this pipeline, the decoding and encoding occur simultaneously in memory, maximizing CPU usage and eliminating the need for intermediate storage.