12

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
  • 121
  • 1
  • 1
  • 4

1 Answers1

33

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:

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Thanks 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_ – Jonathan Nov 28 '13 at 14:54
  • @Jonathan You need to show your actual ffmpeg command and the complete ffmpeg console output. – llogan Nov 28 '13 at 19:45
  • A 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! – Baptiste Mathus Aug 07 '14 at 20:16
  • I 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. – Ben Aug 11 '15 at 05:15