2

I am extracting a 1-minute-long fragment of a video file starting at 30 min from the beginning, and adding subtitles from an external file using the following command:

ffmpeg -ss 1800 -i input_video.m4v -ss 1800 -i input_subtitle.srt -map 1:0 -c:a aac -c:v libx264 -c:s mov_text -crf 35 -preset slow -t 60 -avoid_negative_ts 1 output_video.mp4

The problem I get is that the transcoded video has duration 00:01:03.50 instead of expected 00:01:00.00. And when playing such video with VLC, the playback stops before the progress bar reaches the end.

The duration becomes exactly 1 minute only if I remove the subtitle input. Looks like the duration of the output file is extended to fit in the last phrase from the subtitle that starts within the defined 1-minute range.

Is there a way to force the output duration to exactly the value set in the -d flag?

2
  • Do the subtitles have to be soft?
    – Gyan
    Commented Jan 16, 2017 at 7:50
  • Yes. They must be switchable.
    – BartoNaz
    Commented Jan 16, 2017 at 7:51

1 Answer 1

1

Due to how ffmpeg parses subtitle files, there are two options available to you:

Output to MP4, but possibly skip a subtitle:

ffmpeg -i input_video.m4v -i input_subtitle.srt -ss 1800 -c:a aac -c:v libx264 -c:s mov_text -crf 35 -preset slow -t 60 output_video.mp4

This should produce a 60 second file but it will skip over any subtitles which start before 1800s but extend past it.

Output to MKV:

ffmpeg -i input_video.m4v -i input_subtitle.srt -ss 1800 -c:a aac -c:v libx264 -c:s ass -crf 35 -preset slow -t 60 output_video.mkv

Here any subtitle starting before 1800s and extending past it will be present and of the commensurate length i.e. if a sub starts at 1795 and ends at 1802, then it will last for 2 seconds in the output.

2-step method

ffmpeg -i subfile -ss 1800 -t 60 sub.ass

and

ffmpeg -ss 1800 -i video.mp4 -i sub.ass -c:s mov_text out.mp4

This extracts the subs corresponding to the desired video segment and then that is muxed with the video.

5
  • Thanks for the suggestion. In the last example wouldn't subtitle last for 5 seconds if it's truncated?
    – BartoNaz
    Commented Jan 16, 2017 at 16:25
  • But both these solutions have a major problem that they have -ss flag before the output file, which will trigger the slow seeking without using keyframes. So it will take very long to first decode 30 minutes of video before reaching the interesting part. See more on this here: trac.ffmpeg.org/wiki/Seeking
    – BartoNaz
    Commented Jan 16, 2017 at 16:28
  • 1
    I'm well-aware of slow seeking. But if you want the sub decoder to not include earlier subs and to write correct relative timings for the included subs, then it has to be slow seek, because unlike audio/video streams, subtitles aren't packetized and are present as 1 "frame". Fast seek works at the demuxer level and relies on the container index to skip to the byte offset of the nearest preceding keyframe. But there's only 1 frame for subtitles.
    – Gyan
    Commented Jan 16, 2017 at 18:09
  • 1
    If the time expense is too high, you can do this in two steps. Use ffmpeg -i subfile -ss 1800 -t 60 sub.ass and then ffmpeg -ss 1800 -i video.mp4 -i sub.ass -c:s mov_text out.mp4
    – Gyan
    Commented Jan 16, 2017 at 18:11
  • Thanks a lot. This clarifies it a lot. Please, update the answer with this 2-step solution, and I can accept it.
    – BartoNaz
    Commented Jan 16, 2017 at 18:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .