16

EDIT: This question has become very popular and is one of the top results for searching "convert mkv to h264 ffmpeg" and thus I feel it is appropriate to add that for anyone stumbling upon this question to rather use

ffmpeg -i input.mkv -c:v libx264 -c:a aac output.mp4

as libvo_aacenc has been removed in recent versions of FFmpeg and it now has a native aac encoder. For more information visit the FFmpeg wiki page for encoding AAC.

Here is the original question:

I would like to convert my .mkv files to .mp4 using FFmpeg. I have tried the following code:

ffmpeg -i input.mkv -c:v libx264 -c:a libvo_aacenc output.mp4

But I get the error:

Error while opening encoder for output stream #0:1 - maybe incorrect parameters such as bit_rate, rate, width or height.

Is there any way to get around this? I have tried setting the bitrate of the audio but the problem seems to persist.

Rikus Honey
  • 534
  • 1
  • 3
  • 17
  • Related: https://superuser.com/questions/639983/avconv-cant-convert-stream-to-mp4-file-fails-with-unable-to-set-encoding-para – Blender Jun 17 '15 at 17:45
  • As the error says, you have incorrect (or missing) parameters. Refer to: https://trac.ffmpeg.org/wiki/Encode/H.264 – Maxito Jun 17 '15 at 18:22
  • 1
    `mkv` is a [video container](https://en.wikipedia.org/wiki/Comparison_of_video_container_formats) and `H.264` is an [encoding format](https://en.wikipedia.org/wiki/Comparison_of_video_codecs). You cannot convert a container to an endoding. – Endoro Jun 17 '15 at 20:23
  • @Rikus Honey Did you enable `libvo-aacenc` like this `--enable-libvo-aacenc`. Why don't you use `libfaac` or `libfdk_aac`. They are free. – budthapa Jun 18 '15 at 12:11

2 Answers2

32

I suggest you first check whether your .mkv file already has H.264/AAC streams in the first place. Because if it does, all you have to do is copy the streams and change the container:

ffmpeg -i input.mkv -c copy output.mp4

If it doesn't, you probably got rejected because your formats are not compatible with .mp4. Try the following to output H.264/AAC:

ffmpeg -i input.mkv -c:v libx264 -c:a aac output.mp4

But again, if your .mkv already contains H.264/AAC, USE THE FIRST SOLUTION. It'll be faster and will have a better quality.

llogan
  • 121,796
  • 28
  • 232
  • 243
Ely
  • 1,189
  • 9
  • 12
  • 1
    You can use `-codec copy` or `-c copy` instead of `-vcodec copy -acodec copy`. – llogan Jun 20 '15 at 16:49
  • 1
    Also, if you want to encode using x264, it's usually a good idea to set some specific encoding settings (preset and some method of rate/q control), see https://trac.ffmpeg.org/wiki/Encode/H.264 – Ronald S. Bultje Jun 21 '15 at 13:00
3

As an alternative to ffmpeg you can use HandBrakeCLI, e.g.:

HandBrakeCLI --encoder "x264" --quality 25.0 --aencoder "copy:aac" --audio-fallback "av_aac" --all-audio --all-subtitles -i "input.mkv" -o "output.mkv"

HandBrakeCLI is part of the package handbrake-cli.

For parameter explanation, see HandBrakeCLI --help or https://handbrake.fr/docs/en/latest/cli/command-line-reference.html

  • Altough the original question was how it is done with ffmpeg, this is a valid answer, because HandBrake does essentially use ffmpeg... Great solution, thanks! – luttztfz Jul 12 '20 at 17:22