0

Okay, I have a media composition and have added to it 3 media clips...

MediaComposition composition = new MediaComposition();

MediaClip clip1 = await MediaClip.CreateFromFileAsync(file);
MediaClip clip2 = await MediaClip.CreateFromFileAsync(file);
MediaClip clip3 = await MediaClip.CreateFromFileAsync(file);

composition.Clips.Add(clip1);
composition.Clips.Add(clip2;
composition.Clips.Add(clip3);

Now I want each clip to display a distinct caption and to accomplish this I have written a custom video effect that uses the DrawTextLayout method of the Win2D API. Let me be clear, I don't want to do this with overlays! But since video effects are built using the factory design pattern, how would I go about loading different data on each instance of the video effect?

Here is the core of MyVideoEffect:

public void ProcessFrame(ProcessVideoFrameContext context)
{
    using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(canvasDevice, context.InputFrame.Direct3DSurface))
    using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(canvasDevice, context.OutputFrame.Direct3DSurface))
    using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession())
    {
    CanvasTextFormat textFormat = new CanvasTextFormat();
    CanvasTextLayout textLayout = new CanvasTextLayout(canvasDevice, text, textFormat, width, height);
            ds.DrawTextLayout(someTextLayout, x, y, Colors.Yellow);       
    }
}

Here, the "text" parameter should have a different value each time.

How can I specify the unique value when adding the video effect?

composition.Clips[0].VideoEffectDefinitions.Add(new VideoEffectDefinition(typeof(MyVideoEffect).FullName));
composition.Clips[1].VideoEffectDefinitions.Add(new VideoEffectDefinition(typeof(MyVideoEffect).FullName));
composition.Clips[2].VideoEffectDefinitions.Add(new VideoEffectDefinition(typeof(MyVideoEffect).FullName));

Is there an elegant way of doing this hopefully without having to resort to timers?

  • 1
    So is it possible for you to create different Vidoe effect for different clips instead of using the same one? – Roy Li - MSFT Sep 22 '21 at 08:28
  • It's doable. Instead of creating dozens of different video effects, I would rather do it programmatically. I guess I have to look into dynamic class creation... – M Psyllakis Sep 23 '21 at 10:56
  • 1
    Looks like you could send a parameter into the custom effect: [code](https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/custom-video-effects#add-your-custom-effect-to-a-clip-in-a-mediacomposition). Does it work for you? – Ax1le Sep 24 '21 at 08:17
  • Yes and no, thank you for bringing it to my attention though! I was hoping to pass a List as I wanted the video clip to iterate through the list and show a different string (Sentence) every X frames, but it can only accept primitives, and in that sense, is no different than the ApplicationData.Current.LocalSettings container. So I guess I'll have to break down a more complex data structure to a string. – M Psyllakis Sep 24 '21 at 12:52
  • 1
    So now you are working on putting simple strings as parameters to the Video effect class, right? Actually, @Anonymous's suggestion did work for you. – Roy Li - MSFT Sep 27 '21 at 06:27

0 Answers0