1

I want to replace the subtitle present in Stream #0:6(eng) with an external subtitle.ass

video information

Input #0, matroska,webm, from 'input.mkv':
  Metadata:
    creation_time   : 2021-03-06T17:59:28.000000Z
    ENCODER         : Lavf58.29.100
Stream #0:0: Video: hevc (Main), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 23.98 fps, 23.98 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      BPS-eng         : 2464510
      DURATION-eng    : 00:22:57.001000000
      NUMBER_OF_FRAMES-eng: 33015
      NUMBER_OF_BYTES-eng: 424204175
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:1(jpn): Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, stereo, fltp, 224 kb/s
    Metadata:
      BPS-eng         : 224000
      DURATION-eng    : 00:22:57.408000000
      NUMBER_OF_FRAMES-eng: 43044
      NUMBER_OF_BYTES-eng: 38567424
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:2(eng): Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, stereo, fltp, 224 kb/s (default)
    Metadata:
      BPS-eng         : 224000
      DURATION-eng    : 00:22:57.408000000
      NUMBER_OF_FRAMES-eng: 43044
      NUMBER_OF_BYTES-eng: 38567424
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:3(spa): Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, stereo, fltp, 224 kb/s
    Metadata:
      BPS-eng         : 224000
      DURATION-eng    : 00:22:57.408000000
      NUMBER_OF_FRAMES-eng: 43044
      NUMBER_OF_BYTES-eng: 38567424
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:4(fre): Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, stereo, fltp, 224 kb/s
    Metadata:
      BPS-eng         : 224000
      DURATION-eng    : 00:22:57.408000000
      NUMBER_OF_FRAMES-eng: 43044
      NUMBER_OF_BYTES-eng: 38567424
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:5(por): Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, stereo, fltp, 224 kb/s
    Metadata:
      BPS-eng         : 224000
      DURATION-eng    : 00:22:57.408000000
      NUMBER_OF_FRAMES-eng: 43044
      NUMBER_OF_BYTES-eng: 38567424
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:6(eng): Subtitle: hdmv_pgs_subtitle ([255][255][255][255] / 0xFFFFFFFF), 1920x1080 (default)
    Metadata:
      title           : English BD PGS Sub
      BPS-eng         : 35741
      DURATION-eng    : 00:22:47.742000000
      NUMBER_OF_FRAMES-eng: 692
      NUMBER_OF_BYTES-eng: 6110646
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:7(spa): Subtitle: hdmv_pgs_subtitle ([255][255][255][255] / 0xFFFFFFFF), 1920x1080
    Metadata:
      title           : Spanish BD PGS Sub
      BPS-eng         : 41766
      DURATION-eng    : 00:22:47.742000000
      NUMBER_OF_FRAMES-eng: 764
      NUMBER_OF_BYTES-eng: 7140737
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:8(fre): Subtitle: hdmv_pgs_subtitle ([255][255][255][255] / 0xFFFFFFFF)
    Metadata:
      title           : French BD PGS Sub
      BPS-eng         : 36969
      DURATION-eng    : 00:20:43.492000000
      NUMBER_OF_FRAMES-eng: 586
      NUMBER_OF_BYTES-eng: 5746361
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:9(por): Subtitle: hdmv_pgs_subtitle ([255][255][255][255] / 0xFFFFFFFF), 1920x1080
    Metadata:
      title           : Portuguese BD PGS Sub
      BPS-eng         : 40091
      DURATION-eng    : 00:22:47.909000000
      NUMBER_OF_FRAMES-eng: 590
      NUMBER_OF_BYTES-eng: 6855165
      _STATISTICS_WRITING_APP-eng: mkvmerge v53.0.0 ('Fool's Gold') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2021-03-06 17:59:28
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
2
ffmpeg -i input.mkv -i subtitle.ass -map 0 -map -0:6 -map 1 -c copy -metadata:s:s:4 language=eng output.mkv
  • -map 0 includes all streams from input.mkv.
  • -map -0:6 is a negative map and excludes Stream #0:6.
  • -map 1 includes all streams from subtitle.ass.
  • -c copy enables stream copy mode, so everything is just muxed and re-encoding is avoided. Like a copy and paste.
  • -metadata:s:s:3 language=eng sets the language as English for the newly added subtitles.
    • The new subtitles are going to be added after all the others due to the order of the -map options.
    • There are 3 subtitles being included from input.mkv, so the subtitles from subtitle.ass will be the 4th subtitle.
    • s:s:34 refers to stream:subtitles:index #3, or "the 4th subtitle stream in the output" (it starts counting from 0).

Or a more flexible but more complicated method using subtitle language metadata:

ffmpeg -i input.mkv -i subtitle.ass -map 0 -map -0:s:m:language:eng -map 1 -c copy output.mkv
  • -map -0:s:m:language:eng is a negative map that excludes any subtitle stream from input.mkv that has English language metadata.

See FFmpeg Wiki: Map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.