2
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?
1You don't need the
-ac 2in your third command: it's being ignored since you are only re-muxing (stream copying). Your fourth command is not the same as the third: it will re-encode while the previous command just re-muxes. – llogan – 2019-02-25T22:31:54.210@llogan Great catch. I tested the conversion with a
.dtsfile I had but forgot to check whether the output was actually stereo. So to confirm - any changes made to a stream can only be made if the stream is re-encoded, and a re-encode can only happen if the stream is not copied? – Hashim – 2019-02-25T23:17:46.6371Yes to both questions – llogan – 2019-02-26T00:02:44.547
@llogan Interesting. Is there a way to use FFmpeg to use the same codec as the input for the output, while still getting it to re-encode? – Hashim – 2019-02-26T00:08:38.830
1There is no internal setting for this, but one option is to use
ffprobeto get thecodec_name, which can roughly correlate to an encoder name, then use if/then statements in your favorite scripting language. IIRC, other users have asked this same question on the Stack Exchange network, so there are probably some examples. – llogan – 2019-02-26T00:13:33.590@llogan That's probably a little more work than I'll ever go to for something like this, but I appreciate the comprehensive solution. What would really be useful is if
ffmpegwarned novices to it like myself that any encoding options included are ignored when-c:a copyis used - it's obvious now that it's spelt out to me, but not so intuitive when you don't understand the theory behind it, and it could avoid a lot of confusion for users. Would it possible for this to be put in as a feature request? – Hashim – 2019-02-26T00:44:37.353@llogan Or alternatively, just an option to actually make FFmpeg grab the codec from the input and still re-encode. – Hashim – 2019-02-26T00:46:50.450