-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_subtitle.sh
59 lines (48 loc) · 1.39 KB
/
rename_subtitle.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
set -euo pipefail
# Function to print usage
function usage() {
echo "Usage: $0 <language> <folder_path>"
echo "e.g. $0 ar /Movies/MyMovie"
exit 1
}
# Check that the user has provided the correct number of arguments
if [ $# -lt 2 ]; then
usage
fi
LANGUAGE=$1 # e.g. ar
FOLDER_PATH=$2 # e.g. /Movies/MyMovie
# Echo the command and then run it
function echo_then_run() {
echo "$@"
"$@"
}
# Check that the user has provided the correct arguments
function check_input() {
if [[ -z "$FOLDER_PATH" ]]; then
echo "Please provide a folder path as an argument"
exit 1
fi
if [[ ! -d "$FOLDER_PATH" ]]; then
echo "Folder does not exist"
exit 1
fi
if [[ -z "$LANGUAGE" ]]; then
echo "Please provide a language code as an argument"
exit 1
fi
}
# Rename all files in a folder that do not contain a language code in their name to include the language code
# e.g. MyMovie.srt -> MyMovie.ar.srt
function check_rename() {
check_input
echo "Renaming all files that do not contain '$LANGUAGE' in their name"
SUFFIX="$LANGUAGE"
find "$FOLDER_PATH" \( -name "*.srt" -o -name "*.ass" \) -type f | while read -r f; do
if [[ $f != *"$LANGUAGE".srt && $f != *"$LANGUAGE".ass ]] && [[ -f $f ]]; then
echo_then_run mv -- "$f" "${f%.*}.$SUFFIX.${f##*.}"
fi
done
exit 0
}
check_rename