<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gavin Charlesworth</title>
    <description>The latest articles on DEV Community by Gavin Charlesworth (@gavinjc).</description>
    <link>https://dev.to/gavinjc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4279685%2F0adc35cd-1d17-4695-b1df-59611af3bc26.png</url>
      <title>DEV Community: Gavin Charlesworth</title>
      <link>https://dev.to/gavinjc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gavinjc"/>
    <language>en</language>
    <item>
      <title>MediaComposition can't do transitions</title>
      <dc:creator>Gavin Charlesworth</dc:creator>
      <pubDate>Thu, 03 Dec 2026 05:46:13 +0000</pubDate>
      <link>https://dev.to/gavinjc/mediacomposition-cant-do-transitions-17o7</link>
      <guid>https://dev.to/gavinjc/mediacomposition-cant-do-transitions-17o7</guid>
      <description>&lt;p&gt;&lt;em&gt;Developed and tested on Windows 11 Pro 26H1 (build 28000.3501) with Win2D 1.2.0.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;I wanted a crossfade.&lt;/p&gt;

&lt;p&gt;Not a complicated one. Two photographs, one second, the first dissolving into the second —&lt;br&gt;
the single most common transition in the history of slideshows. I was building a Windows app&lt;br&gt;
that turns a folder of photos and an MP3 into a video, and Windows ships an API that composes&lt;br&gt;
video from clips, applies effects, mixes in background audio and renders the result to MP4.&lt;br&gt;
&lt;code&gt;Windows.Media.Editing.MediaComposition&lt;/code&gt;. Built in since Windows 10, no dependencies, uses the&lt;br&gt;
OS encoder.&lt;/p&gt;

&lt;p&gt;It cannot crossfade. Not "doesn't yet" — cannot, by construction. It took me a while to be sure&lt;br&gt;
of that, because nothing says so anywhere, so here is the write-up I wanted to find.&lt;/p&gt;
&lt;h2&gt;
  
  
  What MediaComposition is good at
&lt;/h2&gt;

&lt;p&gt;Credit first, because it is genuinely useful. You build a &lt;code&gt;MediaComposition&lt;/code&gt;, add &lt;code&gt;MediaClip&lt;/code&gt;s —&lt;br&gt;
from video files, from still images with a duration, from a flat colour — add&lt;br&gt;
&lt;code&gt;BackgroundAudioTrack&lt;/code&gt;s, and call &lt;code&gt;RenderToFileAsync&lt;/code&gt;. You can preview the whole thing through&lt;br&gt;
&lt;code&gt;GeneratePreviewMediaStreamSource&lt;/code&gt; into a &lt;code&gt;MediaPlayerElement&lt;/code&gt;. It is maybe forty lines to go&lt;br&gt;
from a list of files to a playable MP4, hardware-encoded, with no third-party dependency and no&lt;br&gt;
codec licensing to think about.&lt;/p&gt;

&lt;p&gt;For assembling clips end to end, it is the right tool and I still use it. Just not for this.&lt;/p&gt;
&lt;h2&gt;
  
  
  Three dead ends
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;IBasicVideoEffect&lt;/code&gt; processes one stream.&lt;/strong&gt; This is the documented extensibility point —&lt;br&gt;
implement the interface, add it to a clip's &lt;code&gt;VideoEffectDefinitions&lt;/code&gt;, and you get each frame to&lt;br&gt;
do what you like with. Except you get each frame &lt;em&gt;of that clip&lt;/em&gt;. A transition is, definitionally,&lt;br&gt;
two sources visible at once. There is no frame of clip B available while you are processing a&lt;br&gt;
frame of clip A, because clip B is not playing yet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;MediaComposition.Clips&lt;/code&gt; is strictly sequential.&lt;/strong&gt; Clips sit end to end on one timeline. There&lt;br&gt;
is no overlap, no track index, no offset that lets B start before A finishes. The collection is&lt;br&gt;
an ordered list, not a timeline with lanes. Whatever you were going to overlap, you cannot.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;MediaOverlay&lt;/code&gt; looks like the escape hatch, and isn't.&lt;/strong&gt; Overlays are the one place&lt;br&gt;
MediaComposition composites two things simultaneously: you create a &lt;code&gt;MediaOverlayLayer&lt;/code&gt;, add a&lt;br&gt;
&lt;code&gt;MediaOverlay&lt;/code&gt; wrapping a clip, and give it a &lt;code&gt;Delay&lt;/code&gt;, a &lt;code&gt;Position&lt;/code&gt; and an &lt;code&gt;Opacity&lt;/code&gt;. So —&lt;br&gt;
position clip B as an overlay starting one second before A ends, ramp its opacity from 0 to 1,&lt;br&gt;
and there's your crossfade.&lt;/p&gt;

