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
.
Add audio to video: stream copy
ffmpeg -i video.avi -i audio.mp3 -codec copy -shortest output.avi
- Omitting the
-map
option will use the default stream selection. This will work if your video input has no audio.
- This example uses
-codec copy
to stream copy (no re-encoding; quality is preserved and it is fast).
- The
-shortest
option will make output.avi
the same duration as the shortest input.
Add audio to video: re-encode
If your output doesn't like the original formats, or if you want to change formats you can specify the encoders:
ffmpeg -i video.avi -i audio.mp3 -c:v libx264 -c:a libvorbis -shortest output.mkv
To manually choose specific streams
Sometimes the default stream selection won't give you the result you want. In this example video.mp4
has video and audio, and audio.m4a
only has audio. Use -map
to choose video from video.mp4
and audio from audio.m4a
:
ffmpeg -i video.mp4 -i audio.m4a -map 0:v -map 1:a -c copy -shortest output.mp4
-map 0:v
– From input 0
(the first input, which is video.mp4
) choose the v
ideo stream(s).
-map 1:a
– From input 1
(the second input, which is audio.m4a
) choose the a
udio stream(s).
Mixing/combining two audio inputs into one
Take video from video.webm
, and use the amerge filter to combine the audio from video.webm
and audio.oga
:
ffmpeg -i video.webm -i audio.oga -filter_complex \
"[0:a][1:a]amerge=inputs=2[a]" \
-map 0:v -map "[a]" -c:v copy -c:a libvorbis -ac 2 -shortest out.webm
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
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
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
).
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
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
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());
}
}