-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo-subtitle
executable file
·204 lines (178 loc) · 6.71 KB
/
video-subtitle
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/video-subtitle
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/video-subtitle
## https://davoudarsalani.ir
source "$HOME"/main/scripts/gb
source "$HOME"/main/scripts/gb-color
trap 'width="$(tput cols)"' SIGWINCH
title="${0##*/}"
function display_help {
source "$HOME"/main/scripts/.help
video_subtitle_help
}
function prompt {
for _ in "$@"; {
case "$1" in
-d )
dir="${dir:-"$(get_input 'directory')"}"
[ -d "$dir" ] || {
red 'no such dir'
exit
} ;;
esac
shift
}
}
function get_opt {
local options
options="$(getopt --longoptions 'help,directory:' --options 'hd:' --alternative -- "$@")"
eval set -- "$options"
while true; do
case "$1" in
-h|--help )
display_help ;;
-d|--directory )
shift
dir="$1" ;;
-- )
break ;;
esac
shift
done
}
get_opt "$@"
heading "$title"
main_items=( "replace ' ', ---, -_-, !, %, & and ," 'convert vtt to srt' 'change srt color to #f3f300' 'burn' 'remove TMPCOMMA and ----BURN' 'remove youtube ID' 'prepend random numbers' 'create 0-list' 'help' )
main_item="$(pipe_to_fzf "${main_items[@]}")" && wrap_fzf_choice "$main_item" || exit 37
case "$main_item" in
"replace ' ', ---, -_-, !, %, & and ," )
replaced_en="$(get_single_input ' have you replaced .en.vtt with .vtt?')" && printf '\n'
[ "$replaced_en" == 'y' ] || exit ## NOTE do NOT remove
prompt -d
printf '\n'
declare -i replaced_count=0
readarray -t files < <(find "$dir" -mindepth 1 -maxdepth 1 | sort)
for file in "${files[@]}"; {
brown "→ $file"
new_name="$(printf '%s\n' "$file" | \
sed -e 's/ /-/g' -e 's/\(---\|-_-\|-–-\|\%\|\!\)//g' -e 's/\&/and/g' -e 's/,/TMPCOMMA/g')"
mv -nv "$file" "$new_name" && accomplished
(( replaced_count++ ))
line
}
printf '\n'
blue "$replaced_count symbols replaced" ;;
'convert vtt to srt' )
prompt -d
printf '\n'
declare -i vtt_count=0
readarray -t vtts < <(find "$dir" -mindepth 1 -maxdepth 1 -iname '*.vtt' | sort)
for vtt in "${vtts[@]}"; {
brown "→ $vtt"
ffmpeg -i "$vtt" "${vtt%.vtt}".srt 2>/dev/null && accomplished
(( vtt_count++ ))
line
}
printf '\n'
blue "$vtt_count vtts converted to srt" ;;
'change srt color to #f3f300' )
prompt -d
printf '\n'
declare -i srt_count=0
readarray -t srts < <(find "$dir" -mindepth 1 -maxdepth 1 -iname '*.srt' | sort)
for srt in "${srts[@]}"; {
brown "→ $srt"
sed -Ei '/(-->|^$|^[0-9][0-9]*$)/!s/^/<font color="#f3f300">/g' "$srt" && accomplished
(( srt_count++ ))
line
}
printf '\n'
blue "$srt_count srts changed to #f3f300" ;;
burn )
prompt -d
printf '\n'
declare -i vid_count=0
readarray -t vids < <(find "$dir" -mindepth 1 -maxdepth 1 -iname '*.mp4' | sort)
for vid in "${vids[@]}"; {
SECONDS=0
already_burnt_count="$(wc -l < <(find "$dir" -mindepth 1 -maxdepth 1 -iname '*----BURN.mp4' | sort))"
brown "→ $vid $(grey "(#"$(( "$already_burnt_count" + 1 ))")")"
HandBrakeCLI -i "$vid" -o "${vid%.mp4}----BURN.mp4" --srt-file "${vid%.mp4}.srt" \
--srt-codeset UTF-8 --srt-lang eng --srt-burn --encoder x264 --audio 1 2>/dev/null && {
accomplished
} || {
red 'burn failed'
}
(( vid_count++ ))
dur="$(convert_second "$SECONDS")"
blue "Video burn duration: $dur"
line
}
printf '\n'
blue "$vid_count videos burned" ;;
'remove TMPCOMMA and ----BURN' )
prompt -d
printf '\n'
declare -i vids_count=0
readarray -t rename_vids < <(find "$dir" -mindepth 1 -maxdepth 1 -iname '*.mp4' | sort)
for rename_vid in "${rename_vids[@]}"; {
brown "→ $rename_vid"
new_name="$(printf '%s\n' "$rename_vid" | sed -e 's/TMPCOMMA/,/g;s/----BURN//g')"
purple "$(mv -nv "$rename_vid" "$new_name")" && accomplished
(( vids_count++ ))
line
}
printf '\n'
blue "$vids_count videos removed TMPCOMMA and ----BURN" ;;
'remove youtube ID' )
prompt -d
printf '\n'
declare -i burned_vid_count=0
readarray -t burned_vids < <(find "$dir" -mindepth 1 -maxdepth 1 -iname '*.mp4' | sort)
for burned_vid in "${burned_vids[@]}"; {
brown "→ $burned_vid"
new_name="$(printf '%s\n' "$burned_vid" | sed -e 's/.\{13\}mp4$/.mp4/')"
purple "$(mv -nv "$burned_vid" "$new_name")" && accomplished
(( burned_vid_count++ ))
line
}
printf '\n'
blue "$burned_vid_count youtube IDs removed" ;;
'prepend random numbers' )
declare -a rand_list
prompt -d
printf '\n'
readarray -t rename_vids < <(find "$dir" -mindepth 1 -maxdepth 1 -iname '*.mp4' | sort)
number_of_vids="${#rename_vids[@]}"
## create list and rand_list
readarray -t list < <(seq "$number_of_vids")
list_length="${#list[@]}"
while [ "${list[0]}" ]; do ## while array is full. no need to [@]
(( rand_index="RANDOM % list_length" ))
rand_member="${list[$rand_index]}"
rand_list+=( "$rand_member" )
unset "list[$rand_index]"
readarray -t list < <(printf '%s\n' "${list[@]}")
list_length="${#list[@]}"
done
## prepend prefixes
for ((i=0; i<"$number_of_vids"; i++)); {
cur_vid="${rename_vids[$i]}"
brown "→ $cur_vid"
root="${cur_vid%/*}"
base="${cur_vid##*/}"
prefix="$(printf '%03d' "${rand_list[$i]}")"
purple "$(mv -nv "$cur_vid" "${root}/${prefix}-${base}")" && accomplished
line
}
printf '\n'
blue "$i videos renamed randomly" ;;
'create 0-list' )
prompt -d
find "$dir" -mindepth 1 -maxdepth 1 -iname '*.mp4' | sort | \
sed -e 's/---/TMPPATTERN/g;s/--/TMPPATTERN/g;s/-/ /g;s/TMPPATTERN/ - /g' -e 's/\.mp4$//g' -e 's/^.\+\///g' > "$dir"/0-list.txt && accomplished ;;
help )
display_help ;;
esac