Force a specific output container
FFmpeg usually picks the output container from the output filename, but it can be told to use a specific muxer with the `-f` flag. This is the right approach when the output is being piped to another process, or when the container name is not in the filename (e.g., a stream to a network service).
The command
ffmpeg -i in.mp4 -f matroska -c:v libx264 -c:a aac out.mkvWhat it does
The command forces the matroska muxer regardless of the filename. The encoder is libx264 for video and AAC for audio, both of which matroska supports. The output is a normal MKV file. The flag is most useful for network outputs: `ffmpeg -i in.mp4 -f flv rtmp://server.tld/app/stream` forces the FLV muxer for an RTMP stream.
Requirements
A muxer built into FFmpeg and codecs compatible with the muxer. The flag is a no-op if the muxer does not support the codec (e.g., passing `-f mp4` with a ProRes codec will fail). The container is written as specified, regardless of the filename extension.
Things to avoid
- Using `-f` to force a muxer that does not support the codec. The command will fail with 'Format ... not supported' or, worse, silently produce a malformed file.
- Confusing the muxer with the codec. `-f` sets the muxer (the container); `-c:v` sets the video codec. They are independent, but the codec must be compatible with the muxer.
- Forgetting the `-f` flag on a piped output. Without a filename and without `-f`, FFmpeg will use the default muxer, which may not be what you want.
- Relying on the filename to set the muxer. Some muxers (MPEG-TS, raw streams) do not have a file extension; use `-f` to be explicit.