Question

I am writing a script that takes videofiles such as MKV or AVI and changes them to MP4, and then prepares them from web streaming. I want my output to use H.264 and AAC codecs.

So far my command looks like this:

ffmpeg -i input.mkv -vcodec h264 -acodec aac -ab 128k -ac 2 -strict -2 output.mp4

However, when the codec already is h.264 it still muxes it, it says: (h264 -> libx264).

If I replace '-vcodec h264' with '-vcodec copy' it goes a lot faster when the codec is H.264 but of course won't change the codec if there is a different codec in the input file.

Is there a way for FFmpeg to recognize that the codec is almost the same, and thus not muxing the videostream, but still changing video codec if the source isn't H.264?

Was it helpful?

Solution

No. FFmpeg can only copy bitstreams or re-encode. It can't guess if you want to keep a certain codec. You'll have to parse the file info and then decide whether you want to copy or not.

Some examples of how to do that are listed here: MKV to MP4 transcoding script issues

Basically, you could do this (shameless plug from @evilsoup there):

ffprobe input.mkv 2>&1 | sed -n '/Video:/s/.*: \([a-zA-Z0-9]*\).*/\1/p' | sed 1q'

This would output h264 for an H.264 video stream.

Minor tip: Try getting used to specifying the exact encoder you want. h264 isn't really an encoder for FFmpeg – it defaults to libx264. So rather use -c:v libx264.
One more thing: aac is the built-in AAC encoder from FFmpeg. Third party encoders like libfdk_aac or libfaac offer a VBR encoding mode and generally better quality than aac.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top