[Libav-user] Add subtitles to MKV file

ingmar ingmar at kingsoftware.com.tw
Sat Jul 5 10:15:44 CEST 2014

Hello Everyone!


I am trying to add some text, being a stream, into an MKV file which has
encoded video and audio data already. Because of some reasons, storing the
text to an isolated file is not acceptable.


The problem I encountered was the times of subtitles were incorrect like




00:00:00,000 --> 00:00:00,999

2014-07-01 11:59:28



00:00:01,000 --> 00:00:01,999

2014-07-01 11:59:29




00:02:11,072 --> 00:02:12,071

2014-07-01 11:59:28



00:02:12,072 --> 00:02:13,071

2014-07-01 11:59:29


My approach is following.

1. Create 3 streams: video, audio and subtitle with code AV_CODEC_ID_SUBRIP
and add to the target MKV file.

2. Encode all video and audio data.

3. Create a temporary SRT file and store all text in it.

4. Read a packet from SRT file (av_read_frame) -> decode
(avcodec_decode_subtitle2) -> encode (avcodec_encode_subtitle) -> write to
MKV file (av_interleaved_write_frame).


I have been sure the time_base fields of the input and output streams were
the same; and also for the pts, dts and duration ones of the input and
output packets.


Any advice would be greatly appreciated.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20140705/b29a1b79/attachment.html>

More information about the Libav-user mailing list