I sometimes receive video files in separate audio and video components. Video is typically delivered as an MP4 file while audio is delivered either as an MP3 or an MP4 file. I then need to merge these into a single MP4 video file.
Below is how to do this using ffmpeg.
Marge MP4 video and MP3 audio
To merge a video file (video.mp4) and an audio file (audio.mp3) into a single video file with both the video and audio using ffmpeg, you can use the following command:
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental output.mp4
Explanation of ffmpeg options:
-i video.mp4
: Specifies the input video file (video.mp4 in this case). Replace video.mp4 with the actual filename or path to your video file.-i audio.mp3
: Specifies the input audio file (audio.mp3 in this case). Replace audio.mp3 with the actual filename or path to your audio file.-c:v copy
: Copies the video codec from the input video file to the output file without re-encoding it. This helps maintain the original video quality and avoids unnecessary processing.-c:a aac
: Specifies the audio codec for the output file as AAC. This will encode the audio in AAC format.-strict experimental
: This is necessary if your FFmpeg version is older than 3.1. It enables experimental AAC audio encoding.output.mp4
: Specifies the output file name (output.mp4 in this case). Replace output.mp4 with your desired file name or path.
Merge MP4 video and MP4 audio
To merge a video file (video.mp4) and an audio file (audio.mp4) into a single video file with both the video and audio using ffmpeg, you can use the following command:
ffmpeg -i video.mp4 -i audio.mp4 -c:v copy -c:a copy output.mp4
Explanation of options:
-i video.mp4
: Specifies the input video file (video.mp4 in this case). Replace video.mp4 with the actual filename or path to your video file.-i audio.mp4
: Specifies the input audio file (audio.mp4 in this case). Replace audio.mp4 with the actual filename or path to your audio file.-c:v copy
: Copies the video codec from the input video file to the output file without re-encoding it. This helps maintain the original video quality and avoids unnecessary processing.-c:a copy
: Copies the audio codec from the input audio file to the output file without re-encoding it. This preserves the original audio quality and avoids unnecessary processing.output.mp4
: Specifies the output file name (output.mp4 in this case). Replace output.mp4 with your desired file name or path.