8

I'm trying to automate FFmpeg to remux all video files in a given directory to MKV. I'm currently using

ffmpeg -i $INPUT -c copy $OUTPUT.mkv

However, some streams are skipped that way - for example, if there are 2 audio streams only 1 goes to the output file.

How do I specify that all streams from the input must be copied to the output?

Since I'm trying to automate FFmpeg, it would be best if the command does not change for each file, i.e. manually specifying all the streams with -map would require me to first parse every file. I would do this if I need to, but if there is a better solution I'd prefer that.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 1
    Per the agreement on [your own Meta discussion](http://meta.stackexchange.com/q/168740/159624), this question should have gone to Super User. I voted to migrate. – slhck Feb 24 '13 at 14:26
  • 1
    From http://meta.stackexchange.com/a/168743/170007 - *Developing applications using FFmpeg.exe and writing scripts to automate its usage → on-topic for both, but more on-topic for SU*. I'm trying to automate ffmpeg, so it's ontopic here, right? Oh, and thanks for the answer :) – sashoalm Feb 24 '13 at 14:28
  • As you like—I think it's borderline because there's not a programming language involved, and if it's `$INPUT` or `input.mp4` doesn't make a difference really. It's just that I've seen many of these questions closed here for the wrong reasons, so rather have them migrated before they end up being deleted at some point :) You're welcome. – slhck Feb 24 '13 at 14:37

1 Answers1

21

How do I specify that all streams from the input must be copied to the output?

The -map option can do this, with the shortcut -map 0.

For example:

ffmpeg -i input.mkv -c copy -map 0:0 -map 0:2 output.mkv

to copy the stream 0:0 and 0:2 to output.mkv.

ffmpeg -i input.mkv -c copy -map 0 output.mkv

to copy all input streams from input 0 (input.mkv) to the output (even if there are multiple video, audio or subtitle streams).

The -map value corresponds to the input number (0 is the first input, 1 is the second input, etc): If you add an additional input (-> input 1), and also want to copy all of the contents, then you will need to add -map 1.

You can use ffprobe to analyse files and see which stream is mapped where. Try -fflags +genpts if you get an unknown timestamp error. For a detailed tutorial, see the FFmpeg Wiki page on the -map option.

Dej
  • 271
  • 3
  • 10
slhck
  • 36,575
  • 28
  • 148
  • 201
  • 8
    And if you get the error `Can't write packet with unknown timestamp` you have to add `-fflags +genpts` after the ffmpeg command. See [this bug](https://trac.ffmpeg.org/ticket/1979) and the workaround mentioned here http://askubuntu.com/a/413968/135671 – erik Feb 01 '14 at 09:51
  • In case anyone finds this answer but can't find `ffmpeg`; `ffmpeg` is no longer present in the Ubuntu 14.04 repositories, but its fork `avconv` does the same (`sudo apt-get install libav-tools`). – JeroenHoek Jun 02 '14 at 18:12
  • @JeroenHoek In addition, ffmpeg builds can be downloaded from http://ffmpeg.org/download.html – slhck Jun 02 '14 at 20:00
  • The wiki link now redirects here: https://trac.ffmpeg.org/wiki/Map It is probably best to update it. – Nulano Apr 07 '17 at 04:19
  • @Nulano Thanks. Next time you can just propose an edit to the post yourself! – slhck Apr 07 '17 at 08:38