-
Notifications
You must be signed in to change notification settings - Fork 1
/
srtran
executable file
·82 lines (70 loc) · 1.72 KB
/
srtran
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
#!/usr/bin/awk -f
#
# NAME
#
# srtran - linearly transform timestamps in SubRip (SRT) subtitles
#
# SYNOPSIS
#
# srtran [-v a=<a>] [-v b=<b>] [<file>...]
#
# OPTIONS
#
# -v a=<a> Set the value of a to <a> (default: a = 1).
# -v b=<b> Set the value of b to <b> (default: b = 0).
#
# DESCRIPTION
#
# Each timestamp in the subtitle file is linearly transformed using
# the function f(t) = a*t + b. This is useful for many things, e.g.,
# fixing the framerate of subtitles, syncing subtitles, etc.
#
# EXAMPLE
#
# To convert a subtitle file with original framerate 23.976 fps to
# 25.000 fps, note that a = 23.976/25.000 = 0.95904. Thus,
#
# srtran -v a=0.95904 -v b=0 input.srt >output.srt
#
BEGIN {
RS = "\r?\n[0-9]+\r?\n" # Frame separator
FS = "\r?\n" # Line separator
FN = 1 # Subtitle frame number
if (a == "")
a = 1.0
if (b == "")
b = 0.0
}
function seconds(subtime) {
split(subtime, array, /[:,]/)
return array[1]*3600 + array[2]*60 + array[3] + array[4]*0.001
}
function subtime(seconds) {
hours = int(seconds/3600)
min = int((seconds - hours*3600)/60)
secs = int((seconds - hours*3600 - min*60))
msecs = int((seconds - hours*3600 - min*60 - secs)*1000)
return sprintf("%02d:%02d:%02d,%03d", hours, min, secs, msecs)
}
{
if (NR == 1) {
interval = $2
subline = 3
} else {
interval = $1
subline = 2
}
split(interval, period, "-->")
start = a*seconds(period[1]) + b
end = a*seconds(period[2]) + b
if (start >= 0) {
print FN
print subtime(start), "-->", subtime(end)
for (i = subline; i <= NF; i++)
print $i
FN++
}
}
END {
printf("srtran: %d frames lost\n", (NR - FN + 1)) >"/dev/stderr"
}