251

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.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Vetalll
  • 3,472
  • 6
  • 24
  • 34
  • @Kiquenet Yes, you are right. And answer is already given. Just read this thread. This question is 5 years old. – Vetalll May 11 '17 at 08:03
  • 1
    By adding new audio you mean replacing the audio, right? I might edit your question to make that clear. – tommy.carstensen Aug 24 '19 at 21:49
  • 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 . – Ham Jul 15 '21 at 16:32

10 Answers10

579

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

llogan
  • 121,796
  • 28
  • 232
  • 243
  • 8
    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! – yonix Sep 07 '13 at 21:10
  • This inserts an empty frame at the beginning. – Lenar Hoyt Jan 03 '15 at 19:24
  • i have try this command , but i lose the video audio . my video format is mp4 – Allan Aug 14 '15 at 03:03
  • @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. – llogan Aug 15 '15 at 06:05
  • The 1st solution without the `-shortest` option worked better for me. – mmj Nov 12 '16 at 15:31
  • @chovy Your `ffmpeg` wasn't compiled with libvorbis support. [More info](https://trac.ffmpeg.org/wiki/Errors). – llogan Jan 23 '17 at 06:16
  • 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! – Javier Mr Mar 03 '17 at 21:28
  • Why do you use -shortest ? What does this do? – sebastian.roibu Mar 23 '17 at 08:46
  • Ia ma trying the command for Mixing/combining two audio inputs into one – 1234567 Sep 12 '17 at 05:24
  • 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 https://stackoverflow.com/questions/46168296/mix-audio-to-video-changing-volume-of-both-video-and-audio-ffmpeg – 1234567 Sep 12 '17 at 05:25
  • 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 – Shevach Riabtsev Sep 04 '20 at 15:59
  • i'm using `amovie= audioPath..` for the same but it's failing command when audio input name contains `,` is there any solution for this? – Vivek Thummar Jan 01 '21 at 11:20
  • @VivekThummar Not related to this Q/A. You should ask as a new question. – llogan Jan 01 '21 at 18:46
  • Best answer ever. – sistematico Feb 15 '21 at 07:28
  • If you met `Timestamps are unset in a packet for stream 0.` warning you could try adding `-fflags +genpts ` before your first `-i`. https://trac.ffmpeg.org/ticket/7434 – Hang Chen Aug 11 '21 at 19:27
  • 1
    This doc helps me understand `-map` to further suit my needs - https://trac.ffmpeg.org/wiki/Map – Hang Chen Aug 11 '21 at 19:28
  • I'm working with your cmd "Mixing/combining two audio inputs into one" to add background music to my video. But how can I loop "audio.mp3" into video if the length of video longer than audio? And how can I change volume of audio? – VnDevil Nov 15 '22 at 16:30
  • Thank you very much I learned a lot with this. Is there a way to give the audio a name? For example when I go to the audio part in VLC player I see: Track 1 [english], Track 2 [portugese]...but when I add a audio with -map I only see something like Track 2.... – Ricardo Bohner Nov 27 '22 at 13:36
  • does anyone have any idea on how to record audio from Android Phone? using ffmpeg. https://stackoverflow.com/questions/76643602/ffmpeg-command-to-capture-video-audio-from-android-mobile – Uday Jul 10 '23 at 16:37
30

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
nbrooks
  • 18,126
  • 5
  • 54
  • 66
jwilson
  • 309
  • 2
  • 6
28

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).

user2513149
  • 850
  • 12
  • 15
7

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
Tod
  • 71
  • 1
  • 1
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

1

This shows how to merge all audio tracks into one entire directory with ffmpeg:

ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -c:v copy -shortest output.mp4
OliverHennhoefer
  • 677
  • 2
  • 8
  • 21
schwarz
  • 11
  • 1
1

Here is how I did what the OP wanted.

My setup is I have two stream of media one video (with its own audio channel) & one audio. I am not converting from but I am restreaming live source by integrating it with an additional audio channel without replacing the old audio from the video stream.

Here is the code I used.

ffmpeg -i "Video stream with its own audio" -i "another audio stream" -map 0:v -map 0:a -map 1:a -shortest -f mpegts "multicast udp stream out put"

what the code does is, it maps each video and audio channels after it acquired the streams from the live source. -map 0:v is the video stream, -map 0:a is the audio that is coming from the video source (notice the 0s from the -map) and finally -map 1:a which is the audio stream from the second source.

then it just restreams it using mpegts through a multicast address. You can change this to a file, a unicast stream or any other supported output format.

Here is the code I am using.

ffmpeg -i "rtp://@231.0.0.208:1234" -i "rtp://@231.0.0.206:1234" -map 0:v -map 0:a -map 1:a -shortest -f mpegts "udp://@231.0.0.45:1234"

Hope this helps some one. Thanks!

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Enthusiast
  • 13
  • 5
0

If you are using an old version of FFMPEG and you cant upgrade you can do the following:

ffmpeg -i PATH/VIDEO_FILE_NAME.mp4 -i PATH/AUDIO_FILE_NAME.mp3 -vcodec copy -shortest DESTINATION_PATH/NEW_VIDEO_FILE_NAME.mp4

Notice that I used -vcodec

Waqleh
  • 9,741
  • 8
  • 65
  • 103
0

Code to add audio to video using ffmpeg.

If audio length is greater than video length it will cut the audio to video length. If you want full audio in video remove -shortest from the cmd.

String[] cmd = new String[]{"-i", selectedVideoPath,"-i",audiopath,"-map","1:a","-map","0:v","-codec","copy", ,outputFile.getPath()};

private void execFFmpegBinaryShortest(final String[] command) {



            final File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/videoaudiomerger/"+"Vid"+"output"+i1+".mp4");




            String[] cmd = new String[]{"-i", selectedVideoPath,"-i",audiopath,"-map","1:a","-map","0:v","-codec","copy","-shortest",outputFile.getPath()};


            try {

                ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
                    @Override
                    public void onFailure(String s) {
                        System.out.println("on failure----"+s);
                    }

                    @Override
                    public void onSuccess(String s) {
                        System.out.println("on success-----"+s);
                    }

                    @Override
                    public void onProgress(String s) {
                        //Log.d(TAG, "Started command : ffmpeg "+command);
                        System.out.println("Started---"+s);

                    }

                    @Override
                    public void onStart() {


                        //Log.d(TAG, "Started command : ffmpeg " + command);
                        System.out.println("Start----");

                    }

                    @Override
                    public void onFinish() {
                        System.out.println("Finish-----");


                    }
                });
            } catch (FFmpegCommandAlreadyRunningException e) {
                // do nothing for now
                System.out.println("exceptio :::"+e.getMessage());
            }


        }
Saurav Prakash
  • 588
  • 1
  • 5
  • 26
  • 1
    What is `Environment.getExternalStorageDirectory().getAbsolutePath()+"/videoaudiomerger/"+"Vid"+"output"+i1+".mp4");` ? – Kiquenet Apr 28 '17 at 23:18
0

The marked answer does not set the audio track's language.

The following is an example that specifies German for the default audio track (the video's only audio channel) and English for the audio track that is added anew:

ffmpeg -i "/dir/video.mkv" -i "/dir2/audio.ac3" -map 0 -map 1:a -c:v copy -shortest -metadata:s:a:0 language=ger -metadata:s:a:0 title="GER" -metadata:s:a:1 language=eng -metadata:s:a:1 title="ENG" "/dir/output.mkv"

(The s:a:0 starts counting from 0, adjust that number as needed. If the audio track language is already specified, you don't need to set it and would only need to set it for the audio track that you add.)

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
mYnDstrEAm
  • 751
  • 2
  • 8
  • 26