[FFmpeg-user] Cross fading of videos

Tony Mobily merc at mobily1.com
Tue Dec 15 06:43:19 CET 2015


Hi Ryan,

Well, now I to know FFMpeg!
But I have to say, this is what we ended up with. It's simple, it's
straightforward, and it just seems to work...


/tmp/ffmpeg -i segment2s.mp4 -i segment1s.mp4 -an \
-filter_complex \
"   [0:v]trim=start=0:end=9,setpts=PTS-STARTPTS[firstclip];
    [1:v]trim=start=1,setpts=PTS-STARTPTS[secondclip];
    [0:v]trim=start=9:end=10,setpts=PTS-STARTPTS[fadeoutsrc];
    [1:v]trim=start=0:end=1,setpts=PTS-STARTPTS[fadeinsrc];
    [fadeinsrc]format=pix_fmts=yuva420p,
                fade=t=in:st=0:d=1:alpha=1[fadein];
    [fadeoutsrc]format=pix_fmts=yuva420p,
                fade=t=out:st=0:d=1:alpha=1[fadeout];
    [fadein]fifo[fadeinfifo];
    [fadeout]fifo[fadeoutfifo];
    [fadeoutfifo][fadeinfifo]overlay[crossfade];
    [firstclip][crossfade][secondclip]concat=n=3[output];
    [0:a][1:a] acrossfade=d=1 [audio]
" \
-map "[output]" -map "[audio]" result.mp4

That was exactly my approach; I was doing my best to make it happen, doing
exactly that: get the first video minus the last second, get the second
video minus the initial second, get the two versions of the "middle
second", and then fade them together. And voila', you have a well faded
video. The audio is ridiculosly simple too.

As I was banging my head against the wall making this happen, Guido found
this post:

http://superuser.com/questions/1001039/what-is-an-efficient-way-to-do-a-video-crossfade-with-ffmpeg

All I had to do is fix the syntax a little, add the audio, and voila'!

Just in case you can use it for your project -- free of charge! :D

Thanks for your answer...

Merc.


On 11 December 2015 at 15:44, Ryan Williams <ryan at ryanwilliams.id.au> wrote:

> Please note that I haven't tested any of the following suggestions.
> I do however have dissolve with crossfade working a project of mine and I
> have based these suggestions off working code.
>
> 1) Dissolve (Assuming the resolution of both videos is 1280x720 and the
> audio format is identical)
>
> Notes:
> '+9/TB' represents (length of 1st video in seconds - length of dissolve in
> seconds)
> 'ns=43076’ represents (1 second at 44100 Hz sample rate - 1024 samples) to
> work around a bug I reported (which may've been fixed already).
> The reason for including the same files multiple times is a work around
> filters which "scrub" (trim, atrim, select etc) thus causing buffering
> problems during encoding.
>
> Visuals Explained:
> * Create a 1 second fade in from the start of the 2nd video to be the
> transition.
> * Overlay the transition onto the 1st video so that the end lines up.
> * Take the remainder of the 2nd video and concatenate it on the end.
>
> Audios Explained:
> * Cossfade the 2nd videos audio with the first with a 1 second overlap
> (Allowing for the 1024 sample bug I reported)
> * Truncate that new audio stream to be the same length as the first video
> * Take the remainder of the 2nd videos audio and append it to the end.
>
> The reason for making the audio in 2 parts is so that each part can be
> resynced with the visuals in the concat filter.
> If you attempt to conjure the visuals completely separate from the audio
> you may get lip sync issues during playback of the 2nd clip.
>
> ffmpeg \
>  -y \
>  -i "2.mp4" \
>  -i "1.mp4" \
>  -i "2.mp4" \
>  -i "1.mp4" \
>  -i "2.mp4" \
>  -i "2.mp4" \
>  -filter_complex " \
>    [0:a] anullsink; \
>    [0:v] trim='duration=1', format='pix_fmts=yuva420p',
> fade='alpha=1:d=1:t=in', setpts='PTS-STARTPTS+9/TB' [transition-v]; \
>
>    [1:v][transition-v] overlay='eval=init', setpts='PTS-STARTPTS'
> [scene-0]; \
>    [2:v] select='e=gte(t\, 1)', setpts='PTS-STARTPTS',
> format='pix_fmts=yuva420p' [scene-1]; \
>
>    [3:a][4:a] acrossfade='ns=43076', atrim='duration=10' [track-0]; \
>    [5:a] atrim='start=1', asetpts='PTS-STARTPTS' [track-1]; \
>
>    [scene-0][track-0] [scene-1][track-1] concat='n=2:v=1:a=1'
> [output-v][output-a] \
>  " \
>  -map '[output-a]' \
>  -strict experimental -acodec aac \
>  -map '[output-v]' \
>  -s 1280x720 \
>  -vcodec mpeg4 \
>  -pix_fmt +yuvj420p \
>  -q:v 0 \
>  "output.mp4"
>
>
> 2) Slicing
> ffmpeg -y -i 1.mp4 -filter_complex " \
>   [0:v] trim='start=10.45:end=23.54' [trim-v]; \
>   [0:a] atrim='start=10.45:end=23.54' [trim-a]; \
>   [trim-v][trim-a] concat='n=1:v=1:a=1' [output] \
> " \
> -map '[output]' \
> -strict experimental -acodec aac \
> -vcodec mpeg4 \
> -q:v 0 \
> output.mp4
>
>
> 3) Overwriting
> ffmpeg -y -i 1.mp4 -i 2.mp4 -filter_complex " \
>   [0:v] trim='duration=31.5' [trim-v]; \
>   [0:a] trim='duration=31.5' [trim-a]; \
>   [trim-v][trim-a][1:v][1:a] concat='n=2:v=1:a=1' [output] \
> " \
> -map '[output]' \
> -strict experimental -acodec aac \
> -vcodec mpeg4 \
> -q:v 0 \
> output.mp4
>
> -----Original Message-----
> From: ffmpeg-user [mailto:ffmpeg-user-bounces at ffmpeg.org] On Behalf Of
> Tony Mobily
> Sent: Friday, 11 December 2015 16:45
> To: ffmpeg-user at ffmpeg.org
> Subject: [FFmpeg-user] Cross fading of videos
>
> Hi,
>
> I am new to ffmpeg. I am reading through the documentation -- although
> there is a lot to take in!
> I was wondering if you could give me a couple of hints on how to get
> started.
>
> 1) Cross-dissolve
>
> I have two videos, 1.mp4 and 2.mp4, 10 seconds each. I want to concatenate
> those videos, so that the first one has a cross-dissolve with the second
> one when they are joined. If the effect goes on for 1 second, this
> necessarily means that the resulting video will necessarily be 19 seconds
> (since the last second of 1.mp4 will "blend" with the first second of 2.mp4.
> Is this something that can be done with ffmpeg?
>
> 2) Slicing
>
> I have a 60 second video, and want to get a slice from the second 10.45 to
> the second 23.54. Is that possible?
>
> 2) Overlay
> I have two videos, 1.mp4 (30 seconds) and 2.mp4 (8.5 seconds). I want to
> create a result video where 2.mp4 overwrites the last 8.5 seconds of 1.mp4.
> Is this possible without doing cut/concatenation?
>
> Thank you in advance -- and I apologise if I asked silly questions!
>
> Merc.
>> _______________________________________________
> ffmpeg-user mailing list
> ffmpeg-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
>
> _______________________________________________
> ffmpeg-user mailing list
> ffmpeg-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-user
>


More information about the ffmpeg-user mailing list