Is it safe to omit the codec when converting with ffmpeg?

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?

Hashim

Posted 2019-02-25T21:48:11.707

Reputation: 6 967

1You don't need the -ac 2 in 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 .dts file 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.637

1Yes 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 ffprobe to get the codec_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 ffmpeg warned novices to it like myself that any encoding options included are ignored when -c:a copy is 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

Answers

3

It's best to specify the encoder

  • Some of the default selections are legacy or weird. For example:

    • flv1 is chosen for FLV output. Most people expect H.264 (libx264).

    • flac is 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 your ffmpeg does 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 use ffmpeg builds 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.

llogan

Posted 2019-02-25T21:48:11.707

Reputation: 31 929

Great answer, this is the kind of intimate knowledge of FFmpeg I was relying on to get an answer to this question. I noticed that your website says you're an FFmpeg developer. Is there any particular reason for the first point - FFmpeg using legacy/weird encoders - and is there a possibility that they'll change/become more up to date anytime soon? – Hashim – 2019-02-25T23:20:18.750

Since posting the question, I also noticed that the FFmpeg website uses the simpler syntax on its homepage, so is clearly trying to encourage such use for beginners, and I have to admit the codec syntax and how complicated it looks had, prior to this week, been a barrier to learning how to use ffmpeg for a while.

– Hashim – 2019-02-25T23:23:52.630

@Hashim Regarding the legacy encoders, sometimes things are updated (webm now uses libbpx-vp9 & libopus by default for example), but sometimes things languish. I'd be fine with updating some defaults to more modern, sane, or useful values, but I imagine some of the more argumentative developers would balk at some changes, and may suggest it will confuse users (not that the legacy stuff isn't confusing), so some settings stay the same indefinitely. – llogan – 2019-02-26T00:23:33.670

That's a real shame. When you use a program like ffmpeg with the pedigree that it has, you'd expect its defaults to be both current and optimised for the majority of people, I hope something changes soon on that front. Thanks regardless for all your help. – Hashim – 2019-02-26T00:27:25.097

1

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.

szatmary

Posted 2019-02-25T21:48:11.707

Reputation: 2 181