Merge both audio with delay use ffmpeg - audio

I have a project that wants to use ffmpeg to merge 2 audio 1.mp3 and 2.mp3 like this:
(length of audio 1.mp3 is longer than length of audio 2.mp3)
|<---------------------------------- 1st mp3 file ---------------------------------------->|
|<-----12s------><-----------2nd mp3 file -------------><-------15s------>|
result:
|<------------------------output_audio.mp3------------------------------->|
Ths for your help !

Use the adelay and amix filters:
ffmpeg -i 1.mp3 -i 2.mp3 -filter_complex "[1:a]adelay=12s:all=1[a1];[0:a][a1]amix=inputs=2[a]" -map "[a]" output.mp3

Related

ffmpeg: two videos side-by-side with audio1 lang=ger & audio2 lang=eng

I have two videos from two GoPro cameras. Both videos are rendered via ffmpeg side-by-side into a single video (left and right) and the audio is currently combined/mixed into two channels (stereo). I can hear both camera audio channels at the same time.
Two channels stereo:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[v]; [0:a][1:a]amerge[a]" -map "[v]" -map "[a]" -ac 2 side-by-side.mp4
audio from both videos mixed into a single file with 2 channels - mediainfo
Now I want to switch between the two audio channels (cam1 or cam2) while I'm playing the side-by-side video.
My first try: With four channels (without -ac 2 parameter):
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[v]; [0:a][1:a]amerge[a]" -map "[v]" -map "[a]" side-by-side.mp4
audio from both videos mixed into a single file with 4 channels - mediainfo
But the most video players can't easily select the channels 1+2 or 3+4 while playing.
So I tried two languages:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[v]; [0:a][1:a]amerge[a]" -map "[v]" -map "[a]" -metadata:s:a:0 language=ger -metadata:s:a:1 language=eng side-by-side.mp4
audio from both videos mixed into a single file with 4 channels with languages - mediainfo
But that's wrong. I can only see german with 4 channels. How can I put channels 1+2 into german and channels 3+4 into english? Afterwards I should be able to use the multi language feature from most video players to switch the audio between cameras.
Thank you,
Miriam
This worked for me
ffmpeg -i Video1.mp4 -i Video2.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[v]" -map [v] -map 0:a:0 -map 1:a:0 -metadata:s:a:0 language=ger -metadata:s:a:1 language=eng output.mp4
With this you should be able to switch between the audios

How to delay audio after a specific position with ffmpeg?

I have a 10 seconds a.mp4 with two streams: Stream #0 is a video stream and Stream #1 is a audio stream.
Now, I want to delay the audio stream by 4 seconds after the time position 00:03. It is to say, in the output file, I want that: 00:00-00:03 is the original audio, 00:03-00:07 has no sound, 00:07-00:14 is the original 00:03-00:10 audio.
I've tried this:
ffmpeg -i a.mp4 -t 00:00:03 -i a.map4 -itsoffset 4 -ss 00:00:03 -i a.mp4 -map 0:v -map 1:a -map 2:a -codec copy output.mp4
But it seems that there are two audio streams in the output.mp4 and only one of them can be played once. Then I tried amix filter:
ffmpeg -i a.mp4 -t 00:00:03 -i a.mp4 -itsoffset 4 -ss 00:00:03 -i a.mp4 -filter_complex "[1:a][2:a] amix=inputs=2" -map 0:v output.mp4
But it also doesn't work. I'm new to ffmpeg so I have no idea what should I do now? Any idea for me? Very much thanks!
Use the asetpts filter to change timestamps, and aresample to (optionally) insert silence in that gap.
ffmpeg -i a.mp4 -af "asetpts='if(lt(T\,3),PTS,PTS+4/TB)',aresample=async=1" -c:v copy output.mp4
Test without aresample to see if your player is tolerant of large gaps in the audio stream.

Merge 2 Files (audio and Video), with BITC and watermark in FFMPEG

