Skip to content

Instantly share code, notes, and snippets.

@Casey-Bateman
Created July 28, 2014 15:51
Show Gist options
  • Save Casey-Bateman/5d8e9282f82d5d760f32 to your computer and use it in GitHub Desktop.
Save Casey-Bateman/5d8e9282f82d5d760f32 to your computer and use it in GitHub Desktop.
Crossfade Concat explanation
using Hudl.FFmpeg.Command;
using Hudl.FFmpeg.Common;
using Hudl.FFmpeg.Filters.BaseTypes;
using Hudl.FFmpeg.Metadata;
using Hudl.FFmpeg.Resources.BaseTypes;
using Hudl.FFmpeg.Sugar;
...
//below are a few constants and assumptions that can be made
// command: the FFmpeg command object.
// streamFrom: the video stream we will be fading from.
// streamTo: the video stream we will be fading to.
// crossfadeDuration: Timespan.FromSeconds(1)
// crossfadeExpression: "A*(if(gte(T,{0}),1,T/{0}))+B*(1-(if(gte(T,{0}),1,T/{0})))"
//first we get the current info for the stream, so we know what we are working with.
var streamFromMetadata = MetadataHelpers.GetMetadataInfo(command, streamFrom);
var streamFromMinusDuration = streamFromMetadata.VideoStream.Duration - crossfadeDuration;
//we must split the from and to streams to create enough copies to work with.
var fromSplit = command.Select(streamFrom)
.Filter(Filterchain.FilterTo<VideoStream>(new Split(2)));
var toSplit = command.Select(streamTo)
.Filter(Filterchain.FilterTo<VideoStream>(new Split(2)));
//now we take one of copy of each stream, and trim out the crossfaded portion
var fromMain = fromSplit.Take(0)
.Filter(new TrimVideo(null, streamFromMinusDuration, VideoUnitType.Seconds));
var toMain = toSplit.Take(0)
.Filter(new TrimVideo(crossfadeDuration, null, VideoUnitType.Seconds));
//this next part is the actual crossfaded video, we trim to just the crossfaded portion
var fromBlend = fromSplit.Take(1)
.Filter(new TrimVideo(streamFromMinusDuration, null, VideoUnitType.Seconds));
var toBlend = toSplit.Take(1)
.Filter(new TrimVideo(null, crossfadeDuration, VideoUnitType.Seconds));
//this step we would actually perform a blend of the two portions of video from above
var crossfade = command.Select(fromBlend.StreamIdentifiers)
.Select(toBlend.StreamIdentifiers)
.Filter(Filterchain.FilterTo<VideoStream>(new Blend(crossfadeExpression)));
//the final step would be to concatenate them all together.
var result = command.Select(fromMain.StreamIdentifiers)
.Select(blendOut.StreamIdentifiers)
.Select(toMain.StreamIdentifiers)
.Filter(Filterchain.FilterTo<VideoStream>(new Concat()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment