Answers.Body: <p><a href="https://ffmpeg.org/ffmpeg-filters.html#trim" rel="nofollow noreferrer">https://ffmpeg.org/ffmpeg-filters.html#trim</a></p>
  <blockquote>
  <p>Also note that this filter does not modify the timestamps. If you wish for the output timestamps to start at zero, insert a setpts filter after the trim filter.</p>
  </blockquote>
  <p>Add <code>setpts=PTS-STARTPTS</code> after your trim filter</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-15T02:58:54.733Z
Answers.LastActivityDate: 2022-04-15T02:58:54.733Z
Answers.UserDisplayName: Anonymous
Body: <p>When using <code>trim</code> to crop the start <em>and</em> end of a video, the frames <em>leading</em> up to my &quot;crop start&quot; time are static instead of removed.  However, the frames <em>following</em> the &quot;crop end&quot; time are correctly removed.</p>
  <p>To clarify- if the input is 10s, crop start is 2s, crop end is 5s, I expect a 3s video.  Instead I get a 5s video, where 0-2s are black frames.</p>
  <p>What part of my command could be causing this?</p>
  <pre class="lang-js prettyprint-override"><code>const args = [
      // '-ss',           // not helping
      // `${trimStart}`,  // not helping
      '-i',
      `${file.path}`,
      '-filter_complex',
      `\
          [0]split[body][pre];\
          [pre]trim=duration=${crossfadeDuration / 2},\
          format=yuva420p,\
          fade=d=${crossfadeDuration / 2}:alpha=1,\
          setpts=PTS+(${trimStart + newDuration - crossfadeDuration / 2}/TB)[jt];\
          [body]trim=${crossfadeDuration / 2},\
          setpts=PTS-STARTPTS[main];\
          [main]trim=start=${trimStart}:duration=${newDuration}[main];\
          [main][jt]overlay,${filter}\
          fps=${fps &lt; 24 ? fps : 24},\
          crop=trunc(iw/2)*2:trunc(ih/2)*2,\
          setsar=1:1`,
          // '-an',       // not helping
          // '-shortest', // not helping
          '-y',
      `${out}`,
  ]
  </code></pre>
  <p>For context- my app is adding seamless loops to videos with an interactive trimmer.  Here is a GIF of what the above command currently does:</p>
  <p><a href="https://i.sstatic.net/3ht7N.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/3ht7N.gif" alt="timeline editor showing missing start frames" /></a></p>
  <p>I suspect it has to do with PTS, but I'm really struggling to wrap my head around which PTS settings do what depending on their order, especially when dealing with multiple streams of varrying length.</p>
  
Tags: <ffmpeg>
Title: How to avoid black frames with ffmpeg "trim" filter?
UserDisplayName: Fractal
ThreadText: # How to avoid black frames with ffmpeg "trim" filter?
  
  (Asked by: username_0 on 2022-04-14)
  
  When using `trim` to crop the start *and* end of a video, the frames *leading* up to my "crop start" time are static instead of removed. However, the frames *following* the "crop end" time are correctly removed.
  
  To clarify- if the input is 10s, crop start is 2s, crop end is 5s, I expect a 3s video. Instead I get a 5s video, where 0-2s are black frames.
  
  What part of my command could be causing this?
  
  ```js
  const args = [
      // '-ss',           // not helping
      // `${trimStart}`,  // not helping
      '-i',
      `${file.path}`,
      '-filter_complex',
      `\
          [0]split[body][pre];\
          [pre]trim=duration=${crossfadeDuration / 2},\
          format=yuva420p,\
          fade=d=${crossfadeDuration / 2}:alpha=1,\
          setpts=PTS+(${trimStart + newDuration - crossfadeDuration / 2}/TB)[jt];\
          [body]trim=${crossfadeDuration / 2},\
          setpts=PTS-STARTPTS[main];\
          [main]trim=start=${trimStart}:duration=${newDuration}[main];\
          [main][jt]overlay,${filter}\
          fps=${fps < 24 ? fps : 24},\
          crop=trunc(iw/2)*2:trunc(ih/2)*2,\
          setsar=1:1`,
          // '-an',       // not helping
          // '-shortest', // not helping
          '-y',
      `${out}`,
  ]
  
  ```
  
  For context- my app is adding seamless loops to videos with an interactive trimmer. Here is a GIF of what the above command currently does:
  
  [![timeline editor showing missing start frames](https://i.sstatic.net/3ht7N.gif)](https://i.sstatic.net/3ht7N.gif)
  
  I suspect it has to do with PTS, but I'm really struggling to wrap my head around which PTS settings do what depending on their order, especially when dealing with multiple streams of varrying length.
  
  **Tags:** ffmpeg
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-04-15. Score: 1 | Accepted answer
  
  <https://ffmpeg.org/ffmpeg-filters.html#trim>
  
  > Also note that this filter does not modify the timestamps. If you wish for the output timestamps to start at zero, insert a setpts filter after the trim filter.
  
  Add `setpts=PTS-STARTPTS` after your trim filter
  
  
  ---
  
  