Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMPEG mux video and audio (from another video) - mapping issue

I would like to place the audio from a video to another video without an audio (in one command):

ffmpeg.exe -i video1_noAudio.mov -i video2_wAudio.mov -vcodec copy -acodec copy video1_audioFromVideo2.mov 

I guess "-map" is the correct way to do it but I got confused with it.

Can you suggest how to resolve it?

like image 962
Mark Avatar asked Oct 17 '12 16:10

Mark


People also ask

How do I Mux audio and video?

To work with this add-on, just open the app UI and drag input files (video & audio) in the app UI. Then, press on the - Command - button to insert the mux command and finally press on the - Mux - button to start the job.

What is Vsync FFmpeg?

According to ffmpeg documentation, -vsync 0 is a passthrough, where each frame is passed with its timestamp from the demuxer to the muxer. -vsync 1 duplicates frames and timecode to achieve exactly the requested constant frame rate.

What is FFmpeg encoding?

ffmpeg is a command-line tool that converts audio or video formats. It can also capture and encode in real-time from various hardware and software sources such as a TV capture card. ffplay is a simple media player utilizing SDL and the FFmpeg libraries.


2 Answers

Overview of inputs

input_0.mp4 has the desired video stream and input_1.mp4 has the desired audio stream:

mapping diagram

In ffmpeg the streams look like this:

$ ffmpeg -i input_0.mp4 -i input_1.mp4  Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input_0.mp4':   Duration: 00:01:48.50, start: 0.000000, bitrate: 4144 kb/s     Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 4014 kb/s, SAR 115:87 DAR 1840:783, 23.98 fps, 23.98 tbr, 16k tbn, 47.95 tbc (default)     Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 124 kb/s (default)  Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'input_1.mp4':   Duration: 00:00:30.05, start: 0.000000, bitrate: 1754 kb/s     Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 720x480 [SAR 8:9 DAR 4:3], 1687 kb/s, 59.94 fps, 59.94 tbr, 60k tbn, 119.88 tbc (default)     Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 55 kb/s (default) 

ID numbers

ffmpeg refers to input files and streams with index numbers. The format is input_file_id:input_stream_id. Since ffmpeg starts counting from 0, stream 1:1 refers to the audio from input_1.mp4.

Stream specifiers

This can be enhanced with stream specifiers. For example, you can tell ffmpeg that you want the first video stream from the first input (0:v:0), and the first audio stream from the second input (1:a:0). I prefer this method because it's more efficient. Also, it is less prone to accidental mapping because 1:1 can refer to any type of stream, while 2:v:3 only refers to the fourth video stream of the third input file.

Examples

The -map option instructs ffmpeg what streams you want. To copy the video from input_0.mp4 and audio from input_1.mp4:

$ ffmpeg -i input_0.mp4 -i input_1.mp4 -c copy -map 0:0 -map 1:1 -shortest out.mp4 

This next example will do the same thing:

$ ffmpeg -i input_0.mp4 -i input_1.mp4 -c copy -map 0:v:0 -map 1:a:0 -shortest out.mp4 
  • -map 0:v:0 can be translated as: from the first input (0), select video stream type (v), first video stream (0)

  • -map 1:a:0 can be translated as: from the second input (1), select audio stream type (a), first audio stream (0)

Additional Notes

  • With -c copy the streams will be stream copied, not re-encoded, so there will be no quality loss. If you want to re-encode, see FFmpeg Wiki: H.264 Encoding Guide.

  • The -shortest option will cause the output duration to match the duration of the shortest input stream.

  • See the -map option documentation for more info.

like image 168
llogan Avatar answered Sep 28 '22 15:09

llogan


I have a new command for merging audio to video

ffmpeg -i video.mp4 -i audio.mp4 -map 0.0 -map 1.0 -acodec copy -qscale 4 -vcodec mpeg4 outvideo.mp4 

-qscale is option set quality to video of ffmpeg

-acodec copy is option copy default quality of audio to output video

-vcodec mpeg4 is option copy default quality of video to output video

like image 26
HoangHieu Avatar answered Sep 28 '22 15:09

HoangHieu