Pipe Linux Command Output to FFmpeg

This article provides a quick overview and practical guide on how to stream data directly from a Linux command or process into FFmpeg using standard streams or named pipes. By piping output directly, you can process, transcode, or stream real-time media without wasting disk space or CPU cycles on intermediate temporary files.


Using Standard Input (stdin) with FFmpeg

The most straightforward way to pipe data into FFmpeg is by using the Linux pipe operator (|) and telling FFmpeg to read from standard input (stdin). In FFmpeg, stdin is represented by a single dash (-).

cat video.raw | ffmpeg -f rawvideo -pixel_format rgb24 -video_size 1920x1080 -i - output.mp4

When reading from a pipe, FFmpeg cannot “seek” backward or forward through the file. Because of this, you must explicitly define the input format using the -f flag so FFmpeg knows how to parse the incoming stream.


Piping Live Video Streams

You can pipe live video from tools like raspivid or wget/curl directly into FFmpeg for encoding or streaming to platforms like YouTube or Twitch.

curl -s http://example.com/live_stream.ts | ffmpeg -i - -c:v libx264 -c:a aac output.mkv

In this example, FFmpeg reads the containerized .ts stream directly from the curl output via the pipe.


Handling Multiple Inputs with Named Pipes (FIFOs)

Standard piping (|) only allows you to pass one data stream. If you need to pipe multiple inputs into FFmpeg (for example, merging a separate video command and audio command), you must use Named Pipes, also known as FIFOs.

You can create named pipes using the mkfifo command:

mkfifo video_pipe
mkfifo audio_pipe

Once created, you can direct your Linux commands to write to these pipes in the background, and point FFmpeg to read from them as if they were regular files:

# Start the data producers in the background
generate_video_cmd > video_pipe &
generate_audio_cmd > audio_pipe &

# Run FFmpeg using the named pipes as inputs
ffmpeg -i video_pipe -i audio_pipe -c:v copy -c:a copy output.mp4

# Clean up the pipes when finished
rm video_pipe audio_pipe

Key Considerations