-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.sh
executable file
·180 lines (148 loc) · 3.33 KB
/
sync.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
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
#!/bin/bash
filename=""
offset=""
direction=""
set -e
function help
{
echo "This script shifts srt subtitle file on a given offset (format hours:minutes:seconds:milliseconds)"
echo ""
echo "Usage: $0 --file <srt_file_path> --offset <offset>" --forward --backward
}
function add_offset
{
local value=$1
local offset_mseconds=$2
local mseconds=$(echo $value | awk -F ":|," '{print (($1 * 60 + $2) * 60 + $3) * 1000 + $4}')
local res_mseconds=$(echo $mseconds $offset_mseconds | awk '{print $1 + $2}')
local is_neg=$(echo $res_mseconds | awk '{print $1 < 0 ? 1 : 0}')
if [ "$is_neg" == "1" ]; then
local new_value="00:00:00,000"
else
local new_value=$(echo $res_mseconds | awk '
{
res=$1;
ms=res%1000;
res=res/1000;
sec=res%60;
res=res/60;
min=res%60;
hrs=res/60;
printf "%02d:%02d:%02d,%03d", hrs, min, sec, ms;
}' )
fi
echo $new_value
}
if [ $# -le 0 ]; then
help
exit 1
fi
while :; do
if [ $# -le 0 ]; then
break
fi
case $1 in
--help)
help
exit 1
;;
--file)
if [ -n "$2" ]; then
filename="$2"
shift
else
echo "ERROR: file name should not be empty"
exit 1
fi
;;
--offset)
if [ -n "$2" ]; then
offset="$2"
shift
else
echo "ERROR: offset should not be empty"
exit 1
fi
;;
--forward)
if [ "$direction" != "" ]; then
echo "ERROR: only single direction could be set up"
exit 1
fi
direction="1"
;;
--backward)
if [ "$direction" != "" ]; then
echo "ERROR: only single direction could be set up"
exit 1
fi
direction="-1"
;;
*)
echo "ERROR: unknown argument"
exit 1
;;
esac
shift
done
if [ "$filename" == "" ]; then
echo "ERROR: empty filename"
exit 1
fi
if [ "$offset" == "" ]; then
echo "ERROR: empty offset"
exit 1
fi
if [ "$direction" == "" ]; then
echo "ERROR: empty direction"
exit 1
fi
output=$(echo "$filename.synced.srt" | sed 's/.srt//')
rm -f $output
echo "Converting dos2unix"
dos2unix $filename &> /dev/null
offset_mseconds=$(echo $offset | awk -F ":" '{print (($1 * 60 + $2) * 60 + $3) * 1000 + $4}')
offset_mseconds=$(echo $offset_mseconds $direction | awk '{print $1 * $2}')
new_index=$((0))
while IFS= read -r line
do
# Record in srt format is next:
#
# <index>
# <timing>
# <actual text #1>
# <actual text #2>
# ...
# empty line
#
# skip empty lines
if [ "$line" == "" ]; then
continue
fi
# save index
index="$line"
# save new timing
IFS= read -r line
start=$(echo $line | awk '{print $1}')
end=$(echo $line | awk '{print $3}')
new_start=$(add_offset $start $offset_mseconds)
new_end=$(add_offset $end $offset_mseconds)
if [ "$new_end" != "00:00:00,000" ]; then
new_index=$((new_index+1))
echo "$new_index" >> $output
echo "$new_start --> $new_end" >> $output
fi
# save actual text
IFS= read -r line
while [ "$line" != "" ]; do
if [ "$new_end" != "00:00:00,000" ]; then
echo "$line" >> $output
fi
IFS= read -r line
done
# save empty line after record
if [ "$new_end" != "00:00:00,000" ]; then
echo "" >> $output
fi
done < "$filename"
echo "INFO: synced $index records. RESULT: $output"