当不存在字幕时,FFMpeg 会出错

当不存在字幕时,FFMpeg 会出错

我正在尝试使用 ffmpeg 将 .mkv 转换为 .mp4,并且在转换过程中,我还尝试刻录字幕(如果存在)。这是命令行。这是一个脚本:

$PGMNAME -i "$1" -s hd720 -c:v libx264 -crf 23 -c:a copy -strict -2 -vf subtitles="$1"  "$BASEPATH/$FILENAME.mp4"

"$1"原始文件是 .mkv 文件,包含(或不包含,这是问题所在)字幕。使用上述操作,我收到以下错误:

Unable to locate subtitle stream file.mkv

Error initializing filter 'subtitles'

这些是通过无线方式下载的电视节目。不知道哪些节目包含字幕。我该如何重写命令以避免出现错误Unable to locate subtitle stream file.mkv

答案1

用于ffprobe确定是否有字幕流。如果有,则使用字幕筛选。

ffprobe -loglevel error -select_streams s:0 -show_entries stream=codec_type -of csv=p=0 input.mkv
  • 如果有字幕就会输出subtitle
  • 如果没有字幕则不会输出任何内容。

相关内容