Skip to content

Instantly share code, notes, and snippets.

@cobanov
Created April 27, 2026 09:46
Show Gist options
  • Select an option

  • Save cobanov/8f15889ea7199b355bce4f7afb9defae to your computer and use it in GitHub Desktop.

Select an option

Save cobanov/8f15889ea7199b355bce4f7afb9defae to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# fix-subs: SRT dosyalarini UTF-8'e cevirir ve video dosyalariyla esler
# Kullanim: cd /klasor/yolu && fix-subs
# veya: fix-subs /klasor/yolu
set -e
target_dir="${1:-.}"
cd "$target_dir"
echo "==> Klasor: $(pwd)"
# 1) Encoding donusumu
if ls *.srt &>/dev/null; then
mkdir -p backup
echo "==> SRT dosyalari UTF-8'e ceviriliyor (yedek: ./backup/)"
for f in *.srt; do
if [[ ! -f "backup/$f" ]]; then
cp "$f" "backup/$f"
fi
iconv -f WINDOWS-1254 -t UTF-8 "backup/$f" > "$f" 2>/dev/null || {
echo " ! $f cevrilemedi, atlandi"
cp "backup/$f" "$f"
continue
}
echo "$f"
done
else
echo "==> SRT dosyasi bulunamadi, encoding adimi atlandi"
fi
# 2) Isim eslestirme: SxxEyy pattern'ine gore
videos=(*.mkv(N) *.mp4(N) *.avi(N))
subs=(*.srt(N))
if (( ${#videos[@]} == 0 || ${#subs[@]} == 0 )); then
echo "==> Video ya da altyazi yok, eslestirme atlandi"
exit 0
fi
echo "==> Altyazi isimleri SxxEyy pattern'ine gore esitleniyor"
matched=0
unmatched_subs=()
unmatched_videos=()
# Her altyaziyi episode kodu ile videoya eslestir
typeset -A video_map
for v in "${videos[@]}"; do
if [[ "$v" =~ '[Ss]([0-9]{2})[Ee]([0-9]{2})' ]]; then
key="S${match[1]}E${match[2]}"
video_map[$key]="$v"
fi
done
for s in "${subs[@]}"; do
if [[ "$s" =~ '[Ss]([0-9]{2})[Ee]([0-9]{2})' ]]; then
key="S${match[1]}E${match[2]}"
if [[ -n "${video_map[$key]}" ]]; then
base="${video_map[$key]:r}"
new_name="${base}.srt"
if [[ "$s" != "$new_name" ]]; then
mv "$s" "$new_name"
echo "$s$new_name"
else
echo " = $s (zaten eslesmis)"
fi
((matched++))
unset "video_map[$key]"
else
unmatched_subs+=("$s")
fi
else
unmatched_subs+=("$s")
fi
done
# Geride kalan videolar = altyazisi olmayanlar
for key in ${(k)video_map}; do
unmatched_videos+=("${video_map[$key]}")
done
echo ""
echo "==> Ozet: $matched eslesme"
if (( ${#unmatched_subs[@]} > 0 )); then
echo ""
echo "Bu altyazilar eslesmedi:"
for s in "${unmatched_subs[@]}"; do
echo " - $s"
done
fi
if (( ${#unmatched_videos[@]} > 0 )); then
echo ""
echo "Bu videolarin altyazisi yok:"
for v in "${unmatched_videos[@]}"; do
echo " - $v"
done
fi
echo ""
echo "==> Bitti."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment