You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何删除视频目录中多余SRT字幕,保留独立非英文字幕文件?

Fixing Subtitle Cleanup: Preserve Single Non-English Subs When No English Exists

Your original command works too broadly—it deletes every non-English subtitle across all directories, regardless of whether that directory has an English .en.srt file or not. To match your exact rules, we need to handle each directory individually, checking for the presence of .en.srt first before deciding what to delete.

Here's a targeted solution using find and a small bash script to process each directory:

find /share/CACHEDEV4_DATA/Movies -type f -name "*.srt" -execdir bash -c '
# Ensure extended globbing is enabled for pattern matching
shopt -s extglob

# Check if this directory contains any English subtitles
if compgen -G "*.en.srt" > /dev/null; then
    # Delete all .srt files EXCEPT .en.srt
    rm -v !(*.en.srt)
else
    # Count how many non-English subtitles exist here
    total_subs=$(ls -1 *.srt | wc -l)
    if [ "$total_subs" -gt 1 ]; then
        # Warn about multiple non-English subs (you can adjust this to auto-delete extras if needed)
        echo "⚠️  Multiple non-English subtitles found in $(pwd) - review or clean manually."
        # Uncomment below to keep the first subtitle and delete the rest
        # ls -1 *.srt | tail -n +2 | xargs rm -v
    fi
fi
' \;

How This Works:

  • Directory-specific processing: The -execdir flag runs the bash script directly in each directory containing .srt files, so we evaluate each folder's subtitle situation independently.
  • Check for English subs: compgen -G "*.en.srt" silently checks if any .en.srt files exist (avoids messy error output from ls).
  • Delete non-English subs when English exists: Using !(*.en.srt) (with extended globbing) targets all .srt files except the English ones for deletion.
  • Preserve single non-English subs: If no .en.srt is found, we count the subtitles. If only one exists, we leave it alone. If multiple are present, we warn you (you can uncomment the line to auto-delete extras if that's what you want).

Why Your Original Command Failed:

Your original find command operates globally—it looks for every .srt that isn't .en.srt across your entire Movies directory, and deletes them. It doesn't care if a directory has no English subtitles at all, so it would delete valid single non-English subs that you wanted to keep.

Notes:

  • If your system doesn't have extended globbing enabled by default, the shopt -s extglob line ensures it's turned on for the script.
  • The warning for multiple non-English subs is optional—if you want to automatically keep one (e.g., the first one listed) and delete the rest, uncomment the line with tail -n +2.

内容的提问来源于stack exchange,提问作者R David Paine III

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:34:47