&lt;p&gt;Except:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;Opacity&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a &lt;code&gt;double&lt;/code&gt;. One number, for the overlay's entire duration. There is no keyframe collection,&lt;br&gt;
no animation object, no setter taking a curve or a function of time. The type signature is the&lt;br&gt;
whole argument — a scalar cannot express a ramp. You can have an overlay at 50% opacity for a&lt;br&gt;
second. You cannot have one that goes from 0% to 100% across that second.&lt;/p&gt;
&lt;h2&gt;
  
  
  This isn't a missing feature
&lt;/h2&gt;

&lt;p&gt;That distinction matters, because it determines whether you wait or rewrite.&lt;/p&gt;

&lt;p&gt;A missing feature is something you work around, or ship without and add later. This is&lt;br&gt;
architectural: the composition model has no concept of two sources overlapping in time under&lt;br&gt;
author control. &lt;strong&gt;One crossfade is already enough to require a different engine.&lt;/strong&gt; There is no&lt;br&gt;
version of this where you get 80% of the way with MediaComposition and fill the gap.&lt;/p&gt;

&lt;p&gt;I spent longer than I should have looking for the clever combination that would work. There&lt;br&gt;
isn't one. If you are reading this because you are mid-search: stop, it's fine, go build the&lt;br&gt;
other thing.&lt;/p&gt;
&lt;h2&gt;
  
  
  The workaround to avoid
&lt;/h2&gt;

&lt;p&gt;The one approach that technically works with MediaComposition: render each transition yourself —&lt;br&gt;
somewhere, somehow — encode it as a short MP4, and splice those between your clips.&lt;/p&gt;

&lt;p&gt;It does produce the right picture. It also means hundreds of tiny encodes for a long slideshow,&lt;br&gt;
a temp file per join, and generation loss at every single one, because each spliced segment is a&lt;br&gt;
re-encode of already-encoded frames. You have taken on all the complexity of rendering frames&lt;br&gt;
yourself &lt;em&gt;and&lt;/em&gt; kept all the constraints of a model that can't help you.&lt;/p&gt;

&lt;p&gt;If you are going to render the frames anyway, render all of them.&lt;/p&gt;
&lt;h2&gt;
  
  
  What to build instead
&lt;/h2&gt;

&lt;p&gt;Own the frame pipeline. Composite each frame with Win2D, feed the frames to a&lt;br&gt;
&lt;code&gt;MediaStreamSource&lt;/code&gt;, and let &lt;code&gt;MediaTranscoder&lt;/code&gt; encode with the same OS encoder MediaComposition&lt;br&gt;
would have used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            timeline model
                  │
                  ▼
           FrameComposer          Win2D — given time t, produce one frame
                  │
       ┌──────────┴──────────┐
       ▼                     ▼
  live playback        MediaStreamSource
                             │
                             ▼
       MediaTranscoder.PrepareMediaStreamSourceTranscodeAsync
                             │
                             ▼
                            MP4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One dependency: &lt;code&gt;Microsoft.Graphics.Win2D&lt;/code&gt; (1.2.0 here), MIT, from Microsoft. Still no FFmpeg,&lt;br&gt;
still the OS encoder, still hardware-accelerated.&lt;/p&gt;
&lt;h3&gt;
  
  
  The idea that makes it tractable
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;FrameComposer.Render(session, project, t, …)&lt;/code&gt; is a &lt;strong&gt;pure function of the timeline&lt;/strong&gt;. Give it a&lt;br&gt;
time, it draws the frame at that time. It does not know whether it is being called by a player or&lt;br&gt;
an encoder.&lt;/p&gt;

&lt;p&gt;Everything good falls out of that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Preview and export are the same code with different clocks, so they &lt;strong&gt;cannot&lt;/strong&gt; drift apart.
This is the bug class you most want to eliminate in a renderer, and it's eliminated by
construction rather than by discipline.&lt;/li&gt;
&lt;li&gt;Scrubbing is free. Seek is just calling it with a different &lt;code&gt;t&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A new transition is one class and no engine change. Crossfade is two &lt;code&gt;DrawImage&lt;/code&gt; calls with an
alpha value.&lt;/li&gt;
&lt;li&gt;Pan and zoom is a source-rect transform, not a special case.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  The surface-handoff nobody documents
&lt;/h3&gt;

