title: FFMPEG - Getting consistent accurate video time segments
body: <p>I'm processing a large number of videos that need to be time-synchronized to match an audio track (it's a "virtual choir"-type project).  I have tried several variations based on the contents of <a href="https://stackoverflow.com/questions/21420296/how-to-extract-time-accurate-video-segments-with-ffmpeg/21422776#21422776">this post</a>, but I get a variability of some seconds on the actual output files.  The resulting segment length is variable, and the point in the input video that the output video begins is not at all accurate (I was expecting a +-1 frame error).</p>
  
  <p>Here's my attempt:</p>
  
  <pre><code>:: Syntax:  GetSegment.bat &lt;input file&gt; &lt;starting time in mS&gt; &lt;duration in mS&gt;
  :: Example: GetSegment.bat vid.mp4 10000 50000
  ::          would produce vid-keyed.mp4 and vid-segment.mp4
  
  @set "_VideoIn=%~1"
  @set "_VideoOutKeyed=%~dpn1-keyed%~x1"
  @set "_VideoOutSegment=%~dpn1-segment%~x1"
  @set "_VideoStartms=%~2"
  @set "_VideoLengthms=%~3"
  @set /a _VideoEndms= %_VideoStartms% + %_VideoLengthms%
  ffmpeg -y -i "%_VideoIn%" -force_key_frames %_VideoStartms%ms,%_VideoEndms%ms "%_VideoOutKeyed%"
  ffmpeg -y -ss  %_VideoStartms%ms -i "%_VideoOutKeyed%" -t %_VideoEndms%ms  -codec copy "%_VideoOutSegment%"  
  @exit /b %errorlevel%
  </code></pre>