audio is out of sync after concat 2 files - ffmpeg

i want to show a video, that has 2 movies side by side. each movie is a result of one movie that concat 2 times. the problem is that the audio in the right side is out of sync.
i have 3 commands:
first command concat one movie 2 times
ffmpeg -i 1.mp4 -i 1.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[concatv][concata]" -map "[concatv]" -map "[concata]" Concat1.mp4
second command concat the second movie 2 times
ffmpeg -i 2.mp4 -i 2.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[concatv][concata]" -map "[concatv]" -map "[concata]" Concat2.mp4
at this point the audio is always synced.
the third command takes the 2 results and merge it to one movie side by side with a logo:
ffmpeg -i Concat1.mp4 -i Concat2.mp4 -i logo.png -filter_complex "[0:v]pad=width=iw+20:height=ih+20:x=10:y=10:color=black[a];[1:v]pad=width=iw+20:height=ih+20:x=10:y=10:color=black[b];nullsrc=size=640x480[base];[a]setpts=PTS-STARTPTS, scale=320x480[left];[b]setpts=PTS-STARTPTS, scale=320x480[right];[base][left]overlay=shortest=1[tmp1];[tmp1][right] overlay=320:0[video];[0:a]apad [apa];[apa][1:a]amerge=inputs=2,pan=stereo|FL<c0+c1|FR<c0+c1[audio];[2:v]scale=120:44 [ovrl];[video][ovrl]overlay=15:25[videoandlogo]" -map "[videoandlogo]" -map "[audio]" output.mp4
the problem is that in the output file the audio of the right movie is out of sync in its second time (the second concat). it always the right movie that is out of sync, even when i switch them.
can anyone help?
Thanks.
==============EDIT===================
the problem of the sync has solved by adding apad also to the second file, but after that the command never stops... how can i tell the command to stop when both movies ended if i have apad for both movies?
this is the new command that never stops:
ffmpeg -i 1.mp4 -i 2.mp4 -i logo.png -filter_complex "[0:v]pad=width=iw+20:height=ih+20:x=10:y=10:color=black[a];[1:v]pad=width=iw+20:height=ih+20:x=10:y=10:color=black[b];nullsrc=size=640x480[base];[a]scale=320x480[left];[b]scale=320x480[right];[base][left]overlay=shortest=1[tmp1];[tmp1][right] overlay=320:0[video];[1:a]apad [apa];[0:a]apad[apa1];[apa][apa1]amix=inputs=2:duration=longest[audio];[2:v]scale=120:44 [ovrl];[video][ovrl]overlay=15:25[videoandlogo]" -map "[videoandlogo]" -map "[audio]" output.mp4

Found the answer (hope it will help someone).
for the audio problem, i solved it by putting apad to all of the movies (need to put the apad of the longest movie first). and to fix the never ending movie i put "-t ".
Thanks.

Related

Merge (video and audio) with (background music) with ffmpeg

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?

concatenate audio files with an image

I am trying to concatenate multiple audio files and a single image into one video file using one command.
I have list of mp3 files and a playlist file (.m3u) in a direcotry.
I managed to do this but my solution is bad:
reading the playlist file and creating a new .txt in the ffmpeg required format
concatenating the audio files using the .txt into an .mp3
concatenating the large audio file and the static image into a video
This creates 2 unnecessary files that I have to delete.
I tried a different command
ffmpeg -loop 1 -framerate 1 -i myImage.jpg -i file1.mp3 -i file2.mp3 -i file3.mp3 -filter_complex '[0:0][1:0][2:0]concat=n=3:v=0:a=1' -tune stillimage -shortest output.mp4
however im getting a Error initializing complex filters.
Invalid argument error
Another kick in the nuts is that the system im working on has spaces in the folder names.
i tried using -i "concat:file1.mp3|file2.mp3|..." however i cannot use double quote marks to quote out the path so I get an invalid argument error.
Thank you very much for your help.
Method 1: concat demuxer
Make input.txt containing the following:
file 'file1.mp3'
file 'file2.mp3'
file 'file3.mp3'
Run ffmpeg:
ffmpeg -loop 1 -framerate 1 -i myImage.jpg -f concat -i input.txt -filter_complex "[0]scale='iw-mod(iw,2)':'ih-mod(ih,2)',format=yuv420p[v]" -map "[v]" -r 15 -tune stillimage -map 1:a -shortest -movflags +faststart output.mp4
All MP3 files being input to the concat demuxer must have the same channel layout and sample rate. If they do not then convert them using the -ac and -ar options so they are all the same.
Method 2: concat filter
Update: There seems to be a bug with -shortest not working with the concat filter (I keep forgetting about that). See the method above using the concat demuxer, or replace -shortest with -t. The value for -t should equal the total duration of all three MP3 files.
ffmpeg -loop 1 -framerate 1 -i myImage.jpg -i file1.mp3 -i file2.mp3 -i file3.mp3 -filter_complex "[0]scale='iw-mod(iw,2)':'ih-mod(ih,2)',format=yuv420p[v];[1:a][2:a][3:a]concat=n=3:v=0:a=1[a]" -map "[v]" -r 15 -map "[a]" -tune stillimage -shortest -movflags +faststart output.mp4
Option descriptions
scale filter makes image have even width and height which is required when outputting YUV 4:2:0 with libx264.
format filter sets chroma subsampling to 4:2:0, otherwise libx264 will try to limit subsampling, but most players can only handle 4:2:0.
concat filter is accepting file1.mp3, file2.mp3, and file3.mp3 as inputs. Your original command was trying to concat the video to the audio resulting in Invalid argument.
-map "[v]" chooses the video output from -filter_complex.
-r 15 sets output frame rate to 15 because most players can't handle 1 fps. This is faster than setting -framerate 15.
-map "[a]" chooses the audio output from -filter_complex.
-map 1:a chooses the audio from input #1 (the second input as counting starts from 0).
-movflags +faststart after encoding finishes this option moves some data from the end of the MP4 output file to the beginning. This allows playback to begin faster otherwise the complete file will have to be downloaded first.

Adding background music that fades out with ffmpeg

I have a piece of video that is 30fps and simply want to add overlay faint background music starting at 10 seconds. Then I want it to fade out during the last 15 seconds. I can't find any good examples of this other than weird hacks.
ffmpeg -i main.mp4 -i outro.mp3 -t 10 \
-filter_complex "[a0][1:a]acrossfade=d=15[a];weight1:0.5" \
-map '[v]' -map '[a]' out.mp4
I get bad arguments in the filter_complex. Somethings off right there. in terms of the duration. Any help is greatly appreciated!!
Use
ffmpeg -i main.mp4 -i outro.mp3 \
-filter_complex "[0:a]volume=0,asplit[0a][0acf];[1:a]adelay=10s|10s[1a];\
[0a][1a]amix=inputs=2:duration=first:dropout_transition=0,volume=2,afifo[outro];\
[0acf]atrim=0:16,afifo[0acf];[outro][0acf]acrossfade=d=15[outro];\
[0:a][outro]amix=duration=first:weights='2 1'[a]" \
-map 0:v -c:v copy -map '[a]' out.mp4
The main audio is muted and then cloned to two copies. It is then mixed with the delayed music so that the music duration matches the main audio. It is then crossfaded with the other copy of the muted main audio. This result is then mixed with the original main audio.

Video file is hanging after concatenating video files and drawtext to output

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.

Stack input streams and stagger in ffmpeg

I have 3 input videos I want to stack vertically. I want the 3rd to play immediately and have the second start when the 3rd finishes. The command I currently have working is below:
ffmpeg -i test.mp4 -i test2.mp4 -i test3.mp4 -filter_complex "[0:v]scale=320:240[top];[1:v]scale=320:240[mid];[2]scale=320:240[bot];[top][mid][bot]vstack=inputs=3[v]" -map "[v]" -map 1:a -t 60 out.mp4
This works, but all 3 start immediately and only the audio from clip #2 is mapped. I'd like to tell ffmpeg to copy both audio streams but stagger the clips so that clip 3 plays, then clip 2 plays, both with their respective audio. Is this something that ffmpeg filters can do?

Resources