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: