10

I’m new to FFmpeg (Windows), and need some help.

I want to convert my MKVs to MP4 with as little loss in quality as possible. The MKVs are made with MakeMKV and contain among others DTS soundtrack. They are lossless. The reason I want to convert, is that I plan to stream it to iPads and Apple TV. iPads don’t support DTS and MKVs. The MKVs are both DVDs and Blu-Ray. The MKVs contain video, soundtracks and subtitles.

I have tried this, but get no sound due to DTS soundtrack:

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

Can anybody help me?

What codecs should I use? MP4 is the best for Apple?

Vylix
  • 1,716
  • 4
  • 22
  • 26
Striky
  • 103
  • 1
  • 1
  • 7
  • 1
    Are there specific iPad and Apple TV versions you are targeting, or do you want to support all of them? – llogan May 24 '14 at 00:42
  • 2
    Note: MP4 is not a codec. It's just a file format (a "container"). – slhck May 24 '14 at 06:43
  • I have the newest iPod Air. I have the newest Apple TV. I know MP4 is a container and not a codec :) – Striky May 24 '14 at 08:14
  • Please include some information about one of your typical inputs. The complete console output of `ffmpeg -i input.mkv` will suffice. – llogan May 24 '14 at 23:11
  • I have tried ffmpeg -i input.mkv -c:v copy -c:a copy output.mp4 This did play but got no sound since apple does not play DTS. Have also tried ffmpeg -i input.mkv -strict experimental -c:v copy -c:a aac -b:a 192k -q:a 6 output.mp4 but the film (both video and sound) was not playing smooth on my iPad Air (both stream and downloaded). – Striky May 25 '14 at 07:44
  • I'll be able to provide a more detailed answer if you show some information about your input and your ffmpeg build. – llogan May 25 '14 at 17:08

2 Answers2

22

MP4 is indeed the best format for Apple devices and software. DTS is also indeed not supported, many MP4 video files contain two audio tracks, one DTS and one AAC.

There are multiple encoders available, all of them documented on the ffmpeg wiki. Which codec is available depends on how ffmpeg was compiled. libfdk_aac will give you the best results, but due to this codec being non-free it's not always available.

Things you can try (I put them in the order of my perceived quality, best first)

ffmpeg -i input.mkv -c:v copy -c:a libfdk_aac -b:a 128k output.mp4
ffmpeg -i input.mkv -strict experimental -c:v copy -c:a aac -b:a 192k output.mp4
ffmpeg -i input.mkv -c:v copy -c:a libfaac -b:a 192k output.mp4

If you want to retain the DTS track too, use the -map flag.

Not directly of use for OP, but the OS X program subler makes this process very easy.

EDIT: Comments tl;dr? OP solved problem with the following command

ffmpeg -i input.mkv -strict experimental -map 0:0 -map 0:1 -map 0:2 -map 0:3 -c:v copy -c:a aac -b:a 384 -c:s copy output.mp4

TIP: if -c:s copy for subtitles doesn't work, try -c:s mov_text.
Saved me on multiple occasions.

