-
Notifications
You must be signed in to change notification settings - Fork 0
/
arabic_converter.sh
51 lines (42 loc) · 1.22 KB
/
arabic_converter.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
#!/bin/bash
set -euo pipefail
# Function to print usage
function usage() {
echo "Usage: $0 <folder_path>"
echo "e.g. $0 /Movies/MyMovie"
exit 1
}
# Check that the user has provided the correct number of arguments
if [ $# -lt 1 ]; then
usage
fi
FOLDER_PATH=$1 # e.g. /Movies/MyMovie
# 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
}
# Convert all files in a folder to UTF-8 encoding
# e.g. WINDOWS-1256 -> UTF-8
function subtitle_convert() {
check_input
echo "Converting all files in '$FOLDER_PATH' to UTF-8 encoding"
find "$FOLDER_PATH" -type f -print0 | while IFS= read -r -d '' file; do
if [[ $file == *.srt ]]; then
charset="$(file -bi "$file" | awk -F "=" '{print $2}')"
if [ "$charset" != utf-8 ]; then
iconv -cf WINDOWS-1256 -t utf-8 "$file" >"$file.new" &&
mv -f "$file.new" "$file" &&
echo "$file"
fi
fi
done
exit 0
}
subtitle_convert