I need to write ffmpeg profile to merge to merge video and audio files, and swap audio in video file from audio file, add BITC , and implement watermark from network location.
Can do it separately, but as I`m not FFMPEG expert, hard for me to combine all of above together.
Any advise would be appreciate.
Best regards all
Use the overlay filter for the watermark and the drawtext filter for the burnt-in timecode:
ffmpeg -i video.mp4 -i audio.mp3 -i watermark.png -filter_complex "[0:v:0]drawtext=fontfile=/usr/share/fonts/TTF/DejaVuSansMono.ttf:timecode='01\:23\:45\:00':r=25:x=(w-text_w)/2:y=h-text_h-20:fontsize=20:fontcolor=white:box=1:boxborderw=4:boxcolor=black[bg];[1][bg]overlay=W-w-10:H-h-12:format=auto[v]" -map "[v]" -map 1:a -shortest output.mp4

Merging 2 audios files with a video in ffmpeg

I'm trying to merge 2 audio files (.wav with video file .mp4) to produce the output in video.mp4 extension using ffmpeg on Windows CMD.
This is what I have tried :
ffmpeg -i V.MP4 -i A1.WAV -i A2.WAV -map 0:v -map 1:a -map 2:a -c:v copy -vcodec copy Output.MP4
The CMD output for that is :
This command will output the Output.mp4 file but it will merge the video with only 1 of the audio files which is A2.wav and ignores the other audio file A1.wav
I want to merge without re-encoding or any offset, just merging the 3 files as they are in a file one .mp4 file.
I checked some topics here in stackoverflow like this and this but they didn't help.
In order to mixdown the audio, you should use the amix filter
ffmpeg -i V.MP4 -i A1.WAV -i A2.WAV -filter_complex "[1][2]amix=inputs=2[a]" -map 0:v -map "[a]" -c:v copy Output.MP4

Singler line FFMPEG cmd to Merge Video /Audio and retain both audios

I have a project that requires merging of a video file with another audio file. The expected out put is an video file that will have both the audio from actual video and the merged audio file. The length of the output video file will be same to the size of the actual video file.
Is there a single line FFMPEG command to achieve this using copy and -map parameters ?
The video form I will be using is either flv or mp4
And the audio file format will be mp3
There can be achieved without using map also.
ffmpeg -i video.mp4 -i audio.mp3 output.mp4
In case you want the output.mp4 to stop as soon as one of the input stops (audio/video)
then use
-shortest
For example: ffmpeg -i video.mp4 -i audio.mp3 -shortest output.mp4
This will make sure that the output stops as and when any one of the inputs is completed.
Since you have asked that you want to do it with map. this is how you do it:
ffmpeg -i video.mp4 -i audio.mp3 -map 0:0 -map 1:0 -shortest output.mp4
Now, since you want to retain the audio of the video file, consider you want to merge audio.mp3 and video.mp4. These are the steps:
Extract audio from the video.mp4
ffmpeg -i video.mp4 1.mp3
Merge both audio.mp3 and 1.mp3
ffmpeg -i audio.mp3 -i 1.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 audiofinal.mp3
Remove the audio from video.mp4 (this step is not required. but just to do it properly)
ffmpeg -i video.mp4 -an videofinal.mp4
Now merge audiofinal.mp3 and videofinal.mp4
ffmpeg -i videofinal.mp4 -i audiofinal.mp3 -shortest final.mp4
note: in the latest version of ffmpeg it will only prompt you to use '-strict -2' in case it does then use this:
ffmpeg -i videofinal.mp4 -i audiofinal.mp3 -shortest -strict -2 final.mp4
hope this helps.
You can not do that using one cmd.
1. Get the audio from video file, the audio file name is a.mp3
ffmpeg.exe -i video.mp4 a.mp3
2. Merge two audio files(audio.mp3+a.mp3=audiofinal.mp3)
ffmpeg.exe -i audio.mp3 -i a.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 audiofinal.mp3
3. Merge video file and audio file(video.mp4+audiofinal.mp3=output.mp4)
ffmpeg.exe -i video.mp4 -i audiofinal.mp3 -map 0:v -map 1:a -c copy -y output.mp4
I don't think extracting the audio from the video is necessary. We can just use -filter_complex amix to merge both audios:
ffmpeg -i videowithaudio.mp4 -i audiotooverlay.mp3 -filter_complex amix -map 0:v -map 0:a -map 1:a -shortest videowithbothaudios.mp4
-filter_complex amix overlays the audio from the first input file on top of audio in the second input file.
-map 0:v video stream of the first input file.
-map 0:a audio stream of the first input file.
-map 1:a audio stream of the second input file.
-shortest the length of the output is the length of the shortest input
Use case:
add music to your background
you rendered a video, but muted some part of it, so you don't want to render it again(coz it's too long), instead you render only audio track(fast) and wanna merge it with original video.
Assuming
you have your video with you speech (or just audio track, whatever)
your music_file is not loud. Otherwise, you will not hear yourself D:
Steps:
1) Extract audio from the video
ffmpeg -i test.mp4 1.mp3
test.mp4 - your file
2) Merge both audio.mp3 and 1.mp3
ffmpeg -i audio.mp3 -i 1.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 audiofinal.mp3
audiofinal.mp3 - audio with music
3) Delete audio from original
ffmpeg -i example.mkv -c copy -an example-nosound.mkv
example-nosound.mkv - your video without audio
4) Merge with proper audio
ffmpeg -i audiofinal.mp3 -i example-nosound.wmv -c:v copy -vcodec copy final.wmv
final.wmv - your perfect video.
This is very easy with FFmpeg:
ffmpeg -i vid.mp4 -i audio.mp3 -codec:a libmp3lame -ar 44100 -ab 64k -ac 1 -q:v 1 -pix_fmt yuv420p -map 0:0 -map 1:0
First remove the sound from video if you are not able to merge video and audio by using this command:
ffmpeg -i video.mp4 -an videofinal.mp4

Resources