如何使用 ffmpeg 将集成字幕替换为外部字幕?

如何使用 ffmpeg 将集成字幕替换为外部字幕?

Stream #0:6(eng)我想用外部字幕替换subtitle.ass

视频信息

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

答案1

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包括来自的所有流input.mkv
  • -map -0:6负图且排除Stream #0:6
  • -map 1包括来自的所有流subtitle.ass
  • -c copy启用流复制模式,因此所有内容都只是多路复用,避免了重新编码。就像复制和粘贴一样。
  • -metadata:s:s:3 language=eng将新添加的字幕语言设置为英语。
    • 由于选项的顺序,新字幕将在所有其他字幕之后添加-map
    • 包含了来自 的 3 个字幕input.mkv,因此来自 的字幕subtitle.ass将是第 4 个字幕。
    • s:s:34指的是stream:subtitles:index #3,或“输出中的第 4 个字幕流”(从 0 开始计数)。

或者使用字幕语言元数据的更灵活但更复杂的方法:

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是一个负映射,它排除任何input.mkv具有英语元数据的字幕流。

FFmpeg Wiki:地图

相关内容