ffmpeg 合并音频、视频和字幕不起作用。我做错了什么?

ffmpeg 合并音频、视频和字幕不起作用。我做错了什么?

我看过很多指南,但我不明白我做错了什么。这是 ffprobe 代码:

ffprobe version N-104437-ge84c83ef98-20211028 Copyright (c) 2007-2021 the FFmpeg developers
  built with gcc 10-win32 (GCC) 20210408
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-shared --disable-static --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --enable-libvmaf --enable-vulkan --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libglslang --enable-libgme --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20211028
  libavutil      57.  7.100 / 57.  7.100
  libavcodec     59. 12.100 / 59. 12.100
  libavformat    59.  8.100 / 59.  8.100
  libavdevice    59.  0.101 / 59.  0.101
  libavfilter     8. 15.100 /  8. 15.100
  libswscale      6.  1.100 /  6.  1.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100
Input #0, matroska,webm, from 'filesource.mkv':
  Metadata:
    encoder         : libebml v1.3.5 + libmatroska v1.4.8
    creation_time   : 2019-06-28T17:22:18.000000Z
  Duration: 00:24:00.08, start: 0.000000, bitrate: 3203 kb/s
  Stream #0:0: Video: h264 (High), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn (default)
    Metadata:
      BPS-eng         : 3071385
      DURATION-eng    : 00:23:59.982000000
      NUMBER_OF_FRAMES-eng: 34525
      NUMBER_OF_BYTES-eng: 552842428
      _STATISTICS_WRITING_APP-eng: mkvmerge v22.0.0 ('At The End Of The World') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2019-06-28 17:22:18
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
  Stream #0:1(jpn): Audio: aac (LC), 44100 Hz, stereo, fltp (default)
    Metadata:
      BPS-eng         : 128000
      DURATION-eng    : 00:24:00.078000000
      NUMBER_OF_FRAMES-eng: 62019
      NUMBER_OF_BYTES-eng: 23041253
      _STATISTICS_WRITING_APP-eng: mkvmerge v22.0.0 ('At The End Of The World') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2019-06-28 17:22:18
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
  Stream #0:2(eng): Subtitle: ass
    Metadata:
      BPS-eng         : 102
      DURATION-eng    : 00:21:00.020000000
      NUMBER_OF_FRAMES-eng: 243
      NUMBER_OF_BYTES-eng: 16208
      _STATISTICS_WRITING_APP-eng: mkvmerge v22.0.0 ('At The End Of The World') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2019-06-28 17:22:18
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
  Stream #0:3(ita): Subtitle: ass (default) (forced)
    Metadata:
      title           : Sottotitoli completi
      BPS-eng         : 107
      DURATION-eng    : 00:21:00.020000000
      NUMBER_OF_FRAMES-eng: 257
      NUMBER_OF_BYTES-eng: 16881
      _STATISTICS_WRITING_APP-eng: mkvmerge v22.0.0 ('At The End Of The World') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2019-06-28 17:22:18
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
  Stream #0:4(spa): Subtitle: ass
    Metadata:
      BPS-eng         : 121
      DURATION-eng    : 00:23:34.740000000
      NUMBER_OF_FRAMES-eng: 322
      NUMBER_OF_BYTES-eng: 21506
      _STATISTICS_WRITING_APP-eng: mkvmerge v22.0.0 ('At The End Of The World') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2019-06-28 17:22:18
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
  Stream #0:5(por): Subtitle: ass
    Metadata:
      BPS-eng         : 143
      DURATION-eng    : 00:23:39.750000000
      NUMBER_OF_FRAMES-eng: 318
      NUMBER_OF_BYTES-eng: 25453
      _STATISTICS_WRITING_APP-eng: mkvmerge v22.0.0 ('At The End Of The World') 64-bit
      _STATISTICS_WRITING_DATE_UTC-eng: 2019-06-28 17:22:18
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
  Stream #0:6: Attachment: ttf
    Metadata:
      filename        : OpenSans-Semibold.ttf
      mimetype        : application/x-truetype-font
Unsupported codec with id 98304 for input stream 6

在 stackoverflow 上寻找各种解决方案时,我发现这个命令应该有效:

ffmpeg -progress pipe:1 -hide_banner -i "pathsource.mkv" -map 0:0 -map 0:1 -map 0:3 -c:a aac -c:v copy -c:s mov_text "destination.mp4"

或者

ffmpeg -progress pipe:1 -hide_banner -i "pathsource.mkv" -map 0:v -map 0:a -map 0:s -c:a aac -c:v copy -c:s mov_text "destination.mp4"

