How to use -map option of FFmpeg, i.e. how to map various input streams into output streams

The FFmpeg howto's examples (at the moment) are all about working with a single input file. Merging streams from multiple input files into a single output file is just as simple.

Let's say you saved some YouTube video to your PC. Typically the 720p versions do have audio streams, but the 1080p versions do not. But of course you would like to have the full HD version with sound. Smile Nothing easier.

First run ffmpeg on both input files:
$ ffmpeg -i 1080p.mp4 -i 720p.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1080p.mp4':
  Metadata:
    major_brand     : dash
    minor_version   : 0
    compatible_brands: iso6avc1mp41
    creation_time   : 2014-08-09 16:53:53
  Duration: 00:03:03.92, start: 0.000000, bitrate: 1970 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 1967 kb/s, 29.97 tbr, 90k tbn, 59.94 tbc
    Metadata:
      creation_time   : 2014-08-09 16:53:53
      handler_name    : VideoHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '720p.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2014-08-09 16:53:51
  Duration: 00:03:03.97, start: 0.000000, bitrate: 1184 kb/s
    Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1037 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #1:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 143 kb/s
    Metadata:
      creation_time   : 2014-08-09 16:53:51
      handler_name    : IsoMedia File Produced by Google, 5-11-2011

This will print out all the properties of all streams from both files and give each stream a unique ID. The video stream from 1080p.mp4 is named "0:0", the video stream from 720p.mp4 is called "1:0" and the audio stream from 720p.mp4 is called "1:1".
Now to get the video stream from 1080p.mp4 and the audio stream from 720p.mp4, all you have to do is to name these streams with -map options ...
$ ffmpeg -i 1080p.mp4 -i 720p.mp4 -map 0:0 -map 1:1 -c:v copy -c:a copy 1080p_w_audio.mp4

The various -c:... options tell FFmpeg what codec to use while adding the streams to the output file. We do not want any conversion, so we simply copy both streams. Voala! Smile

Syndicate content