<?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: coderWenzi</title>
    <description>The latest articles on DEV Community by coderWenzi (@coderwenzi).</description>
    <link>https://dev.to/coderwenzi</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%2F4275073%2F920d73a1-e674-4f54-bc05-a3726040f4d9.png</url>
      <title>DEV Community: coderWenzi</title>
      <link>https://dev.to/coderwenzi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/coderwenzi"/>
    <language>en</language>
    <item>
      <title>Preparing user videos for AI video models with FFmpeg: 5 checks that saved us failed jobs</title>
      <dc:creator>coderWenzi</dc:creator>
      <pubDate>Mon, 30 Nov 2026 20:56:08 +0000</pubDate>
      <link>https://dev.to/coderwenzi/preparing-user-videos-for-ai-video-models-with-ffmpeg-5-checks-that-saved-us-failed-jobs-4f15</link>
      <guid>https://dev.to/coderwenzi/preparing-user-videos-for-ai-video-models-with-ffmpeg-5-checks-that-saved-us-failed-jobs-4f15</guid>
      <description>&lt;p&gt;AI video models are picky about their input. A clip that plays fine on a phone can still be rejected upstream for its length, its shape or its audio track, or it can produce a broken result. Each failure costs a paid generation and a confused user.&lt;/p&gt;

&lt;p&gt;We run every uploaded video through a small Node 22 + FFmpeg service before it reaches the model. This post covers the five checks that removed most of our failed jobs, with the commands we actually use.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Lock FFmpeg down before it touches an upload
&lt;/h2&gt;

&lt;p&gt;An uploaded file is untrusted input, and FFmpeg can open network URLs and many exotic formats. The preparation service allows only the demuxers it needs and only local file protocols:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="s1"&gt;'-xerror'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'-format_whitelist'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'mov,png_pipe,jpeg_pipe,webp_pipe,bmp_pipe'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'-protocol_whitelist'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'file,pipe'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// ...the actual job&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="nf"&gt;execFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ffmpeg'&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;120_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;killSignal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'SIGKILL'&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;Three details matter here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-format_whitelist&lt;/code&gt; stops a renamed file from reaching a demuxer you never meant to expose. MP4 and MOV both use the &lt;code&gt;mov&lt;/code&gt; demuxer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-protocol_whitelist file,pipe&lt;/code&gt; blocks &lt;code&gt;http&lt;/code&gt;, &lt;code&gt;concat&lt;/code&gt; and friends inside crafted playlists.&lt;/li&gt;
&lt;li&gt;Every command has a timeout and a hard kill. A malformed file should fail fast instead of tying up a worker.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We also never return FFmpeg's raw error to the user. It can contain local paths and metadata from the upload, so the service maps every failure to one plain message.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Measure duration the way the model does
&lt;/h2&gt;

&lt;p&gt;Our model accepts 4 to 30 seconds. The first version read &lt;code&gt;format.duration&lt;/code&gt; and called it done. It wasn't enough.&lt;/p&gt;

&lt;p&gt;A video file has several durations: the container's, the picture track's and the audio track's, each with its own start offset. A phone recording often has an audio track that runs a few milliseconds past the picture. Opus audio in MP4 can also start at a positive offset. The model counts the longest track, so a "29.98 second" video can be rejected as over 30.&lt;/p&gt;

&lt;p&gt;We compute two numbers with &lt;code&gt;ffprobe -show_streams -show_format&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;duration&lt;/strong&gt;: the picture track, which is what the user sees.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mediaDuration&lt;/strong&gt;: the maximum of the container duration and every audio or video stream's &lt;code&gt;duration + start_time&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The picture must be 4 to 30 seconds. The whole file must also be at most 30 seconds. When only a tiny audio tail (50 ms or less) crosses the limit, we trim the audio against the picture timeline and leave the video stream untouched:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ffmpeg -i in.mp4 -map 0:v:0 -map 0:a? -c:v copy \
  -c:a aac -b:a 192k -af &lt;span class="s2"&gt;"atrim=end=29.97"&lt;/span&gt; -movflags +faststart out.mp4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the fix we hash the video packets of both files (&lt;code&gt;-c:v copy -f hash -hash sha256&lt;/code&gt;) to prove the picture did not change. Anything longer than a 50 ms tail goes back to the user with the exact numbers, so they know what to trim.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Cut exactly where asked, and aim a little short
&lt;/h2&gt;

&lt;p&gt;Free previews keep 5 seconds, and long uploads keep the 30-second part the user picks. Stream copy (&lt;code&gt;-c copy&lt;/code&gt;) cuts on keyframes, so the clip can start a second early or run long. We re-encode instead, so the cut lands on the requested frame:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ffmpeg -ss 12.000 -i in.mov -t 4.950 \
  -map 0:v:0 -map 0:a:0? -c:v libx264 -preset veryfast -crf 18 \
  -pix_fmt yuv420p -c:a aac -b:a 192k -movflags +faststart out.mp4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note &lt;code&gt;-t 4.950&lt;/code&gt; for a 5-second clip. Frame and audio rounding can add a few milliseconds, and video APIs often bill any started second as a whole second. Aiming 50 ms short keeps a 5-second clip billed as 5 seconds. We re-probe the output and reject it if it still came out too long.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Center-crop extreme aspect ratios instead of failing
&lt;/h2&gt;

&lt;p&gt;Phone screen recordings are tall, for example 790 × 2140. That is about 2.7:1, and our model rejects anything beyond 2.5:1 in either direction. Instead of an error, we crop the long side, centered, to the limit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ffmpeg -i in.mp4 -map 0:v:0 -map 0:a:0? \
  -vf &lt;span class="s2"&gt;"crop=w=min(iw\,2*floor(ih*2.5/2)):h=min(ih\,2*floor(iw*2.5/2))"&lt;/span&gt; \
  -c:v libx264 -preset veryfast -crf 18 -pix_fmt yuv420p -c:a aac out.mp4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The crop runs after FFmpeg applies rotation metadata, so &lt;code&gt;iw&lt;/code&gt; and &lt;code&gt;ih&lt;/code&gt; are the upright picture the user sees. The &lt;code&gt;2*floor(.../2)&lt;/code&gt; keeps both sides even, which H.264 with &lt;code&gt;yuv420p&lt;/code&gt; requires. Videos already inside the limit are never re-encoded.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Find shot changes, including the soft ones
&lt;/h2&gt;

&lt;p&gt;When a clip has several shots, the model needs to know where each shot starts. If it misses a cut, the generated video can drift back to the original person after that cut.&lt;/p&gt;

&lt;p&gt;FFmpeg's scene filter finds hard cuts cheaply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ffmpeg -i in.mp4 -map 0:v:0 -an \
  -vf &lt;span class="s2"&gt;"setpts=PTS-STARTPTS,scale=320:-2,select='gt(scene,0.3)',showinfo"&lt;/span&gt; \
  -f null - 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1 &lt;span class="o"&gt;|&lt;/span&gt; grep -o &lt;span class="s1"&gt;'pts_time:[0-9.]*'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It misses soft transitions: fades through black, flashes and whip pans in dark footage. For those, we decode the clip into tiny 32 × 18 RGB thumbnails at 30 fps (&lt;code&gt;-f rawvideo -pix_fmt rgb24&lt;/code&gt;) and look at two signals in Node:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Correlation between neighbouring frames.&lt;/strong&gt; Each thumbnail is normalized per channel (subtract the mean, divide by the standard deviation), so a brightness change alone does not look like a cut. A correlation below 0.35 between two frames with real contrast counts as a cut.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Brightness dips.&lt;/strong&gt; When brightness falls below 45% of the level on both sides for at most 1.5 seconds, and the pictures before and after the dip differ, the darkest frame counts as a cut.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Soft cuts closer than 0.5 seconds to another cut, or to either end of the clip, are dropped. The pass stays cheap because each 32 × 18 frame is only 1,728 bytes, so a 15-second clip is under 1 MB of raw pixels.&lt;/p&gt;

&lt;h2&gt;
  
  
  What else runs on every file
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Photos&lt;/strong&gt; are decoded completely, not just probed. A truncated JPEG often probes fine, so we also walk the JPEG markers and require a real end-of-image marker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PNG transparency&lt;/strong&gt; is checked pixel by pixel with &lt;code&gt;alphaextract&lt;/code&gt;. A PNG with an alpha channel but no transparent pixels gets the channel removed, and we compare picture hashes before and after. A photo that really is transparent is sent back with a request to fill the background.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Jobs are isolated.&lt;/strong&gt; Each preparation gets its own temporary directory that expires after ten minutes, and a service instance runs at most two preparations at once.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;We built this service for &lt;a href="https://genjutsu.video/" rel="noopener noreferrer"&gt;Genjutsu AI&lt;/a&gt;, a tool that swaps a person or object in a short video with one from a photo. The checks above are model-agnostic, though. If you send user video to any generation API, measuring duration across all tracks and handling extreme aspect ratios before upload will save you paid retries.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>ffmpeg</category>
      <category>node</category>
      <category>video</category>
    </item>
  </channel>
</rss>
