How to add a new audio (not mixing) into a video using ffmpeg?

204,283

Solution 1

Replace audio

diagram of audio stream replacement

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.
  • The -shortest option will make the output the same duration as the shortest input.

Add audio

diagram of audio stream addition

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.
  • The -shortest option will make the output the same duration as the shortest input.

Mixing/combining two audio inputs into one

diagram of audio downmix

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

Solution 2

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

Solution 3

If the input video has multiple audio tracks and you need to add one more then use the following command:

ffmpeg -i input_video_with_audio.avi -i new_audio.ac3 -map 0 -map 1 -codec copy output_video.avi

-map 0 means to copy (include) all streams from the first input file (input_video_with_audio.avi) and -map 1 means to include all streams (in this case one) from the second input file (new_audio.ac3).

Solution 4

None of these solutions quite worked for me. My original audio was being overwritten, or I was getting an error like "failed to map memory" with the more complex 'amerge' example. It seems I needed -filter_complex amix.

ffmpeg -i videowithaudioyouwanttokeep.mp4 -i audiotooverlay.mp3 -vcodec copy -filter_complex amix -map 0:v -map 0:a -map 1:a -shortest -b:a 144k out.mkv

Solution 5

Nothing quite worked for me (I think it was because my input .mp4 video didn't had any audio) so I found this worked for me:

ffmpeg -i input_video.mp4 -i balipraiavid.wav -map 0:v:0 -map 1:a:0 output.mp4

Share:
204,283

Related videos on Youtube

Vetalll
Author by

Vetalll

Android developer.

Updated on September 05, 2020

Comments

  • Vetalll
    Vetalll almost 4 years

    I used a command like:

    ffmpeg -i video.avi -i audio.mp3 -vcodec codec -acodec codec output_video.avi -newaudio
    

    in latest version for adding new audio track to video (not mix).

    But I updated the ffmpeg to the newest version (ffmpeg version git-2012-06-16-809d71d) and now in this version the parameter -newaudio doesn't work.

    Tell me please how I can add new audio to my video (not mix) using ffmpeg.

    • Vetalll
      Vetalll about 7 years
      @Kiquenet Yes, you are right. And answer is already given. Just read this thread. This question is 5 years old.
    • tommy.carstensen
      tommy.carstensen almost 5 years
      By adding new audio you mean replacing the audio, right? I might edit your question to make that clear.
    • Ham
      Ham almost 3 years
      it would be better to clarify what you try to achieve in this question , adding a new audio in the context could mean (1) apply the audio stream in audio.mp3 to the output file (2) the 2 audio streams in audio.mp3 and video.avi co-exist in the output file , then let application switch between them .
  • Vetalll
    Vetalll almost 12 years
    as i remember, last variant will replace the old audio with new.
  • yonix
    yonix almost 11 years
    This didn't work for me at first, but using -map 0:0 -map 1:0 instead of -map 0 -map 1 did the trick for me. Thanks!
  • Iqbal Malik
    Iqbal Malik over 10 years
    Is there any option to merge new audio with old audio?
  • Lenar Hoyt
    Lenar Hoyt over 9 years
    This inserts an empty frame at the beginning.
  • Allan
    Allan almost 9 years
    @jwilson i have try this command , but i lose the video audio . my video format is mp4
  • Allan
    Allan almost 9 years
    i have try this command , but i lose the video audio . my video format is mp4
  • llogan
    llogan almost 9 years
    @Allan.Chan The original question did not want the audio from the video input, but I added another example: see the Mixing/combining another audio input example.
  • mmj
    mmj over 7 years
    The 1st solution without the -shortest option worked better for me.
  • llogan
    llogan over 7 years
    @chovy Your ffmpeg wasn't compiled with libvorbis support. More info.
  • Javier Mr
    Javier Mr over 7 years
    The option to add a silent audio streams rocks! I was trying to send a muted video with Telegram and wanted to show it as a video not as a gif (Telegram gui show short silent videos as if they were gifs); adding an empty audio stream tricked Telegram!
  • sebastian.roibu
    sebastian.roibu over 7 years
    Why do you use -shortest ? What does this do?
  • Kiquenet
    Kiquenet about 7 years
    @Allan solution for not lose ?
  • Kiquenet
    Kiquenet about 7 years
    What is Environment.getExternalStorageDirectory().getAbsolutePath()+‌​"/videoaudiomerger/"‌​+"Vid"+"output"+i1+"‌​.mp4"); ?
  • 1234567
    1234567 almost 7 years
    Ia ma trying the command for Mixing/combining two audio inputs into one
  • 1234567
    1234567 almost 7 years
    I am trying the command for Mixing/combining two audio inputs into one, how can we change volume of (audio in video) and the new audio we want to add, more details in question stackoverflow.com/questions/46168296/…
  • Nabi K.A.Z.
    Nabi K.A.Z. over 6 years
    I thinks, this answer must be the best answer.
  • Shevach Riabtsev
    Shevach Riabtsev almost 4 years
    i have mp4 with single video and single audio stream, i use the following command (w/o 'map'): ffmpeg -i gameclip.mp4 -i new.mp3 -codec copy -shortest -y test.mp4 In the new file mp4 the old audio is replaced with the new one. The new audio is not added as more audio track. In order to add the new audio w/o overwriting existing audio tracks i use '-map' : -map 0:v:0 -map 0:a:0 -map 1:a:0
  • Vivek Thummar
    Vivek Thummar over 3 years
    i'm using amovie= audioPath.. for the same but it's failing command when audio input name contains , is there any solution for this?
  • llogan
    llogan over 3 years
    @VivekThummar Not related to this Q/A. You should ask as a new question.
  • sistematico
    sistematico over 3 years
    Best answer ever.
  • Hang Chen
    Hang Chen almost 3 years
    If you met Timestamps are unset in a packet for stream 0. warning you could try adding -fflags +genpts before your first -i. trac.ffmpeg.org/ticket/7434
  • Hang Chen
    Hang Chen almost 3 years
    This doc helps me understand -map to further suit my needs - trac.ffmpeg.org/wiki/Map
  • Michael
    Michael about 2 years
    is this re-encoding everything?