summaryrefslogtreecommitdiff
path: root/bin/srtrename
diff options
context:
space:
mode:
authornavewindre <boneyaard@gmail.com>2025-07-15 07:23:39 +0200
committernavewindre <boneyaard@gmail.com>2025-07-15 07:23:39 +0200
commit4e00800a88e917a9cd37cf8aa9a64252927c7c9b (patch)
tree56b996432b123915ee2acb7be4a38e7d473dd337 /bin/srtrename
parent048c29db28039510bc069bf20e1311fd45899173 (diff)
a
Diffstat (limited to 'bin/srtrename')
-rwxr-xr-xbin/srtrename62
1 files changed, 25 insertions, 37 deletions
diff --git a/bin/srtrename b/bin/srtrename
index 33166e8..535f4eb 100755
--- a/bin/srtrename
+++ b/bin/srtrename
@@ -1,42 +1,30 @@
#!/bin/bash
-# Enable debug output
-set -x
-
-# Loop through all subtitle files
for srt in *.srt; do
- # Skip if no .srt files found
- [[ -f "$srt" ]] || { echo "No .srt files found"; exit 1; }
-
- echo "Processing subtitle file: $srt"
-
- # Extract the episode number (S01E04) - more specific pattern
- episode=$(echo "$srt" | grep -o "S[0-9]\{2\}E[0-9]\{2\}")
- echo "Extracted episode number: $episode"
-
- # Find the corresponding video file
- video_file=$(find . -maxdepth 1 -type f -name "*${episode}*.mkv")
- echo "Found video file: $video_file"
-
- # If a matching video file is found
- if [[ -n "$video_file" ]]; then
- # Remove the .mkv extension and leading ./
- video_basename=$(basename "$video_file" .mkv)
- # Create new subtitle name
- new_name="${video_basename}.srt"
-
- echo "Will rename '$srt' to '$new_name'"
-
- # Only rename if the new name is different
- if [[ "$srt" != "$new_name" ]]; then
- mv -v "$srt" "$new_name"
- else
- echo "File already has the correct name"
- fi
- else
- echo "No matching video file found for '$srt'"
- # List all mkv files for debugging
- echo "Available mkv files:"
- ls -l *.mkv
+ [[ -f "$srt" ]] || { echo "no .srt files found"; exit 1; }
+
+ episode=$(echo "$srt" | grep -o "S[0-9]\{2\}E[0-9]\{2\}")
+ if [[ -z "$episode" ]]; then
+ echo "no episode number found in '$srt'"
+ exit
+ fi
+
+ video_file=$(find . -maxdepth 1 -type f -name "*${episode}*.mkv")
+ if [[ $video_file == '' ]] then
+ epnumber=$(echo "$episode" | head -c6 | tail -c2)
+ echo "no matching video file found for '$episode', trying ep number $epnumber..."
+ video_file=$(find . -maxdepth 1 -type f -name "* ${epnumber} *.mkv")
+ fi
+
+ if [[ -n "$video_file" ]]; then
+ echo "found video file: $video_file"
+ video_basename=$(basename "$video_file" .mkv)
+ new_name="${video_basename}.srt"
+
+ if [[ "$srt" != "$new_name" ]]; then
+ mv -v "$srt" "$new_name"
fi
+ else
+ echo "no matching video file found for '$srt'"
+ fi
done