You can use the -map
option (full documentation) to select specific input streams and map them to your output.
The most simple map
syntax you can use is -map i:s
, where i
is the input file ID and s
is the stream ID, both starting with 0
. In your case, that means we select track 0
and 4
:
ffmpeg -i vidui.mkv -c:v libx264 -c:a ac3 -crf 20 -map 0:0 -map 0:4 vidui.mp4
If you want to choose video, audio or subtitle tracks specifically, you can also use stream specifiers:
ffmpeg -i vidui.mkv -c:v libx264 -c:a ac3 -crf 20 -map 0:v:0 -map 0:a:1 vidui.mp4
Here, 0:v:0
is the first video stream and 0:a:1
is the second audio stream.
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.
Best Answer
Make sure you run the latest version of FFmpeg. For Windows and Linux, static builds are availabe from the homepage. For macOS, you can install FFmpeg through Homebrew.
Then, in the simplest case run:
Setting video quality
For controlling video quality, set the
crf
parameter, which defaults to 23. Lower means better quality, but higher file size. Try values between 19 and 26 to see what fits best. You can also set a certain bit rate, depending on which file size you want. Here, for example, 500 kBit/s:For audio, you can set the bit rate too, with
-b:a
.Multiple channel audio
If your audio stream is using multiple channels (e.g. 5.1 sound), you need to use another AAC encoder (
libfdk_aac
). This encoder is not available in the static builds, but can be obtained with the pre-packaged / Homebrew versions of ffmpeg.Copying all streams
In case your input file has more than one video, audio and subtitle stream, ffmpeg by default does not convert all of them.
Use
-map 0
to instruct ffmpeg to take all streams from the input file (see the FFmpeg Wiki for more info). This is useful for retaining different languages and subtitles that might be in the original.