ffmpeg cut video and burn subtitle in a single command
Asked Answered
P

2

6

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

Pironi answered 3/1, 2020 at 8:27 Comment(0)
S
13

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
Shote answered 3/1, 2020 at 9:59 Comment(0)
U
0

Subtitles go as a separate stream (just as video and audio do). You need to specify which streams you want to get copied. Assuming video is Stream #0:0, audio is Stream #0:1 and subtitles are Stream #0:2, the command will be

ffmpeg -ss 25:00 -to 26:00 -i vid.mp4 -c copy -map 0:0 -map 0:1 -map 0:2 out.mp4

You can inquire about existing streams using ffprobe:

ffprobe -i vid.mp4

which will yield something like

Stream #0:0(eng): Video: h264 (High), yuv420p(progressive), 1912x1036 [SAR 1:1 DAR 478:259], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
Stream #0:1(fre): Audio: ac3, 48000 Hz, 5.1(side), fltp, 448
Stream #0:2(eng): Subtitle: subrip
Ultracentrifuge answered 19/1, 2022 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.