9

I have a video file in MKV format. I like the quality of the video, but I dislike having the audio in FLAC format since I decided it takes up too much space.

It is a dual audio file—it’s an anime with Japanese and English audio—and it has several subtitle streams inside as well.

This is the command I use:

ffmpeg -i "01.mkv" -c:v copy -c:a ac3 -c:s copy "test.mkv"

However it only gets the first audio and first subtitle string. I need help with the map option for multiple streams.

6
  • What is the problem with the command you currently have? I suspect you need a -map 0:a? -map 0:s? -map 0:v before your -c:v, but apart from that it looks okay to me.
    – Mokubai
    Dec 2, 2017 at 22:51
  • Right now it only gets the first audio and first subtitle string. I will attempt with the map option. I read about it, but I couldn't quite understand how to use it. Would it look like this then? ffmpeg -i "01.mkv" -map 0:a? -map 0:s? -map 0:v -c:v copy -c:a ac3 -c:s copy "test.mkv" Dec 2, 2017 at 22:54
  • Look at the answers here if you haven’t already. Dec 2, 2017 at 22:56
  • Yes, give that a shot. I'm still getting to grips with ffmpeg myself, but the map command is used to tell it that you definitely want those things to be pulled through to the output. -map 0:a:1 would specify only to copy audio stream number 1, while -map 0:a? should effectively wildcard it and copy them all.
    – Mokubai
    Dec 2, 2017 at 23:05
  • Just encoded my test file and it seems to have worked perfectly! Thanks! Now to do the part I have no idea about, which is to write a bash script. But that is for another question. Thanks again! Dec 2, 2017 at 23:07

3 Answers 3

11

Add -map 0 after the input, which includes in the output all streams of all types:

ffmpeg -i "01.mkv" -map 0 -c:v copy -c:a ac3 -c:s copy "test.mkv"

You can also tell the map command to include only some stream types.

-map 0:a would copy all audio streams and -map 0:a:1 would copy only the first audio stream.

If you have input that may or may not have a stream of that type, append ? at the end (i.e. -map 0:a?).

Type Matches
v Video
V Video except attached pictures, video thumbnails, cover arts
a Audio
s Subtitles
d Data
t Attachments
2
  • thank you that worked. for some reason -c:v short cuts do not work, I had to use -acodec -vcodec and -scodec
    – manit
    Oct 15, 2021 at 18:57
  • confirmed correct 24 Oct 2021
    – Chris Reid
    Oct 24, 2021 at 23:52
9

I believe you need to specify the mapping of the audio and subtitle streams to ensure that all of them are copied through rather than the first. To do so you need to add -map 0:a? -map 0:s? -map 0:v before your -c:v

This should make your command

ffmpeg -i "01.mkv" -map 0:a? -map 0:s? -map 0:v -c:v copy -c:a ac3 -c:s copy "test.mkv"

The map command is used to tell it that you definitely want those things to be pulled through to the output. -map 0:a:1 would specify only to copy audio stream number 1, while -map 0:a? should effectively wildcard it and copy them all.

1
  • 4
    A map expression of the form -map x? tells ffmpeg to select all streams matching x but not throw an error if no matching streams are found. Just -map x will select all matching streams.
    – Gyan
    Dec 3, 2017 at 4:34
1

Just use "-map 0 -scodec copy" instead of having to specify the [v]ideo, [a]udio, and [s]ubtitles manually with all these options. Works for me.

From the documentation:

For example, to map ALL streams from the first input file to output

ffmpeg -i INPUT -map 0 output

The command I use to convert H.265 videos to H.264 (while leaving the audio and subtitles alone) is:

ffmpeg -i source.mkv -vcodec h264 -acodec copy -scodec copy -map 0 output.mkv

Try it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.