How to add a new audio (not mixing) into a video using ffmpeg?
mp3 music to wav
ffmpeg -i music.mp3 music.wav
truncate to fit video
ffmpeg -i music.wav -ss 0 -t 37 musicshort.wav
mix music and video
ffmpeg -i musicshort.wav -i movie.avi final_video.avi
Replace audio
ffmpeg -i video.mp4 -i audio.wav -map 0:v -map 1:a -c:v copy -shortest output.mp4
- The
-map
option allows you to manually select streams / tracks. See FFmpeg Wiki: Map for more info. - This example uses
-c:v copy
to stream copy (mux) the video. No re-encoding of the video occurs. Quality is preserved and the process is fast.- If your input audio format is compatible with the output format then change
-c:v copy
to-c copy
to stream copy both the video and audio. - If you want to re-encode video and audio then remove
-c:v copy
/-c copy
.
- If your input audio format is compatible with the output format then change
- The
-shortest
option will make the output the same duration as the shortest input.
Add audio
ffmpeg -i video.mkv -i audio.mp3 -map 0 -map 1:a -c:v copy -shortest output.mkv
- The
-map
option allows you to manually select streams / tracks. See FFmpeg Wiki: Map for more info. - This example uses
-c:v copy
to stream copy (mux) the video. No re-encoding of the video occurs. Quality is preserved and the process is fast.- If your input audio format is compatible with the output format then change
-c:v copy
to-c copy
to stream copy both the video and audio. - If you want to re-encode video and audio then remove
-c:v copy
/-c copy
.
- If your input audio format is compatible with the output format then change
- The
-shortest
option will make the output the same duration as the shortest input.
Mixing/combining two audio inputs into one
Use video from video.mkv
. Mix audio from video.mkv
and audio.m4a
using the amerge filter:
ffmpeg -i video.mkv -i audio.m4a -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map 0:v -map "[a]" -c:v copy -ac 2 -shortest output.mkv
See FFmpeg Wiki: Audio Channels for more info.
Generate silent audio
You can use the anullsrc filter to make a silent audio stream. The filter allows you to choose the desired channel layout (mono, stereo, 5.1, etc) and the sample rate.
ffmpeg -i video.mp4 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-c:v copy -shortest output.mp4
Also see
- Combine two audio streams into one
- FFmpeg Wiki: Audio Channel Manipulation
- FFmpeg mux video and audio from another video