Adding background music that fades out with ffmpeg - 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.

Related

Render video till video duration with filters

In the below example, I need to render video with video time duration(i.e mp4)But this causes video execution to an infinite loop.
ffmpeg -i v.mp4 -stream_loop -1 -i w.webm -vcodec libvpx-vp9 -filter_complex overlay -y output.mp4
It should also work with vice versa
ffmpeg -stream_loop -1 -i w.webm -vcodec libvpx-vp9 -i v.mp4 -filter_complex overlay -y output.mp4
For the above command, the output video should be rendered till mp4 time duration.
Please help me.
Use overlay=shortest=1
See the overlay filter documentation for more info.

ffmpeg, add static image to beginning and end with transitions

ffmpeg noob here, trying to help my mother with some videos for real estate walkthroughs. I'd like to set up a simple pipeline that I can run videos through and have outputted as such:
5 second (silent) title card ->
xfade transition ->
property walk through ->
xfade transition ->
5 second (silent) title card
Considerations:
The intro / outro card will be the same content.
The input walkthrough videos will be of variable length so, if possible, a dynamic solution accounting for this would be ideal. If this requires me to script something using ffprobe, I can do that - just need to gain an understanding of the syntax and order of operations.
The video clip will come in with some audio already overlaid. I would like for the title cards to be silent, and have the video/audio clip fade in/out together.
I have gotten a sample working without the transitions:
ffmpeg -loop 1 -t 5 -i title_card.jpg \
-i walkthrough.MOV \
-f lavfi -t 0.1 -i anullsrc \
-filter_complex "[0][2][1:v][1:a][0][2]concat=n=3:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" \
-vcodec libx265 \
-crf 18 \
-vsync 2 \
output_without_transitions.mp4
I have been unable to get it to work with transitions. See below for the latest iteration:
ffmpeg -loop 1 -t 5 -r 60 -i title_card.jpg \
-r 60 -i walkthrough.MOV \
-f lavfi -t 0.1 -i anullsrc \
-filter_complex \
"[0][1:v]xfade=transition=fade:duration=0.5:offset=4.5[v01]; \
[v01][0]xfade=transition=fade:duration=0.5:offset=12.8[v]" \
-map "[v]" \
-vcodec libx265 \
-crf 18 \
-vsync 2 \
output_with_transitions.mp4
This half-works, resulting in the initial title card, fading into the video, but the second title card never occurs. Note, I also removed any references to audio, in an effort to get the transitions alone to work.
I have been beating my head against the wall on this, so help would be appreciated :)
Assuming walkthrough.MOV is 10 seconds long:
ffmpeg -loop 1 -t 5 -framerate 30 -i title_card.jpg -i walkthrough.MOV -filter_complex "[0]settb=AVTB,split[begin][end];[1:v]settb=AVTB[main];[begin][main]xfade=transition=fade:duration=1:offset=4[xf];[xf][end]xfade=transition=fade:duration=1:offset=13,format=yuv420p[v];[1:a]adelay=4s:all=1,afade=t=in:start_time=4:duration=1,afade=t=out:start_time=13:duration=1,apad=pad_dur=4[a]" -map "[v]" -map "[a]" -c:v libx265 -crf 18 -movflags +faststart output.mp4
You will need to upgrade your ffmpeg for this to work. The current release version (4.3 as of this answer) is too old, so get a build from the git master branch. See FFmpeg Download for links to builds for your OS, or see FFmpeg Wiki: Compile Guide.
title_card.jpg frame rate, width, and height must match walkthrough.MOV.
See Merging multiple video files with ffmpeg and xfade filter to see how to calculate xfade and afade offsets.
See FFmpeg Filter documentation for details on each filter.
See How to get video duration in seconds? which can help you automate this via scripting.
apad is supposed to automatically work with -shortest, but it doesn't with -filter_complex. So pad_dur is used to add the additional silence to the last title image, but whole_dur can be used instead if that is easier for you. Another method is to use anullsrc as in your question, then concatenate audio only with the concat filter, but I wanted to show adelay+apad as a viable alternative.

How to apply multiple cropped blurs?

I would like to apply multiple blurs into my video (with audio copied), each of them having different coordinates and durations. Here is what I have tried:
ffmpeg -i test.mp4 -filter_complex \
"[0:v]crop=w=100:h=100:x=20:y=40,boxblur=10:enable='between(t,5,8)'[c1];
[0:v]crop=w=100:h=100:x=40:y=60,boxblur=10:enable='between(t,10,13)'[c2];
[0:v][c1]overlay=x=20:y=40[v];
[0:v][c2]overlay=x=40:y=60[v]" \
-map "[v]" -movflags +faststart output.mp4
However, this results in a Filter overlay has an unconnected output error. I would like to know if there is any good way to solve this. Thanks for your attention.
The 2nd overlay should use the output of the first overlay as its base input.
ffmpeg -i test.mp4 -filter_complex \
"[0:v]crop=w=100:h=100:x=20:y=40,boxblur=10:enable='between(t,5,8)'[c1];
[0:v]crop=w=100:h=100:x=40:y=60,boxblur=10:enable='between(t,10,13)'[c2];
[0:v][c1]overlay=x=20:y=40:enable='between(t,5,8)'[v0];
[v0][c2]overlay=x=40:y=60:enable='between(t,10,13)'[v]" \
-map "[v]" -movflags +faststart output.mp4

audio is out of sync after concat 2 files

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.

chromakey: ffmpeg green color video removal

I am using ffmpeg on Mac to remove green background of video and add other background as image it is converting properly the video but auido is missing. I am using below command.
Am I doing anything wrong here?
ffmpeg -i bg.jpg -i input.mp4 -filter_complex "[1:v]colorkey=0x3BBD1E:0.3:0.2[ckout];[0:v][ckout]overlay[out]" -map "[out]" output.mp4
I solved it my own.
ffmpeg -i bg.jpg -i input.mp4 -filter_complex "[1:v]chromakey=0x3BBD1E:0.1:0.2[ckout];[0:v][ckout]overlay[o]" -map [o] -map 1:a output.mp4
we need to add flags for audio.
Once a map switch is added, only mapped streams are included. Add -map 1:a? -c:a copy

Resources