jornane
  • 1,085
  • 6
  • 14
  • I don't think the internal AAC encoder's VBR mode works as intended. Last time I checked it was broken. Better go with CBR, and use VBR for the other encoders (`libfdk` and `libfaac`) – slhck May 24 '14 at 06:44
  • I have also read that libfdk_aac will give the best result. I have downloaded the 32-bit static version for Windows (git-9722a6a (2014-05-24)). How can I check if I have the libfdk codec? Edit: I get unknown encoder 'libfdk_aac'. WHere can I download it, or is there a almost as good audio codec? – Striky May 24 '14 at 08:58
  • Have tried both ffmpeg -i input.mkv -c:v copy -c:a libfdk_aac -b:a 128k output.mp4 and ffmpeg -i input.mkv -c:v copy -c:a libfaac -b:a 192k output.mp4 No go :( – Striky May 24 '14 at 09:11
  • @Striky "No go :(" I tried the last command with a fresh ffmpeg from Homebrew, works for me. Can you give more details on why it doesn't work for you? – jornane May 24 '14 at 10:11
  • @slhck: I just tried the `-c:a aac -q:a 6` settings and compared the result with `-c:a libfaac -b:a 192k`, in my opinion the **aac** codec gives better results than **libfaac**, but since the codecs are under development, this may change. **libfdk_aac** will probably give even better results, but I don't have it available right now. – jornane May 24 '14 at 10:22
  • Comparing VBR and CBR with different codecs doesn't give useful results, but the quality range of the `aac` encoder isn't even documented, so it's safer to use it with CBR. – slhck May 24 '14 at 12:42
  • You're right, that was a bit naive of me. I re-encoded using `-c:a aac -b:a 192k`, but that did not change my opinion; I still think the experimental **aac** codec gives better results than **libfaac**. I did change my answer to use a CBR for aac, however do note that libfaac does not support CBR, but instead has an ABR when you define the bitrate. – jornane May 24 '14 at 12:59
  • @ffmpeg -i input.mkv -strict experimental -c:v copy -c:a aac -b:a 192k -q:a 6 output.mp4 did work, but somehow did not play well on my iPad Air. Don't know why, if there are codecs not supported by apple? I also need all the soundtracks and subtitles if thats possible. – Striky May 24 '14 at 21:10
  • "did not play well", what is the problem? Apple does have some restrictions, but the aac codec should be fine. All soundtracks is no problem, just look at the `-map` argument. There is a link in the answer. – jornane May 24 '14 at 23:08
  • The film was "stuttering" (is that the right word?), both sound and picture. It was not smooth. I downloaded it to the iPad, and also streamed it. When I first copied the v codec and a codec (just changed container to mp4), I got smooth video, but no sound (since apple does not play DTS). BTW, clicked on the -map link now. Thanks for that :) And what does -q:a 6 stand for? – Striky May 25 '14 at 07:41
  • Found a command that works for me. Will keep working on it. ffmpeg -i input.mkv -strict experimental -map 0:0 -map 0:1 -map 0:2 -map 0:3 -c:v copy -c:a aac -b:a 384 -c:s copy output.mp4. (have video, 2 soundtracks and 1 subtitle). Thanks for your help. – Striky May 25 '14 at 18:10
  • Good to hear that it works! I did some tests with different commands, both your command and my commands work for me (tried on iPad 3), but maybe your input file is different in some way. `-q:a 6` means audio quality 6, the scale is codec-dependent (and undefined for `aac`). – jornane May 25 '14 at 21:37
  • @Striky Are you sure you didn't mean `-b:a 384k`? – Jason R. Coombs Nov 30 '14 at 16:18
0

I know this is an old question — and already has an answer – but I wanted to share some commands I am using with FFmpeg on macOS.

An x264 video encoded MP4 with AAC audio from an MKV

Basic conversion of an MKV with an MP4 in it but converting the audio to AAC at 128k. Note the mappings: -map 0:0 means the first (0) track from the source input (0) and the -map 0:1 means the second (1) track from the same source input (0). I like doing this in cases where there are multiple audio tracks so I could do something like select -map 0:3 to get the third track.

ffmpeg -i input.mkv \
       -c:v copy \
       -c:a aac -b:a 128k \
       -map 0:0 -map 0:1 \
       output.mp4 \
       ;

An HEVC/x265 video encoded MP4 with AAC audio from an MKV

This is similar to the above x264 command, but instead uses the HEVC/x265 to get a smaller file size with the same — if not better quality — video from the source MKV. Note that I prefer to scale iOS videos to 720 when using HEVC/x265; the quality for playback without too much noise is solid on my MacBook, iPad and iPhone. Just be sure your hardware and OS supports HEVC/x265 playback. And be forewarned: HEVC/x265 is very CPU intensive so if your machine doesn’t have a lot of CPU power it can take as long as a day or so to encode 90 minutes of video.

ffmpeg -i input.mkv \
       -vf scale=-1:720 \
       -c:v libx265 -crf 20 \
       -c:a aac -b:a 128k \
       -threads 4 \
       -tag:v hvc1 \
       -map 0:0 -map 0:1 \
       output.mp4 \
       ;

Here is a breakdown of each of the core parameters other than input and output files.

  • -vf scale=-1:720: Scale the video to 720 height and automatically calculate the width. Note that FFmpeg might choke some MKVs for reasons I am not 100% clear on so you would have to explicitly set the width with something like this: -vf scale=1280:720. Experiment and see what works for you.
  • -c:v libx265 -crf 20: The libx265 is the video encoding codec. The -crf 20 is the CRF (constant rate factor). The default CRF if you don’t specify it is 28, but — as of now — find the quality to be pretty much… Meh. I prefer 20 since it is a very good balance between quality and file size.
  • -threads 4: I like to specify thread counts. 4 is what my system can handle now, but sometimes I ratchet it down to 2 or 3. Higher number is faster if you have enough CPUs to handle it.
  • -tag:v hvc1: The magic metadata setting to put in place for iOS devices to properly play back HEVC/x265 videos.

And that’s about that!

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212