copied from google ai 9-19-26
ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 output.mp4
Command Breakdown
-i video.mp4 & -i audio.wav: Defines your input files. The video file is designated as input 0 and the audio file as input 1.
-c:v copy: Copies the video stream directly without re-encoding it. This retains original quality and processes in seconds.
-c:a aac: Encodes the audio to AAC, which is the most compatible format for MP4 containers. (Note: If your audio is already an MP4-compatible format like .m4a or .aac, you can change this to -c:a copy to avoid audio re-encoding as well).
-map 0:v:0 -map 1:a:0: Explicitly tells FFmpeg to grab the first video stream from the first input file, and the first audio stream from the second input file. This ensures you don’t accidentally pull unwanted background tracks.
Common Variations & Use Cases
1. The Video Already Has Audio (Replace the Audio Track)If your original video file has an audio track you want to completely discard and replace with the new audio file, the map commands above will naturally do this by overriding the old stream. To force the output to cut off as soon as the shortest file ends (preventing a frozen frame or trailing silence), add the -shortest flag:
ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 -shortest output.mp4
2. Keep the Original Video Audio AND Mix in the New Audio
If you want to keep the original video’s audio (e.g., background noise) and mix it together with your new audio file (e.g., a voiceover), use the amix filter:
ffmpeg -i video.mp4 -i audio.wav -filter_complex “[0:a][1:a]amix=inputs=2:duration=longest” -c:v copy -c:a aac output.mp4
3. Pure Muxing (No Re-encoding at All)
If your video is an H.264/H.265 stream and your audio is already an AAC/MP3 stream, you can skip encoding altogether. This is the absolute fastest method:
ffmpeg -i video.mp4 -i audio.aac -c copy output.mp4
