Is it safe to omit the codec when converting with ffmpeg?
Sebastian Wright
I noticed today that I could encode or transcode video and audio streams using ffmpeg without specifying a codec, due to the fact that it seems to imply the video or audio codec being encoded or transcoded to from the extension of the output. This means that:
ffmpeg "input.mp3" -c:a aac "output.m4a"...can be replaced by:
ffmpeg "input.mp3" "output.m4a"In addition:
ffmpeg "input.dts" -c:a copy -ac 2 "output.dts"...can be replaced by:
ffmpeg "input.dts" -ac 2 "output.dts"Given that always explicitly including the codec is the most popular way to use ffmpeg I've seen - on this site in particular - I wondered whether there were reasons for this to be the case.
Are there any possible pitfalls to consider when using ffmpeg to encode in this way? Is it safe to assume that it would result in exactly the same conversion that explicitly specifying a codec might?
2 Answers
It's best to specify the encoder
Some of the default selections are legacy or weird. For example:
flv1is chosen for FLV output. Most people expect H.264 (libx264).flacis chosen for OGG/OGA output. Most people expect Vorbis (libvorbis).
The encoder used as the default for an output format can depend on which configure options were used to compile your
ffmpeg. So if yourffmpegdoes not have--enable-libx264, then for MP4 the encoder mpeg4 (MPEG-4 Part 2 video) is used instead of libx264 (H.264 video). Most people useffmpegbuilds with support for the most popular libraries included, but it happens on occasion.It's not much work to be specific.
But sometimes you can be lazy
If you know what encoders are going to be chosen, or if you check and verify, then you can save a few keystrokes if you want. Refer to Stream mapping in the console output to see what encoders ffmpeg uses. Example:
ffmpeg -i input.foo output.mp4
...
Stream mapping: Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264)) Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native))In this case it is using libx264 for video (-c:v libx264), and the native AAC encoder for audio (-c:a aac).
You can view details on a specific muxer to see what codecs will be used by default:
ffmpeg -h muxer=mp4
...
Muxer mp4 [MP4 (MPEG-4 Part 14)]: Default video codec: h264. Default audio codec: aac. 4 Is it safe to assume that it would result in exactly the same conversion that explicitly specifying a codec might
Not necessarily. It is "safe" in that you will get a file that works. Ffmpeg will pick a preferred codec that works with that container. However using a different file extension MAY choose a different codec. Also the preferred codec could change in the future, or based on how Ffmpeg was compiled.