如何用FFmpeg批量提取MKV文件中的印尼SRT字幕?
Got it, let's tackle this batch subtitle extraction problem. Since you already know how to pull Indonesian subtitles from a single file with FFmpeg, scaling this to your 17 files in the dots folder is straightforward with a bash loop. Here are two reliable approaches:
方法1:基于语言代码的批量提取(推荐)
This method is more robust because it targets subtitles by their language code (ind for Indonesian) instead of a fixed stream index—stream numbers can vary between different MKV files, so this avoids errors if some files have different stream layouts.
First, navigate to your
dotsfolder:cd /home/jaac/torrents/rtorrent/dotsRun this bash loop to process all MKV files:
for file in *.mkv; do ffmpeg -i "$file" -map 0:s:m:language:ind "${file%.mkv}_indo.srt" -y done
Let's break down the key parts:
"$file": Quotes ensure the command handles filenames with spaces or special characters correctly.-map 0:s:m:language:ind: Tells FFmpeg to pick the subtitle stream (s) from the input file (0) that matches (m) the Indonesian language codeind.${file%.mkv}_indo.srt: Creates a subtitle filename that matches the original MKV (e.g.,Title.NF.WEB-DL.DDP2.0.x264-Ao.mkvbecomesTitle.NF.WEB-DL.DDP2.0.x264-Ao_indo.srt).-y: Automatically overwrites existing.srtfiles if they exist. Remove this flag if you want FFmpeg to prompt you before overwriting.
方法2:固定流索引的批量提取(仅当所有文件的印尼语字幕都是第7个流)
If you're absolutely certain every MKV in your folder has the Indonesian subtitle at stream index 0:7, you can reuse your original command in a loop:
cd /home/jaac/torrents/rtorrent/dots for file in *.mkv; do ffmpeg -i "$file" -map 0:7 "${file%.mkv}_indo.srt" -y done
额外小贴士:验证字幕流信息
Before running the batch command, you can double-check the subtitle streams in any file to confirm the language or index. Use ffprobe (included with FFmpeg) for this:
ffprobe -v error -select_streams s -show_entries stream=index:stream_tags=language -of csv=p=0 "YourFile.mkv"
This will output something like:
5,eng 7,ind
Which confirms stream 7 is Indonesian (ind), so your original single-file command works for that specific file.
内容的提问来源于stack exchange,提问作者jaac

