Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an external audio track to a video file using VLC or FFMPEG command line

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.

like image 202
Jonathan Avatar asked Nov 27 '13 23:11

Jonathan


1 Answers

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:

  • FFmpeg Wiki: H.264 Video Encoding Guide
  • FFmpeg Wiki: AAC Audio Encoding Guide
like image 153
llogan Avatar answered Nov 07 '22 19:11

llogan