如何使用 ffmpeg 从视频文件中删除不需要的语言?

如何使用 ffmpeg 从视频文件中删除不需要的语言?

我有许多视频文件(500 多个),其中包含我不需要的语言的大量音频和字幕流,因此希望将其删除以节省存储空间。

我对 进行了修改ffmpeg,但是通过处理一个又一个文件来删除流结果是非常耗时。脚本编写也没有运气,因为视频文件包含不同顺序的不同流,这使得通过索引删除变得困难且容易出错。

必须有一个既更快又适用于包含不同流的文件的解决方案,对吧?任何帮助将非常感激。

答案1

您可以使用以下ffmpeg命令行:

ffmpeg -i video.mkv -map 0:v -map 0:m:language:eng -codec copy video_2.mkv

解释:

-i video.mkv             input file (identified as '0:' in mappings below)
-map 0:v                 map video streams from input to output file
-map 0:m:language:eng    map streams for language 'eng' from input to output file
                         (may be specified multiple times for multiple languages)
-codec copy              copy streams without reencoding
video_2.mkv              output file

生成的文件将仅保留英语流(无论如何复制的视频流除外)。

实际上,我不久前甚至为此创建了一个脚本(GitHub Gist:删除不需要的语言.sh):

#!/usr/bin/env bash

# -------------------------------------------------------------------------
#                                                                         -
#  Remove unneeded language(s) from video file                            -
#                                                                         -
#  Created by Fonic <https://github.com/fonic>                            -
#  Date: 04/14/22 - 08/26/22                                              -
#                                                                         -
#  Based on:                                                              -
#  https://www.reddit.com/r/ffmpeg/comments/r3dccd/how_to_use_ffmpeg_to_  -
#  detect_and_delete_all_non/                                             -
#                                                                         -
# -------------------------------------------------------------------------

# Print normal/hilite/good/warn/error message [$*: message]
function printn() { echo -e "$*"; }
function printh() { echo -e "\e[1m$*\e[0m"; }
function printg() { echo -e "\e[1;32m$*\e[0m"; }
function printw() { echo -e "\e[1;33m$*\e[0m" >&2; }
function printe() { echo -e "\e[1;31m$*\e[0m" >&2; }

# Set up error handling
set -ue; trap "printe \"Error: an unhandled error occurred on line \${LINENO}, aborting.\"; exit 1" ERR

# Process command line
if (( $# != 3 )); then
    printn "\e[1mUsage:\e[0m   ${0##*/} LANGUAGES INFILE OUTFILE"
    printn "\e[1mExample:\e[0m ${0##*/} eng,spa video.mkv video_2.mkv"
    printn "\e[1mExample:\e[0m for file in *.mkv; do ${0##*/} eng,spa \"\${file}\" \"out/\${file}\"; done"
    printn "\e[1mNote:\e[0m    LANGUAGES specifies language(s) to KEEP (comma-separated list)"
    exit 2
fi
IFS="," read -a langs -r <<< "$1"
infile="$2"
outfile="$3"

# Sanity checks
[[ -f "${infile}" ]] || { printe "Error: input file '${infile}' does not exist, aborting."; exit 1; }
command -v "ffmpeg" >/dev/null || { printe "Error: required command 'ffmpeg' is not available, aborting."; exit 1; }

# Run ffmpeg
printh "Processing file '${infile}'..."
lang_maps=(); for lang in "${langs[@]}"; do lang_maps+=("-map" "0:m:language:${lang}"); done
ffmpeg -i "${infile}" -map 0:v "${lang_maps[@]}" -codec copy -loglevel warning "${outfile}" || exit $?
exit 0

像这样使用它(例如处理所有.mkv文件并仅保留英语和西班牙语的流):

mkdir out; for file in *.mkv; do remove-unneeded-languages.sh eng,spa "${file}" "out/${file}"; done

相关内容