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.
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.
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.
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
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
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
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
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!
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());
}
}
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.)
I have video file input.mp4 that contain video and two audio source - speaking, and music.mp3 that contain background music. Now, I want to merge them.
Here is the command that i use:
ffmpeg -i input.mp4 -i music.mp3 -c:v copy -filter_complex "[0:a]aformat = fltp:44100:stereo,apad[0a];[1] aformat=fltp:44100:stereo,volume=1.5[1a];[0a] [1a] amerge[a]" -map 0:v -map "[a]" -ac 2 -y -shortest output.mp4
input file
https://d9f35555a8b3e9044c8d-95c21efaab8093d23d4124e599a618ee.ssl.cf5.rackcdn.com/mub_audio/1e03dc67-5079-46d5-b692-d69bbb6ee3e3.mp4
audio file
https://d9f35555a8b3e9044c8d-95c21efaab8093d23d4124e599a618ee.ssl.cf5.rackcdn.com/mub_audio/a6cab88a63eb11eb8d8b901b0efa6f1d.mp3
you can check the output file here
https://d9f35555a8b3e9044c8d-95c21efaab8093d23d4124e599a618ee.ssl.cf5.rackcdn.com/mub_audio/6e6e79ef-a732-49d9-8317-4ba97c05a352.mp4
The input file has a pause after every sentence. But in output, that pause duration doesn't have any background music.
What can be the reason? what is the solution for it?
I try concat two videos (1.mkv has 640:360 image size, 2.mkv has 1280:720 image size), both videos have 2 video tracks and 0 audio tracks. I tried this code:
ffmpeg -i 1.mkv -i 2.mkv -filter_complex "[0:v:0]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2[v0];[0:v:1]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2[v0];[v0][1:v:0][1:v:1]concat=n=2:v=2:a=0[v]" -map "[v]" -c:v "libvpx" 1+2.mkv
But I have wrong:
Filter pad has an unconnected output
Your filter labels need to be adjusted, and you need to change v=2 to v=1 in concat filter:
ffmpeg -i 1.mkv -i 2.mkv -filter_complex "[0:v:0]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2[v0];[1:v:0]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2[v1];[v0][v1]concat=n=2:v=1:a=0[v]" -map "[v]" -c:v libvpx 1+2.mkv
Since 2.mkv is already 1280x720 you can avoid processing that input:
ffmpeg -i 1.mkv -i 2.mkv -filter_complex "[0:v:0]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2[v0];[v0][1:v]concat=n=2:v=1:a=0[v]" -map "[v]" -c:v libvpx 1+2.mkv
I am trying to use FFMPEG to splice few videos and output one combined video.
I managed to get all video stream with this command :
ffmpeg.exe -i 1.mov -i 2.mov -filter_complex "[0:v]scale=1920:1080[v0];[1:v]scale=1920:1080[v1];[v0][v1] concat=n=2:v=1[v]" -map "[v]" out.mp4
Also, to add a dummy audio to a video with this command:
ffmpeg.exe -i 1.mov -f lavfi -i aevalsrc=0 -shortest -i out.mov
Above commands work perfectly, however 2.mov has an audio stream while 1.mov does not.
Is there any method that can set a dummy audio for 1.mov and then combine both video and audio streams from 1.mov and 2.mov at one go, so that output a combined video that can play sound when it is at clip 2.mov.
Use
ffmpeg.exe -i 1.mov -i 2.mov -f lavfi -t 1 -i anullsrc -filter_complex "[0:v]scale=1920:1080[v0];[1:v]scale=1920:1080[v1];[v0][2:a][v1][1:a] concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" out.mp4
-f lavfi -t 1 -i anullsrc adds a silent 1 second audio input, which is used as a counterpart to the video input from 1.mov. The concat filter will pad the audio to match the video duration of 1.mov.
I'm trying to concat 3 video files and add text to output using ffmpeg.
Each part is 10 sec long.
I've end up with this code:
ffmpeg -i output3.mp4 -i output2.mp4 -i output1.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[v][a]; [0:v:0]drawtext=fontfile=tahoma.ttf:text=Sample text:fontcolor=white:fontsize=40:box=1:boxcolor=black#0.7:boxborderw=5:x=100:y=100" -map "[v]" -map "[a]" output.mp4
The result video has 30 seconds but it hangs after 1st part (10s). When I remove drawtext filter part (just concat), then video is fine, but without text...
Anyone can help ?
Use
ffmpeg -i output3.mp4 -i output2.mp4 -i output1.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[v][a]; [v]drawtext=fontfile=tahoma.ttf:text=Sample text:fontcolor=white:fontsize=40:box=1:boxcolor=black#0.7:boxborderw=5:x=100:y=100[v]" -map "[v]" -map "[a]" output.mp4
Your existing syntax applied the text on top of the video stream of the first input file, instead of the resultant video from the concat filter.