问题是,当我运行它时,它会执行操作,但最终结果是视频 + 音频,没有字幕。好像他没看到它们。有人能给我一些建议吗?谢谢我想要视频 #0:0、音频 #0:1 和字幕 #0:3。我可以做到吗?谢谢

编辑:

ffprobe 最终结果(字幕不存在)。为什么?

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '483.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf59.8.100
  Duration: 00:24:00.13, start: 0.000000, bitrate: 2672 kb/s
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 2536 kb/s, 23.98 fps, 23.98 tbr, 24k tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](jpn): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]

如果我使用新版本的 ffmpeg,则对同一文件进行 ffprobe

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '481.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf59.17.100
  Duration: 00:24:00.13, start: 0.000000, bitrate: 3203 kb/s
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 3065 kb/s, 23.98 fps, 23.98 tbr, 16k tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](jpn): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 129 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
  Stream #0:2[0x3](ita): Subtitle: mov_text (tx3g / 0x67337874), 0 kb/s (default)
    Metadata:
      handler_name    : SubtitleHandler

然而,即使出现,您也不会在屏幕上看到字幕。

答案1

您可以使用以下方式刻录文本字幕(硬字幕)字幕过滤器

使用“刻录”字幕的优点是无论视频播放器的配置如何,字幕都会显示。

主要的缺点是我们必须重新编码视频。
重新编码会降低质量,并可能导致文件大小变大(文件变大是为了使质量更接近原始质量)。

对于刻录第二字幕流(意大利语),您可以使用pathsource.mkv:si=1
由于意大利语流是默认流,我们不必添加:si=1


添加“刻录”字幕的命令行(示例):

ffmpeg -y -i pathsource.mkv -vf "subtitles=pathsource.mkv:force_style='Fontsize=72,Fontname=Arial Bold,PrimaryColour=&HFFFFFF&'" -acodec copy -vcodec libx264 -crf 17 -pix_fmt yuv444p destination.mp4

  • -crf 17用于高质量,但文件大小较大(设置较高的值可降低质量)。
  • -yuv444用于获得更好的质量(更好的字幕质量,但文件大小更大),但这并不是至关重要的。
  • 您可以尝试各种字体样式(大小、字体、颜色……)。

如果您仍想尝试嵌入式字幕流(无需重新编码),您可以尝试将流设置为“默认”和“强制”:

ffmpeg -y -progress pipe:1 -hide_banner -i "pathsource.mkv" -map 0:0 -map 0:1 -map 0:3 -c:a aac -c:v copy -c:s mov_text -disposition:s default+forced "destination.mp4"

注意:在这种情况下,“强制”选项似乎不起作用。


测试:

使用音频和两个字幕流构建合成视频:

ffmpeg -y -f lavfi -i testsrc=size=384x216:rate=1 -f lavfi -i sine=frequency=400 -f lavfi -i sine=frequency=1000 -sub_charenc CP1252 -i in0.srt -sub_charenc CP1252 -i in1.srt -filter_complex "[1:a][2:a]amix=inputs=2[a]" -map:v 0 -map:a "[a]" -map:s 3 -map:s 4 -vcodec libx264 -crf 17 -pix_fmt yuv420p -acodec aac -ar 22050 -scodec ass -metadata:s:s:0 language=eng -metadata:s:s:1 language=ita -disposition:s:s:1 default+forced -t 10 pathsource.mkv

样本内容in0.srt

1
00:00:00,0 --> 00:00:01,0
Eng 0 to 1

2
00:00:01,0 --> 00:00:02,0
Eng 1 to 2

3
00:00:02,0 --> 00:00:03,0
Eng 2 to 3

4
00:00:03,0 --> 00:00:04,0
Eng 3 to 4

5
00:00:04,0 --> 00:00:05,0
Eng 4 to 5

内容in1.srt

1
00:00:00,0 --> 00:00:01,0
Ita 0 to 1

2
00:00:01,0 --> 00:00:02,0
Ita 1 to 2

3
00:00:02,0 --> 00:00:03,0
Ita 2 to 3

4
00:00:03,0 --> 00:00:04,0
Ita 3 to 4

5
00:00:04,0 --> 00:00:05,0
Ita 4 to 5

执行后结果示例ffmpeg -y -i pathsource.mkv -vf "subtitles=pathsource.mkv:force_style='Fontsize=72,Fontname=Arial Bold,PrimaryColour=&HFFFFFF&'" -acodec copy -vcodec libx264 -crf 17 -pix_fmt yuv444p destination.mp4

在此处输入图片描述


对于嵌入式字幕流,您可能需要修改视频播放器设置。

VLC 播放器示例:

在此处输入图片描述

相关内容