How to add an external audio track to a video file using VLC or FFMPEG command line
Stream copy
Example to stream copy, or re-mux, the video from video.mp4
(input 0
) and the audio from audio.mp3
(input 1
):
ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -codec copy -shortest out.mp4
This will avoid encoding and will therefore be very quick and will not affect the quality.
Re-encode
You can tell ffmpeg what encoders to use if you do need to re-encode for size or if you need different formats than the inputs. Example to re-encode to H.264 video and AAC audio:
ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -codec:v libx264 \
-preset medium -crf 23 -codec:a aac -b:a 192k -shortest output.mp4
Notes:
The -map
option allows you to specify which stream you want, for example, -map 0:v
refers to the video stream(s) of the first input. If you do not tell ffmpeg what streams you want then it will use the default stream selection which is to choose one stream per stream type. The defaults are often fine, but it is recommended to be explicit so you can get expected results.
The -shortest
option instructs ffmpeg to end the output file when the shortest duration inputs ends.
Using a recent ffmpeg build is always recommended. Easiest method is to download a recent ffmpeg build, but you can also follow a guide to compile ffmpeg.
Also see:
Jonathan
Updated on July 05, 2022Comments
-
Jonathan almost 2 years
I want to add an audio.mp3 soundtrack to a soundless video.mp4 file using a bash script, what is the correct syntax of the "cvlc" "ffmpeg" command line ?
I've recorded the video with VLC and --no-audio option so there is no settings such as bit rate or encoding that can be copied from the original video.
-
Jonathan over 10 yearsThanks for the answer, I've tried both of your suggestions and it returns the following error message: Unrecognized option 'codec' Failed to set value 'copy' for option 'codec' I've tried with 'vcodec' option and it returns : Application provided invalid, non monotonically increasing dts to muxer in stream 0: 50 >= 50
-
llogan over 10 years@Jonathan You need to show your actual ffmpeg command and the complete ffmpeg console output.
-
Baptiste Mathus almost 10 yearsA quick note for something that just helped me using avconv instead of ffmpeg: as avconv is actually a fork of ffmpeg, taking the first exact command line "Stream Copy" above as-is and just replacing 'ffmpeg' with 'avconv' just worked totally fine for me. HTH!
-
Ben almost 9 yearsI tested this on data from youtube which streams the audio and video separately; after obtaining the source data (via httpfox and wget) the first command recombined the source files correctly.