使用ffmpeg为视频添加SRT字幕

发布于 2024-12-01 19:53:04 字数 1559 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最笨的告白 2024-12-08 19:53:04

MP4 容器不支持 SubRip 字幕,仅支持特殊的 mov_text 格式。

选项 1:将字幕转换为 mov_text

ffmpeg -i hifi.avi -i hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k -scodec mov_text hifi.m4v

选项 2:使用 MKV 容器

iPod 不支持此功能,但在 iPod touch 上,您可以安装第三方软件来播放 MKV 文件。

ffmpeg -i hifi.avi -i hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k -scodec copy hifi.mkv

选项 3:将字幕刻录到视频中

可能需要执行其他步骤才能工作,特别是如果您使用的是 Windows。有关详细信息,请参阅 https://trac.ffmpeg.org/wiki/HowToBurnSubtitlesIntoVideo

ffmpeg -i hifi.avi -vf subtitles=hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k hifi.m4v

The MP4 container does not support SubRip subtitles, only a special mov_text format.

Option 1: Convert subtitles to mov_text

ffmpeg -i hifi.avi -i hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k -scodec mov_text hifi.m4v

Option 2: Use an MKV container

iPod will not support this, however on iPod touch you can install third party software to play MKV files.

ffmpeg -i hifi.avi -i hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k -scodec copy hifi.mkv

Option 3: Burn in the subtitles to the video

May require additional steps to work, especially if you're on Windows. See https://trac.ffmpeg.org/wiki/HowToBurnSubtitlesIntoVideo for more information.

ffmpeg -i hifi.avi -vf subtitles=hifi.srt -acodec libfaac -ar 48000 -ab 128k -ac 2 -vcodec libx264 -vpre ipod640 -s 480x240 -b 256k hifi.m4v
故人爱我别走 2024-12-08 19:53:04

这是在 Windows 环境中使用 FFmpeg 将 srt 文件添加到视频的命令

ffmpeg -i Video.wmv -i hi.srt -map 0 -map 1 -c copy -crf 23 video-with-subtitles.mkv

Here is the command to Add srt file to video using FFmpeg in Windows Environment

ffmpeg -i Video.wmv -i hi.srt -map 0 -map 1 -c copy -crf 23 video-with-subtitles.mkv
我很OK 2024-12-08 19:53:04

似乎正在尝试添加两个字幕轨道,其中之一没有编解码器。最新版本的 ffmpeg 不使用 -newsubtitle,因此这可能是您的问题。

尝试去掉-newsubtitle

It appears that two subtitle tracks are trying to be added, one of which has no codec. More recent versions of ffmpeg do not use -newsubtitle, so that might be your problem.

Try leaving off -newsubtitle.

对风讲故事 2024-12-08 19:53:04

首先在你的 Linux 中安装“libass”库。之后使用以下命令配置 ffmpeg

./configure --enable-libass --disable-yasm --enable-libfreetype 

然后将 srt 复制到 ffmpeg 目录中然后使用这些命令

ffmpeg -i subtitle.srt subtitle.ass
ffmpeg -i input.flv -vf ass=subtitle.ass output.flv

very first install 'libass' library in your linux. after that configure ffmpeg with the command

./configure --enable-libass --disable-yasm --enable-libfreetype 

after that copy your srt in the ffmpeg directory then use these command

ffmpeg -i subtitle.srt subtitle.ass
ffmpeg -i input.flv -vf ass=subtitle.ass output.flv
滥情稳全场 2024-12-08 19:53:04

批处理文件。

只需同时放入 2 个文件 - .mp4 和 .srt 即可获得带有嵌入字幕的 .mkv 视频。

如果您想要原始/低视频质量,只需注释/取消注释代码中的相应行即可。

除此之外,我还对 msgBox 进行了异步调用。谁需要它 - 将其复制到您的来源。