&lt;p&gt;Win2D's &lt;code&gt;CanvasRenderTarget&lt;/code&gt; &lt;strong&gt;casts directly to &lt;code&gt;IDirect3DSurface&lt;/code&gt;&lt;/strong&gt;. No interop helper, no&lt;br&gt;
&lt;code&gt;QueryInterface&lt;/code&gt; dance, no copy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;renderTarget&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CanvasRenderTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CanvasDrawingSession&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;renderTarget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateDrawingSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;FrameComposer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; …&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;surface&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IDirect3DSurface&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;renderTarget&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;MediaStreamSample&lt;/span&gt; &lt;span class="n"&gt;sample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MediaStreamSample&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateFromDirect3D11Surface&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;surface&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;sample&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FrameDuration&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That one line is most of why this approach is viable at all — the frame never leaves the GPU on&lt;br&gt;
its way to the encoder.&lt;/p&gt;
&lt;h3&gt;
  
  
  Supplying the frames
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;MediaEncodingProfile&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MediaEncodingProfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateMp4&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Audio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                       &lt;span class="c1"&gt;// video-only; audio is a second pass&lt;/span&gt;

&lt;span class="c1"&gt;// Derive the render size FROM the profile rather than computing it independently.&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Width&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Height&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;videoProperties&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;VideoEncodingProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateUncompressed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;MediaEncodingSubtypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bgra8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;streamSource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MediaStreamSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;VideoStreamDescriptor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;videoProperties&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;BufferTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TimeSpan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;IsLive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;streamSource&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SampleRequested&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;MediaStreamSourceSampleRequest&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;MediaStreamSourceSampleRequestDeferral&lt;/span&gt; &lt;span class="n"&gt;deferral&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetDeferral&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;frameGate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WaitAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cancellationToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nextTimestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                      &lt;span class="c1"&gt;// leaving Sample unset signals end-of-stream&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// … decode window, render, build sample …&lt;/span&gt;
            &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Sample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sample&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;finally&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;frameGate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Release&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;deferral&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cancellationToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;transcoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MediaTranscoder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;HardwareAccelerationEnabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;PrepareTranscodeResult&lt;/span&gt; &lt;span class="n"&gt;prepared&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;transcoder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PrepareMediaStreamSourceTranscodeAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;streamSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;prepared&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CanTranscode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InvalidOperationException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Cannot transcode: {prepared.FailureReason}."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;prepared&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TranscodeAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Five things in there that each cost me time:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Derive the dimensions from the encoding profile.&lt;/strong&gt; Do not compute the render size independently&lt;br&gt;
and hope it matches. A mismatch between your &lt;code&gt;VideoEncodingProperties&lt;/code&gt; and the profile's own&lt;br&gt;
&lt;code&gt;Video.Width&lt;/code&gt;/&lt;code&gt;Height&lt;/code&gt; surfaces as &lt;code&gt;CanTranscode == false&lt;/code&gt; with a &lt;code&gt;FailureReason&lt;/code&gt; that does not&lt;br&gt;
obviously point at it. Letting the profile decide also sidesteps the separate rule that some&lt;br&gt;
hardware encoders reject odd dimensions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Always check &lt;code&gt;CanTranscode&lt;/code&gt; before &lt;code&gt;TranscodeAsync&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;FailureReason&lt;/code&gt; is the only thing that&lt;br&gt;
will tell you what's wrong, and skipping the check gets you a failure much further downstream.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;SampleRequested&lt;/code&gt; can fire concurrently.&lt;/strong&gt; It is not serialised for you. Anything shared — a&lt;br&gt;
timestamp counter, a decode window, a cache — needs a gate. And &lt;strong&gt;both the gate and the deferral&lt;br&gt;
must be released on every exit path&lt;/strong&gt;, including the early return at end-of-stream. Miss one and&lt;br&gt;
the next frame request waits forever on a gate nothing will give back; the render just stops, with&lt;br&gt;
no exception and no error. That one cost me an evening.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Leaving &lt;code&gt;request.Sample&lt;/code&gt; unset is how you signal end-of-stream.&lt;/strong&gt; There's no explicit "done"&lt;br&gt;
call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't dispose the &lt;code&gt;CanvasRenderTarget&lt;/code&gt; after building the sample.&lt;/strong&gt; The transcoder reads the&lt;br&gt;
underlying Direct3D surface &lt;em&gt;asynchronously&lt;/em&gt;, after your handler has returned. Disposing eagerly&lt;br&gt;
is a use-after-dispose on the GPU side. Leaving it to the GC works, but it's the part of this I'd&lt;br&gt;
profile hardest on a long render.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You do not need the Graphics Capture capability.&lt;/strong&gt; Every sample of this pattern is about screen&lt;br&gt;
recording and declares it. We are synthesising frames, not capturing a screen — so declaring it&lt;br&gt;
would be requesting a permission you never use, which matters if, like this app, your whole pitch&lt;br&gt;
is that it declares no capabilities it doesn't need.&lt;/p&gt;
&lt;h3&gt;
  
  
  Where MediaComposition still wins
&lt;/h3&gt;

