-
Notifications
You must be signed in to change notification settings - Fork 0
/
convsubtitler.bash
executable file
·121 lines (98 loc) · 2.7 KB
/
convsubtitler.bash
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# Author: MichalM.Mac
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Config defaults
ENCODING_FROM='CP1250'
ENCODING_TO='UTF-8'
SUB_EXTENSION='.srt'
SILENT='false'
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print_error() {
echo "$@" 1>&2;
}
function usage() {
cat <<EOF
convsubtitler: Converts subtitle files to different encoding using iconv
convsubtitler.bash [ -s ] [ -s ENCODING_FROM ] [ -t ENCODING_TO ] [ -e SUB_EXTENSION ] file|directory...
-h
Show this help.
-s
Silent mode. Do not print any output related to subtitle conversion.
-f ENCODING_FROM
Current subtitle encoding. See man iconv_open for list of valid encodings.
Default is 'CP1250'.
-t ENCODING_TO
Subtitle files will be converted to this encoding. See man iconv_open for list of valid encodings.
Default is 'UTF-8'.
-e SUB_EXTENSION
File extension of subtitle files to be found when searching directory provided as an argument.
Default is '.srt'.
EOF
}
function parse_opts() {
while getopts "hf:t:se:" opt; do
case $opt in
h)
usage
exit 0
;;
f)
ENCODING_FROM="${OPTARG%/}"
;;
t)
ENCODING_TO="${OPTARG%/}"
;;
s)
SILENT="true"
;;
\?)
print_error "Invalid option: -$OPTARG"
quit
;;
:)
print_error "Option -$OPTARG requires an argument."
exit 1
;;
esac
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
convert_sub_encoding(){
if file --mime-encoding "$1" | grep -iq "$ENCODING_TO"; then
[ "$SILENT" == "true" ] ||
print_error "Error: File $1 is alredy encoded in $ENCODING_TO"
else
mv "$1" "${1}_temp"
iconv -f "$ENCODING_FROM" -t "$ENCODING_TO" "${1}_temp" > "$1" && rm "${1}_temp"
[ "$SILENT" == "true" ] ||
echo "Converted file $1 encoding from ${ENCODING_FROM} to ${ENCODING_TO}"
fi
}
find_subs(){
find "$1" -name "*${SUB_EXTENSION}" |
while read file; do
convert_sub_encoding "$file"
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function main() {
# Process input
parse_opts "$@"
shift "$((OPTIND-1))"
if [ $# -eq 0 ]; then
echo "Error: No Aruments specified -> Nothing to do."
fi
# Convert subtitle files
while [ $# -gt 0 ]
do
if [ -d "$1" ]; then
find_subs "$1"
elif [ -f "$1" ]; then
convert_sub_encoding "$1"
else
print_error "Error: $1 is not valid file or directory"
fi
shift
done
}
main "$@"