0

I'm using MakeMKV to create a digital copy of my BluRay discs and have created a script to do some processing on the resulting MKV file:

  • resample (and downscale if wanted) the video
  • convert a single audio track to both AAC and AC3 (for each language I want) and discard the "input" audio track(s)
  • copy all subtitles for specific languages
  • copy chapter marks

I want to extend the script to burn in a single PGS subtitle track. However, when I add the proper overlay filter the resulting burned in subtitles are out of sync.

In general, the script uses mkvextract to extract the relevant video and audio tracks and uses ffmpeg to convert these to new tracks. After conversion all output tracks are merged using mkvmerge.

For the subtitle handling I use mkvextract to extract the proper subtitle track to a .sup file and pass this as an additional input to ffmpeg:

ffmpeg -i ./title_t00/in/title_t00.mkv.h264 \
    -i ./title_t00/in/title_t00.mkv.en.sup \
    -codec:v libx264 \
    -filter_complex "[0:v][1:s]overlay[v1]; [v1]scale=w=-2:h=480:flags=gauss[v2]"\
    -map "[v2]" \
    -sws_flags gauss \
    -crf 10 \
    ./title_t00/out/title_t00.mkv.h264

The filter takes the video track from the first input and the subtitle track from the second input and overlay both. This results in a new video stream v1 which is passed through the scale filter and the result is saved.

As said, the overlay works but the subtitles are out of sync. Is there a way I can properly sync them?

Or is there some magic ffmpeg invocation that can do it all in one pass?

4
  • 1
    How much is the desync and is it constant? Commented Apr 28, 2019 at 8:48
  • For the example video it looks like the subtitles appear about 7 seconds before they should. Also, it seems to be remain constant throughout the video. Using mediainfo the videotrack is 29:20 long, the subtitle track 28:15. So I guess I need to add some time shift in the mix. How can this be determined automatically? Commented Apr 28, 2019 at 9:40
  • Also note that the subtitles are in sync when playing the original file so the information must be available somewhere Commented Apr 28, 2019 at 9:44
  • 1
    Don't extract the subtitles. Feed and reference the mkv directly. Commented Apr 28, 2019 at 9:51

1 Answer 1

0

After looking into stream mapping I found the following solution:

ffmpeg -i INPUT.mkv \
    -filter_complex "[0:v][0:s]overlay[v1]; [v1]scale=w=-2:h=480:flags=gauss[v2]"
    -map "[v2]" \
    -map 0:1
    -map 0:1
    -map 0:2
    -codec:v:0 libx264 -crf 10
    -codec:a:0 libfdk_aac -ac:a:0 2
    -codec:a:1 ac3 -ac:a:1 6
    -codec:s copy \
    OUTPUT.mkv

The filter will overlay the first video track with the first subtitle track, scale down to a height of 480p and map the filtered video stream to be the first output stream. The next two -maps pass the audio stream twice to the output so I can create both an AAC and AC-3 stream. The last -map is for the subtitle stream and this is just copied to the output.

This needs to be fine-tuned so it will support multiple languages for the audio streams, filter any unwanted subtitle tracks and also filter the burned-in subtitle track.

See How to Map Streams in FFMPEG for more information on stream mapping.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.