Burn-in subtitles with avconv?

6,071

Text-based subtitles

ffmpeg has a subtitles filter that can burn SRT files into the video stream. You need ffmpeg compiled with --enable-libass though. I don't think Libav has something similar, at least looking over the available filters.

You first need to extract the subtitles. -map 0:s:0 selects the first subtitle stream for the output, and -c:s:0 selects the codec for it – just to make sure it's really SRT.

ffmpeg -i input.mkv -an -vn -map 0:s:0 -c:s:0 srt subtitles.srt

Then, burn them in.

ffmpeg -i input.mkv -map 0:0 -map 0:1 -target pal-dvd -aspect 16:9 -q:v 1 \
-c:a mp2 -ac 2 -b:a 128k -filter:v subtitles=subtitles.srt output.mpg

Note:

  • The above command is assuming that streams 0:0 and 0:1 in your input are the video and audio streams, respectively. If you only want to prevent the subtitles from being included in the output file, then you can also add -sn to disable them altogether.

  • You have to change -ab to -b:a, since the former is a non-existing option for ffmpeg.


Image-based subtitles

In case your input has image-based subtitles like HDMV PG (seems MakeMKV is creating those, and other tools are unable to extract them), you can try using the overlay filter to burn them in:

ffmpeg -i input.mkv -filter_complex "[0:v][0:s]overlay[v]" -map [v] -map 0:a \
-target pal-dvd -aspect 16:9 -q:v 1 -c:a mp2 -ac 2 -b:a 128k output.mpg

This is also explained on the FFmpeg Wiki.

In case your subtitles come in different dimensions than the original movie (e.g. because the video stream was scaled down but the subtitles weren't), you can try using the -canvas_size option to set the size.

Share:
6,071

Related videos on Youtube

user4811
Author by

user4811

Updated on September 18, 2022

Comments

  • user4811
    user4811 almost 2 years

    I have a mkv file and want to convert it to an DVD compatible mpg file with avconv. How can I also burn-in one subtitle stream from the same mkv file?

    avconv -i input.mkv -map 0:0 -map 0:1 -target pal-dvd -aspect 16:9 -q:v 1 -acodec mp2 -ac 2 -ab 128k output.mpg

  • user4811
    user4811 almost 11 years
    Thanks for your help but the extraction doesn't work. I get the following error message "Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height". The subtitle stream is announced as "Subtitle: hdmv_pgs_subtitle". Do you know what the problem is?
  • user4811
    user4811 almost 11 years
    Thx! I have also tried this but subtitles don't appear and I get the message "sub2video: rectangle overflowing". I have figured out that the problem is that the source was converted from 1080p to 720p but the subtitles are still 1080p. Is is possible to scale the subtitle stream to 720p before overlaying?
  • slhck
    slhck almost 11 years
    Yes, that should be possible. "[0:s]scale=-1:720[subs];[0:v][subs]overlay[v]" or something. I cannot test it right now, but something like that should work.
  • user4811
    user4811 almost 11 years
    Damn, still get "sub2video: rectangle overflowing" and no subtitles in the output. This is correct, or? ffmpeg -i input.mkv -filter_complex "[0:s]scale=-1:720[subs];[0:v][subs]overlay[v]" -map [v] -map 0:a:0 -target pal-dvd -aspect 16:9 -q:v 1 -c:a mp2 -ac 2 -b:a 128k output.mpg
  • user4811
    user4811 almost 11 years
    The solution is the "-canvas_size" parameter to specify the canvas for subtitles. It should be the first parameter!
  • slhck
    slhck almost 11 years
    Oh, I'm glad you found it out!