[FFmpeg-devel] [PATCH] ffmpeg: honor -ss and -t parameters with muxed subtitles.

Clément Bœsch ubitux at gmail.com
Wed Aug 1 18:43:18 CEST 2012

This patch fixes two things:

 - in case of subtitles, check_recording_time() is comparing the current
   PTS to the recording time (-t option, set to INT_MAX by default), so
   the -ss option needs to be taken into account. It is not required in
   do_{audio,video}_out() because this adjustment is set while polling
   the filtergraph (see poll_filters()).

 - the second fix is also an adjustment of the PTS send to the encoder
   (and later transmitted to the muxer) so the TS in the output make
   sense and are not kept verbatim.

Note: this only works for muxers honoring the PTS, such as
lavf/matroskaenc. But for other such as the ASS muxer which just does a
verbatim copy, or the SubRip muxer which doesn't write the TS in some
cases, it will not work yet.
 ffmpeg.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index f85d8e0..c6fafe4 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -1684,13 +1684,22 @@ static void do_subtitle_out(AVFormatContext *s,
         nb = 1;
     for (i = 0; i < nb; i++) {
-        ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base);
+        ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base)
+                       /* start time adjustment so -ss and -t works */
+                       - av_rescale_q(output_files[ost->file_index]->start_time, AV_TIME_BASE_Q, enc->time_base);
         if (!check_recording_time(ost))
         sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
         // start_display_time is required to be 0
         sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
+        /* The following adjustment is only done for audio & video types when
+         * dealing with lavfi. This should be dropped when subtitles are part
+         * of the lavfi loop */
+        sub->pts -= output_files[ost->file_index]->start_time;
         sub->end_display_time  -= sub->start_display_time;
         sub->start_display_time = 0;
         subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,

More information about the ffmpeg-devel mailing list