Search code examples
videoffmpegcutsubtitle

ffmpeg cut video and burn subtitle in a single command


I want to cut a piece out of a video and burn subtitle in that piece.

I can do this in 3 steps:

  1. cut the video
    ffmpeg -ss 25:00 -to 26:00 -i vid.mp4 -c copy out.mp4

  2. cut the subtitle
    ffmpeg -i sub.srt -ss 25:00 -to 26:00 out.srt

  3. burn subtitle in the video piece
    ffmpeg -i out.mp4 -vf subtitles=out.srt -c:a copy -y final.mp4

But I want to do this in a single ffmpeg command.

If I do this
ffmpeg -ss 25:00 -to 26:00 -i vid.mp4 -vf subtitles=sub.srt -c:a copy -y final.mp4
the video is cut but no subtitle is burned into it.
This is fast.

If I do this
ffmpeg -i vid.mp4 -ss 25:00 -to 26:00 -vf subtitles=sub.srt -c:a copy -y final.mp4
the video is cut and subtitles burned correctly,but there is a delay in starting writing the final.mp4.
I think ffmpeg is processing vid.mp4 from the beginning till it reach the -ss time (and drop that part) then continue processing and writing it to final.mp4

Is there a way to do this fast and in a single ffmpeg command?
Like ffmpeg going directly to -ss time and cut that, process it, burn subtitle in it.

Thanks


Solution

  • Yes, the subtitles filter opens the subtitle file internally and isn't aware of the seek point for the video since ffmpeg resets timestamps after seeking.

    So preserve the source timestamps and then reset them i.e.

    ffmpeg -ss 25:00 -to 26:00 -copyts -i vid.mp4 -vf subtitles=sub.srt  -c:a copy -ss 25:00 -y final.mp4