14

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.

xpt
  • 8,261
  • 38
  • 102
  • 156
Jason Murray
  • 191
  • 1
  • 1
  • 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 02 '17 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"` – Jason Murray Dec 02 '17 at 22:54
  • [Look at the answers here](https://superuser.com/q/482570/167207) if you haven’t already. – Giacomo1968 Dec 02 '17 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 02 '17 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! – Jason Murray Dec 02 '17 at 23:07
  • Glad to help, I've consolidated it into an answer. – Mokubai Dec 02 '17 at 23:10

4 Answers4

16

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
Matej Snoha
  • 349
  • 2
  • 4
12

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.

Mokubai
  • 89,133
  • 25
  • 207
  • 233
  • 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 03 '17 at 04:34
3

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!

0

The code

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

only works for me when I have a single source! If I have Video in one file "01.mkv" Audio in another file "02.ac3" and 2 subtitles in another file "subs.mkv" I need to do something like

ffmpeg -i "01.mkv" -i "02.ac3" -i "subs.mkv" -i "subs.mkv" -map 0:v -map 1:a -map 2:s:0 -map 3:s:1 -c:v copy -c:a copy -c:s copy "test.mkv"

Otherwise the file corrupts. note that "subs.mkv" is listed as input 2 times on purpose because listing it 1 time then using

-map 2:s:0 -map 2:s:1

corrupts the file!