使用ffmpeg导出视频字幕以及给视频加字幕


确保已下载ffmpeg软件,可以在命令行运行ffmpeg命令。

ffmpeg version N-113561-ge05d3c1a16-20240207 Copyright (c) 2000-2024 the FFmpeg developers
  built with gcc 13.2.0 (crosstool-NG 1.25.0.232_c175b21)
  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 --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libharfbuzz --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --enable-chromaprint --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libkvazaar --enable-libaribcaption --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libvpl --enable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --enable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --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=20240207
  libavutil      58. 36.101 / 58. 36.101
  libavcodec     60. 39.100 / 60. 39.100
  libavformat    60. 20.100 / 60. 20.100
  libavdevice    60.  4.100 / 60.  4.100
  libavfilter     9. 17.100 /  9. 17.100
  libswscale      7.  6.100 /  7.  6.100
  libswresample   4. 13.100 /  4. 13.100
  libpostproc    57.  4.100 / 57.  4.100
Universal media converter
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

用json的形式显示c.mkv视频的所有字幕信息

ffprobe -v error -select_streams s -show_entries stream=index:stream_tags=language,title -of json c.mkv
{
    "programs": [

    ],
    "streams": [
        {
            "index": 2,
            "tags": {
                "language": "eng",
                "title": "Forced"
            }
        },
        {
            "index": 3,
            "tags": {
                "language": "eng"
            }
        },
        {
            "index": 4,
            "tags": {
                "language": "eng",
                "title": "SDH"
            }
        },
        {
            "index": 5,
            "tags": {
                "language": "ara"
            }
        },
        {
            "index": 6,
            "tags": {
                "language": "bul"
            }
        },
        {
            "index": 7,
            "tags": {
                "language": "chi",
                "title": "Chinese (Simplified)"
            }
        },
        {
            "index": 8,
            "tags": {
                "language": "chi",
                "title": "Chinese (Traditional)"
            }
        },
        {
            "index": 9,
            "tags": {
                "language": "cze"
            }
        },
        {
            "index": 10,
            "tags": {
                "language": "dan"
            }
        },
        {
            "index": 11,
            "tags": {
                "language": "ger"
            }
        },
        {
            "index": 12,
            "tags": {
                "language": "gre"
            }
        },
        {
            "index": 13,
            "tags": {
                "language": "spa",
                "title": "Spanish (Latin America)"
            }
        },
        {
            "index": 14,
            "tags": {
                "language": "spa",
                "title": "Spanish (Spain)"
            }
        },
        {
            "index": 15,
            "tags": {
                "language": "est"
            }
        },
        {
            "index": 16,
            "tags": {
                "language": "fin"
            }
        },
        {
            "index": 17,
            "tags": {
                "language": "fre",
                "title": "French (Canada)"
            }
        },
        {
            "index": 18,
            "tags": {
                "language": "fre",
                "title": "French (France)"
            }
        },
        {
            "index": 19,
            "tags": {
                "language": "heb"
            }
        },
        {
            "index": 20,
            "tags": {
                "language": "hin"
            }
        },
        {
            "index": 21,
            "tags": {
                "language": "hun"
            }
        },
        {
            "index": 22,
            "tags": {
                "language": "ind"
            }
        },
        {
            "index": 23,
            "tags": {
                "language": "ita"
            }
        },
        {
            "index": 24,
            "tags": {
                "language": "jpn"
            }
        },
        {
            "index": 25,
            "tags": {
                "language": "kor"
            }
        },
        {
            "index": 26,
            "tags": {
                "language": "lit"
            }
        },
        {
            "index": 27,
            "tags": {
                "language": "lav"
            }
        },
        {
            "index": 28,
            "tags": {
                "language": "may"
            }
        },
        {
            "index": 29,
            "tags": {
                "language": "dut"
            }
        },
        {
            "index": 30,
            "tags": {
                "language": "nor"
            }
        },
        {
            "index": 31,
            "tags": {
                "language": "pol"
            }
        },
        {
            "index": 32,
            "tags": {
                "language": "por",
                "title": "Portuguese"
            }
        },
        {
            "index": 33,
            "tags": {
                "language": "por",
                "title": "Portuguese (Brazil)"
            }
        },
        {
            "index": 34,
            "tags": {
                "language": "rus"
            }
        },
        {
            "index": 35,
            "tags": {
                "language": "slo"
            }
        },
        {
            "index": 36,
            "tags": {
                "language": "slv"
            }
        },
        {
            "index": 37,
            "tags": {
                "language": "swe"
            }
        },
        {
            "index": 38,
            "tags": {
                "language": "tam"
            }
        },
        {
            "index": 39,
            "tags": {
                "language": "tel"
            }
        },
        {
            "index": 40,
            "tags": {
                "language": "tha"
            }
        },
        {
            "index": 41,
            "tags": {
                "language": "tur"
            }
        },
        {
            "index": 42,
            "tags": {
                "language": "ukr"
            }
        },
        {
            "index": 43,
            "tags": {
                "language": "vie"
            }
        },
        {
            "index": 44,
            "tags": {
                "language": "chi",
                "title": "Chinese (Traditional)"
            }
        }
    ]
}

导出指定语言的字幕文件

ffmpeg -i c.mkv -map "0:44" output.chi.srt

0:44中的44为上方字幕信息中的index数值。

output.chi.srt为导出后的字幕文件名。

将指定字幕文件加入到c.mkv视频上,并且转为mp4格式

ffmpeg -i c.mkv -vf "subtitles=input.srt" output.mp4

input.srt为指定的字幕文件。

output.mp4为转换后的视频文件名称。