由于某种原因,它不接受文件名中的括号 ()。我没有检查其余的字符。

    @echo off
    if "%~x1"==".mp4" (
        set mp4=%~n1
        set path=%~d1%~p1
    )
    if "%~x2"==".mp4" (
        set mp4=%~n2
        set path=%~d1%~p1
    )
    if "%~x1"==".srt" set srt=%~1
    if "%~x2"==".srt" set srt=%~2
    if not "%mp4%"=="" (
        if not "%srt%"=="" (
            :: If your original ffmpeg.exe file located in system32 directory then replace "%~dp0ffmpeg.exe" to ffmpeg
            :: original quality
            ::"%~dp0ffmpeg.exe" -i "%path%%mp4%.mp4" -i "%srt%" -ar 48000 -vcodec libx264 -disposition:s:0 default -scodec copy "%path%%mp4%.mkv"
            :: low quality
            "%~dp0ffmpeg.exe" -i "%path%%mp4%.mp4" -i "%srt%" -ar 48000 -ab 128k -ac 2 -vcodec libx264 -s 480x240 -b 256k -disposition:s:0 default -scodec copy "%path%%mp4%.mkv"
            call :msgbox "File %path%%mp4%.mkv completed."
        ) else call :msgbox "File .srt missing in the input parameters`r`nJust drop 2 files with extensions:`r`n.mp4 - video and .srt - subtitles`r`nto this batch file to get .mkv with subtitles"
    ) else call :msgbox "File .mp4 missing in the input parameters`r`nJust drop 2 files with extensions:`r`n.mp4 - video and .srt - subtitles`r`nto this batch file to get .mkv with subtitles"
    goto :eof
    :msgBox [msgText]
    C:\Windows\System32\mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -Command """"[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')|Out-Null;"""" """"[System.Windows.Forms.MessageBox]::Show(\""""%~1\"""")"""""",0,false:close")
    exit /B

Batch file.

Just drop 2 files on it at the same time - .mp4 and .srt to get a .mkv video with embedded subtitles.

If you want the original/low video quality, just comment/uncomment the appropriate line in the code.

Among all other things, I made an asynchronous call of msgBox. Who needs it - copy it to your sources.

For some reason, it doesn't accept parentheses () in file names. I did not check the rest of the characters.

    @echo off
    if "%~x1"==".mp4" (
        set mp4=%~n1
        set path=%~d1%~p1
    )
    if "%~x2"==".mp4" (
        set mp4=%~n2
        set path=%~d1%~p1
    )
    if "%~x1"==".srt" set srt=%~1
    if "%~x2"==".srt" set srt=%~2
    if not "%mp4%"=="" (
        if not "%srt%"=="" (
            :: If your original ffmpeg.exe file located in system32 directory then replace "%~dp0ffmpeg.exe" to ffmpeg
            :: original quality
            ::"%~dp0ffmpeg.exe" -i "%path%%mp4%.mp4" -i "%srt%" -ar 48000 -vcodec libx264 -disposition:s:0 default -scodec copy "%path%%mp4%.mkv"
            :: low quality
            "%~dp0ffmpeg.exe" -i "%path%%mp4%.mp4" -i "%srt%" -ar 48000 -ab 128k -ac 2 -vcodec libx264 -s 480x240 -b 256k -disposition:s:0 default -scodec copy "%path%%mp4%.mkv"
            call :msgbox "File %path%%mp4%.mkv completed."
        ) else call :msgbox "File .srt missing in the input parameters`r`nJust drop 2 files with extensions:`r`n.mp4 - video and .srt - subtitles`r`nto this batch file to get .mkv with subtitles"
    ) else call :msgbox "File .mp4 missing in the input parameters`r`nJust drop 2 files with extensions:`r`n.mp4 - video and .srt - subtitles`r`nto this batch file to get .mkv with subtitles"
    goto :eof
    :msgBox [msgText]
    C:\Windows\System32\mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -Command """"[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')|Out-Null;"""" """"[System.Windows.Forms.MessageBox]::Show(\""""%~1\"""")"""""",0,false:close")
    exit /B
无边思念无边月 2024-12-08 19:53:04

更好的

ffmpeg -i infile.mp4 -f srt -i infile.srt -c:v copy -c:a copy -c:s mov_text outfile.mp4

Better

ffmpeg -i infile.mp4 -f srt -i infile.srt -c:v copy -c:a copy -c:s mov_text outfile.mp4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文