&lt;p&gt;Audio. Rendering video-only and then handing that MP4 back to MediaComposition as a single clip,&lt;br&gt;
with &lt;code&gt;BackgroundAudioTrack&lt;/code&gt;s attached, gets you mixing, looping-to-fill and muxing for free. It&lt;br&gt;
costs one re-encode of the video, which you can remove later by carrying an audio descriptor on&lt;br&gt;
the same &lt;code&gt;MediaStreamSource&lt;/code&gt; — but it's the right first version, and it means the API still earns&lt;br&gt;
its place in the project.&lt;/p&gt;

&lt;p&gt;It just isn't the renderer.&lt;/p&gt;
&lt;h2&gt;
  
  
  The bug that actually cost me a day
&lt;/h2&gt;

&lt;p&gt;Not the architecture. This:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Win2D and WIC both silently apply EXIF orientation on decode.&lt;/strong&gt; &lt;code&gt;CanvasBitmap.LoadAsync&lt;/code&gt;&lt;br&gt;
pre-rotates pixel data to match the file's EXIF tag, by default, without saying so — and contrary&lt;br&gt;
to what its own documentation implies.&lt;/p&gt;

&lt;p&gt;My pipeline also applied its own rotation, detected separately at import and applied by the&lt;br&gt;
composer on every frame. So every EXIF-tagged photograph was rotated twice.&lt;/p&gt;

&lt;p&gt;What found it: rendering a synthetic 90°-tagged test image and &lt;em&gt;looking at the output&lt;/em&gt;. It came&lt;br&gt;
out 180° rotated, not 90°. Confirmed by measuring &lt;code&gt;CanvasBitmap&lt;/code&gt;'s reported &lt;code&gt;Size&lt;/code&gt; against a&lt;br&gt;
known-tagged file — the decoder had already swapped width and height before I touched it.&lt;/p&gt;

&lt;p&gt;The fix is to decode through &lt;code&gt;BitmapDecoder.GetSoftwareBitmapAsync&lt;/code&gt; with&lt;br&gt;
&lt;code&gt;ExifOrientationMode.IgnoreExifOrientation&lt;/code&gt;, forced to &lt;code&gt;Bgra8&lt;/code&gt;/&lt;code&gt;Premultiplied&lt;/code&gt; for Win2D interop,&lt;br&gt;
so that the explicit rotation value stays the single source of truth:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;SoftwareBitmap&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;decoder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetSoftwareBitmapAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;BitmapPixelFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bgra8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;BitmapAlphaMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Premultiplied&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BitmapTransform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;ExifOrientationMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IgnoreExifOrientation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;ColorManagementMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DoNotColorManage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;CanvasBitmap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateFromSoftwareBitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resourceCreator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Twelve unit tests passed throughout, because not one of them decoded a real file.&lt;/p&gt;

&lt;p&gt;That's the second time this project taught me the same lesson — the first was the deferral&lt;br&gt;
deadlock above — and it's worth stating plainly: &lt;strong&gt;for a render pipeline, &lt;code&gt;dotnet test&lt;/code&gt; passing&lt;br&gt;
is necessary and nowhere near sufficient. Render real output and look at it.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;MediaComposition&lt;/code&gt; cannot produce a transition. The clip model has no overlap,
&lt;code&gt;IBasicVideoEffect&lt;/code&gt; sees one stream, and &lt;code&gt;MediaOverlay.Opacity&lt;/code&gt; is a &lt;code&gt;double&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;One crossfade is enough to require owning the frame pipeline. There's no partial path.&lt;/li&gt;
&lt;li&gt;Win2D → &lt;code&gt;MediaStreamSource&lt;/code&gt; → &lt;code&gt;MediaTranscoder&lt;/code&gt; gets you there with one MIT dependency and the
same OS encoder. &lt;code&gt;CanvasRenderTarget&lt;/code&gt; casts straight to &lt;code&gt;IDirect3DSurface&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Make your frame function pure in &lt;code&gt;t&lt;/code&gt; and preview-versus-export drift stops being a bug class.&lt;/li&gt;
&lt;li&gt;Take the render size from the encoding profile, check &lt;code&gt;CanTranscode&lt;/code&gt;, release the deferral on
every path, and don't dispose the render target.&lt;/li&gt;
&lt;li&gt;Decode with EXIF orientation explicitly ignored, and look at your output.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;This pipeline renders &lt;a href="https://apps.microsoft.com/detail/9PLKC14MGC3N" rel="noopener noreferrer"&gt;ClickTo: Slideshow&lt;/a&gt;, a&lt;br&gt;
free Windows app that turns photos and music into a video or a PowerPoint. The design decisions&lt;br&gt;
behind it, including the alternatives I rejected, are in its&lt;br&gt;
&lt;a href="https://github.com/The-Bureau-of-Stuff/ClickToSlideshow/blob/master/docs/clickto-slideshow-decisions.md" rel="noopener noreferrer"&gt;decision record&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>microsoft</category>
      <category>winui</category>
    </item>
  </channel>
</rss>
