Mux Video and Audio from Two Videos - 2020
We're going to mux two videos one for video source and the other one for audio source:
- Video source: San Francisco & GoldenGate Bridge (SF.mp4)
- Audio source: "San Francisco (Be Sure to Wear Flowers in Your Hair)" by Scott McKenzie (TwoBeSureToWear.mp4)
$ ffmpeg -i SF.mp4 -i TwoBeSureToWear.mp4 ffmpeg version 2.2.git Copyright (c) 2000-2014 the FFmpeg developers ... Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'SF.mp4': Stream #0:0L(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1170x714, 2661 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default) Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'TwoBeSureToWear.mp4': Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 960x720 [SAR 1:1 DAR 4:3], 847 kb/s, 29.97 fps, 29.97 tbr, Stream #1:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
The command looks like this:
$ ffmpeg -i SF.mp4 -i TwoBeSureToWear.mp4 -c copy -map 0:0 -map 1:1 -shortest SanFrancisco.mp4
From the first source (input #0) we take video (stream 0) (map 0:0), and from the 2nd source (input #1) we take audio (stream #1) (-map 1:1)
Note that we can achieve the same thing using the following command:
$ ffmpeg -i SF.mp4 -i TwoBeSureToWear.mp4 -c copy -map 0:v:0 -map 1:a:0 -shortest SanFrancisco2.mp4
where:
- -map 0:v:0 can be translated as: from the first input (0), take the video (v), first output video stream (0)
- -map 1:a:0 can be translated as: from the second input (1), take the audio (a), first output audio stream (0)
- -c copy : The desired streams will be copied, not re-encoded, so there will be no quality loss, and we'll get output quickly.
- -shortest : This option option will cause the output duration to match the duration of the shortest input stream.
The other parameters:
To delay the audio start, we set an offset of 1 minute with -itsoffset option just before -i option as shown below:
$ ffmpeg -i SF.mp4 -itsoffset 00:01:00.000 -i TwoBeSureToWear.mp4 -c copy -map 0:0 -map 1:1 -shortest SanFranciscoC.mp4
Suppose we want to mux one video file which has video (valid) and audio (not needed) with another audio file which has desirable audio. We can just follow the logic of the previous section, and the command for concatenation looks like this:
$ ffmpeg -i SF.mp4 -i Two_BeSureToWearFlowersInYourHair.mp3 -c copy -map 0:0 -map 1:0 -shortest SanFranciscoB.mp4
We took from 1st input (0) a video (0), and from 2nd input (1) an audio (0).
The input audio file is available: Two_BeSureToWearFlowersInYourHair.mp3.
The out file is here: SanFranciscoB.mp4.
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization