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 this post, 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).
Here's my attempt:
:: Syntax: GetSegment.bat <input file> <starting time in mS> <duration in mS>
:: 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%