Skip to content

Instantly share code, notes, and snippets.

@gnehs
Last active March 22, 2024 21:55
Show Gist options
  • Save gnehs/a9520a37ff5429c3b576d6f9663a5568 to your computer and use it in GitHub Desktop.
Save gnehs/a9520a37ff5429c3b576d6f9663a5568 to your computer and use it in GitHub Desktop.
抽取 MKV 字幕檔並轉為繁體
#!/bin/sh
echo "------------------------"
echo " 字幕轉轉醬"
echo " 抽取資料夾下的影片並輸出字幕檔案"
echo "------------------------"
echo " 字幕轉轉醬使用了繁化姬的 API 服務"
echo " https://zhconvert.org/"
echo "------------------------"
echo " "
read -p "❓ 輸入資料夾位置:" videodir
read -p "❓ 要將字幕繁化ㄇ[y/n]:" zhconvert
#echo "⭐ 正在抽取該資料夾下影片:$videodir"
cd "${videodir}"
#讀取 MKV
subtitleid=""
for i in *.[mM][kK][vV]; do
#若目錄內無 .mkv 檔案 $i 會傳回 "*.[mM][kK][vV]"
if [ "$i" != "*.[mM][kK][vV]" ]; then
if test -z "$subtitleid"; then
echo "⭐ 字幕軌清單:"
ffprobe -loglevel error -select_streams s -show_entries stream=index:stream_tags=title -of csv=p=0 "$i"
read -p "❓ 請選擇字幕軌:" subtitleid
fi
#取得副檔名
subname="`echo $i | awk -F '.' '{print $NF}'`"
#取得主檔名
filename="`echo $i | sed -e s/\.${subname}$//`"
#抽取字幕檔案
ffmpeg -loglevel error -i "$i" -map 0:"$subtitleid" "${videodir}/${filename}.ass"
#繁體化字幕
if [ "$zhconvert" = "y" ];then
echo "⭐ 正在繁化 ${filename}.ass"
subtext=$(<${videodir}/${filename}.ass)
zh=$(curl -g -X POST "https://api.zhconvert.org/convert" --data-urlencode "text=${subtext}" --data-urlencode "converter=Taiwan" --http1.1| jq '.data.text')
echo "$zh" > "${videodir}/${filename}.ass"
#echo "⭐️ 已繁化 ${filename}.ass"
fi
#若執行成功
if [ $? -eq 0 ]; then
echo "🎉 ${filename}.ass 已處理完畢"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment