├── .gitignore
├── Makefile
├── subsync.1
├── README_cn.md
├── README.md
├── subsync.c
└── LICENSE
/.gitignore:
--------------------------------------------------------------------------------
1 | # Prerequisites
2 | *.d
3 |
4 | # Object files
5 | *.o
6 | *.ko
7 | *.obj
8 | *.elf
9 |
10 | # Linker output
11 | *.ilk
12 | *.map
13 | *.exp
14 |
15 | # Precompiled Headers
16 | *.gch
17 | *.pch
18 |
19 | # Libraries
20 | *.lib
21 | *.a
22 | *.la
23 | *.lo
24 |
25 | # Shared objects (inc. Windows DLLs)
26 | *.dll
27 | *.so
28 | *.so.*
29 | *.dylib
30 |
31 | # Executables
32 | *.exe
33 | *.out
34 | *.app
35 | *.i*86
36 | *.x86_64
37 | *.hex
38 |
39 | # Debug files
40 | *.dSYM/
41 | *.su
42 | *.idb
43 | *.pdb
44 |
45 | # Kernel Module Compile Results
46 | *.mod*
47 | *.cmd
48 | .tmp_versions/
49 | modules.order
50 | Module.symvers
51 | Mkfile.old
52 | dkms.conf
53 |
54 | #local file
55 | subsync
56 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 |
2 | ifeq ($(PREFIX),)
3 | PREFIX := /usr/local
4 | endif
5 |
6 | TARGET = subsync
7 | VERSION = 0.13.2
8 | CFLAGS = -Wall -O3 -DVERSION=\"$(VERSION)\" -DCFG_LIBICONV
9 |
10 | LIBICONV = libiconv-1.18
11 | ICONV_W32 = -I./$(LIBICONV)_i686/include -L./$(LIBICONV)_i686/lib/.libs
12 | ICONV_W64 = -I./$(LIBICONV)_x86_64/include -L./$(LIBICONV)_x86_64/lib/.libs
13 |
14 | all: $(TARGET)
15 |
16 | allwin: $(TARGET) libiconv $(TARGET)_i686.exe $(TARGET)_x86_64.exe
17 |
18 | $(TARGET): subsync.c
19 | gcc $(CFLAGS) -o $@ $^
20 |
21 | $(TARGET)_i686.exe: subsync.c
22 | i686-w64-mingw32-gcc $(CFLAGS) $(ICONV_W32) -o $@ $^ -liconv
23 |
24 | $(TARGET)_x86_64.exe: subsync.c
25 | x86_64-w64-mingw32-gcc $(CFLAGS) $(ICONV_W64) -o $@ $^ -liconv
26 |
27 | clean:
28 | rm -f $(TARGET)
29 | rm -f $(TARGET)_i686.exe $(TARGET)_x86_64.exe
30 |
31 | cleanall: clean
32 | rm -rf $(LIBICONV)_i686 $(LIBICONV)_x86_64
33 |
34 |
35 | install: $(TARGET)
36 | install -s $(TARGET) $(PREFIX)/bin
37 | install -d $(PREFIX)/share/man/man1
38 | install -m 644 $(TARGET).1 $(PREFIX)/share/man/man1
39 |
40 | uninstall:
41 | rm -f $(PREFIX)/bin/$(TARGET) $(PREFIX)/share/man/man1/$(TARGET).1
42 |
43 | $(TARGET).pdf: $(TARGET).1
44 | man -l -Tps $< | ps2pdf - $@
45 |
46 | release: release-src release-win
47 |
48 | release-src:
49 | mkdir $(TARGET)-$(VERSION)
50 | cp LICENSE Makefile README* subsync.1 subsync.c $(TARGET)-$(VERSION)
51 | tar czf $(TARGET)-$(VERSION).tar.gz $(TARGET)-$(VERSION)
52 | rm -rf $(TARGET)-$(VERSION)
53 |
54 | release-win: $(TARGET)_i686.exe $(TARGET)_x86_64.exe $(TARGET).pdf
55 | mkdir $(TARGET)-$(VERSION)-win
56 | cp LICENSE README* $(TARGET).pdf $(TARGET)_*.exe $(TARGET)-$(VERSION)-win
57 | zip -r $(TARGET)-$(VERSION)-win.zip $(TARGET)-$(VERSION)-win
58 | rm -rf $(TARGET)-$(VERSION)-win
59 |
60 | release-clean:
61 | rm -f $(TARGET)-$(VERSION).tar.gz $(TARGET)-$(VERSION)-win.zip
62 |
63 |
64 | libiconv: $(LIBICONV)_i686 $(LIBICONV)_x86_64
65 |
66 | $(LIBICONV)_i686: $(LIBICONV).tar.gz
67 | tar zxf $(LIBICONV).tar.gz
68 | mv $(LIBICONV) $(LIBICONV)_i686
69 | (cd $(LIBICONV)_i686; ./configure --host=i686-w64-mingw32 --enable-static --disable-shared; make)
70 |
71 | $(LIBICONV)_x86_64: $(LIBICONV).tar.gz
72 | tar zxf $(LIBICONV).tar.gz
73 | mv $(LIBICONV) $(LIBICONV)_x86_64
74 | (cd $(LIBICONV)_x86_64; ./configure --host=x86_64-w64-mingw32 --enable-static --disable-shared; make)
75 |
76 | $(LIBICONV).tar.gz:
77 | wget https://ftp.gnu.org/pub/gnu/libiconv/$(LIBICONV).tar.gz
78 |
79 |
80 |
--------------------------------------------------------------------------------
/subsync.1:
--------------------------------------------------------------------------------
1 | .TH NAME SECTION
2 | .SH Name
3 | subsync \- synchronise video and subtitle
4 |
5 | .SH SYNOPSIS
6 | .B subsync
7 | .I [OPTIONS] [subtitle_files]
8 |
9 | .SH DESCRIPTION
10 | .BR Subsync
11 | is a simple and quick command line tool written in C and standard libraries
12 | to synchronise videos and subtitles. It supports
13 | .I .srt ,
14 | .I .ass
15 | and
16 | .I .ssa
17 | formats. It can shift, scale and non-linearly process the timeline in subtitle files.
18 |
19 | .SH OPTIONS
20 | .TP
21 | .BR \-c , " \-\-chop"
22 | chop the specified number of subtitles. The followed argument
23 | .I N:M
24 | defines chopping range of the start index and the end index, separated by colon(:).
25 | The index counts from 1 and the chopping range includes the start and end index.
26 | The index indicates each line of subtitles with time stamps.
27 |
28 | .TP
29 | .BR \-e , " \-\-encoding"
30 | set default encoding of the input files.
31 | .B subsync
32 | normally passthrough the input files to the output files, which means changing
33 | codepages is not necessary. However, some files encoded by 16-bit or 32-bit units
34 | would break the libc stream input, like UTF-16 or UTF-32. In that case
35 | .B subsync
36 | will convert the input files to UTF-8 before adjusting the subtitles.
37 | .B subsync
38 | can auto-detect the encoding of the input file, yet if the BOM were missing,
39 | you need to specify the default encoding by
40 | .I \-e , " \-\-encoding"
41 | option. The encoding character sets is same to the
42 | .B iconv(1).
43 | Use
44 | .I iconv " \-\-list"
45 | to see the full list.
46 |
47 | .TP
48 | .BR \-o , " \-\-overwrite"
49 | output to the original subtitle files so have them overwritten. The latter
50 | .I --overwrite
51 | allows a backup file.
52 |
53 | .TP
54 | .BR \-r , "\-\-reorder"
55 | reorder the serial number in
56 | .I .srt
57 | files. The followed argument defines the start number.
58 | When specified,
59 | .B subsync
60 | will discard the original serial number and generate new numbers in ascending order.
61 |
62 | .TP
63 | .BR \-s , "\-\-span
64 | specifies the range of the time for processing. When specified,
65 | .B subsync
66 | will only process the subtitle inside this range.
67 | The followed argument defines the start time stamp.
68 | It can be in milliseconds or in
69 | .I HH:MM:SS.MS
70 | format. The second argument is optional, which indicates the end time stamp.
71 | If the second argument is not specified, the default ending is the end of file.
72 |
73 |
74 | .TP
75 | .BR \-w , " \-\-write"
76 | specifies the output file after synchronising.
77 | Otherwise the contents will be sent to the terminal.
78 |
79 | .TP
80 | .BR "\-OFFSET", " \+OFFSET"
81 | specifies the expecting offset of the timeline.
82 | It can be in milliseconds or in
83 | .I HH:MM:SS.MS
84 | format.
85 | When the option begins with a positive sign
86 | .B (\+)
87 | all time stamps in the file will be added by this offset,
88 | so subtitles will be
89 | .B postponed .
90 | When the option begins with a negative sign
91 | .B (\-)
92 | all time stamps in the file will be subtracted by this offset,
93 | so subtitles will be
94 | .B advanced .
95 | However when
96 | .I "\-OFFSET"
97 | is in
98 | .B subtraction statement
99 | by form of
100 | .I "\-expected_timestamp-misaligned_timestamp" ,
101 | the shifting direction is decided by the calculation of the statement.
102 |
103 |
104 | .TP
105 | .BR "\-SCALE"
106 | specifies the scale ratio of the timeline.
107 | It must be given as a real number with a point or a
108 | .B dividing statement
109 | to distinguish from the
110 | .I "\-OFFSET"
111 | option. When specified,
112 | .B subsync
113 | will have all time stamps in the file been multiplied by this scale ratio.
114 | Therefore when the ratio is greater than 1, the timeline prolongs;
115 | when the ratio is less than 1, the timeline shortens.
116 | .B Subsync
117 | ignores the positive or negative sign in this option.
118 |
119 |
120 | .TP
121 | .BR "\-\-help" , " \-\-version" , " \-\-help\-example"
122 | Helps and examples.
123 |
124 | .TP
125 | .BR "\-\-help\-subtract"
126 | calculate the time offset.
127 | Debug purpose but might be useful.
128 |
129 | .TP
130 | .BR "\-\-help\-divide"
131 | calculate the scale ratio of time stamps.
132 | Debug purpose but might be useful.
133 |
134 | .TP
135 | .BR "\-\-help\-strtoms"
136 | test reading the time stamps.
137 | Debug purpose but might be useful.
138 |
139 |
140 | .SH "TIME STAMP"
141 | .B Subsync
142 | supports three forms of time stamp in its parameters.
143 | The
144 | .I millisecond
145 | is most simple and most unreadable. The
146 | .I .srt
147 | format is like
148 | .I 00:10:07,570 ,
149 | each field represents hour, minute, second and millisecond.
150 | The millisecond field is delimited by the comma and the unit is
151 | .B 1 ms .
152 | The counterpart in format of
153 | .I .ass/.ssa
154 | is
155 | .I 00:10:07.57 ,
156 | each field represents hour, minute, second and millisecond.
157 | The millisecond field is delimited by the point and the unit is
158 | .B 10 ms .
159 |
160 |
161 | .SH COPYING
162 | This is free software: you can redistribute it and/or modify
163 | it under the terms of the GNU General Public License as published by
164 | the Free Software Foundation, either version 3 of the License, or
165 | (at your option) any later version.
166 |
167 | This program is distributed in the hope that it will be useful,
168 | but WITHOUT ANY WARRANTY; without even the implied warranty of
169 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
170 | GNU General Public License for more details.
171 |
172 | You should have received a copy of the GNU General Public License
173 | along with this program. If not, see .
174 |
175 |
176 | .SH BUGS
177 | Please send bug reports to "Andy Xuming"
178 |
179 |
180 | .SH EXAMPLES
181 | .TP
182 | .B subsync +12000 source.ass > target.ass
183 | Shift the subtitles backwards by 12 seconds.
184 |
185 | .TP
186 | .B cat source.ass | subsync -00:10:07,570 -w target.ass
187 | Bring forward the subtitles by 607570 milliseconds,
188 | which deducted from the
189 | .I 00:10:07,570
190 | parameter. Subtitles are came from pipe and written to file named
191 | .I target.ass .
192 |
193 | .TP
194 | .B subsync -1.000955 -w target.ass source.ass
195 | Scale the timeline by the ratio
196 | .B 1.000955
197 | so the subtitles will be prolonged evenly.
198 |
199 | .TP
200 | .B subsync +00:00:52,570-0:11:00,140 source.ass > target.ass
201 | Shift the timeline by the
202 | .I subtraction statement .
203 | The first timestamp is the expected time in the video.
204 | The second timestamp is the misaligned time in the subtitle file.
205 |
206 |
207 | .TP
208 | .B subsync -01:35:32,160/1:35:26,690 source.ass > target.ass
209 | Scale the timeline by the
210 | .I dividing statement .
211 | The first timestamp is the expected time in the video.
212 | The second timestamp is the misaligned time in the subtitle file.
213 | Both timestamps come from the last subtitle in the file.
214 |
215 | .TP
216 | .B subsync -01:35:32,160/1:35:26,690 +00:00:52,570-0:11:00,140 source.ass
217 | Shift then scale the timeline by combining the
218 | .I subtraction statement
219 | and
220 | .I dividing statement .
221 | The order of options in command line is irrelevant.
222 |
223 | .TP
224 | .B subsync -s 0:01:15.00 -00:01:38,880-0:03:02.50 -o *.srt
225 | Shift the timeline from 1 minute 15 seconds to the end by the
226 | .I subtraction statement
227 | for all
228 | .B .srt
229 | files in the current directory.
230 | The results will overwrite the originals.
231 |
232 | .TP
233 | .B subsync -e BIG-5 -s 0:01:15.00 -00:01:38,880-0:03:02.50 -o *.srt
234 | Mostly same to above, besides it specifies the
235 | .I BIG-5
236 | as the default encoding. Thus the input files will be treated as
237 | .I BIG-5
238 | encoding and will be converted to
239 | .I UTF-8
240 | in the output files.
241 |
242 |
243 |
--------------------------------------------------------------------------------
/README_cn.md:
--------------------------------------------------------------------------------
1 | # 用 Subsync 调整字幕时间
2 | Subsync 是一个命令行工具,用于调整字幕文件的时间轴,使语音和字幕同步。
3 | 它可以:
4 | - 按偏移时间,提前或推后显示字幕
5 | - 按比例缩放,提前或推后显示字幕
6 | - 在指定的时间范围内调整字幕时间
7 | - 支持 `.srt`, `.ass` 和 `.ssa` 字幕格式
8 | - 用 C 语言写的过滤程序,简单高速
9 | - 只用了普通 C 运行库,可以移植到所有的操作系统
10 | - 命令行工具,很容易集成在脚本里面
11 |
12 |
13 | # 为什么需要 Subsync
14 | 我有一些古老的电视剧和网上找来的 `.srt` 字幕文件,很多是不同步的。
15 | `.srt` 的结构很简单,按理说只要过滤出时间戳,稍微调整一下,再写回去就好了。
16 | 出乎意料的是,只找到了很庞大的 gui 程序,而不是我希望的轻量级命令行工具。
17 | 更麻烦的是,没找到能够缩放时间轴的工具 -- 有些视频是 30 帧/秒的,
18 | 而对应的字幕来自 25 帧/秒,刚开始视频是同步的,越往后偏差越大,
19 | 不能用简单的提前或推后时间戳解决这个问题。所以写了这个小程序填补这些空白。
20 |
21 |
22 | # 编译和安装
23 | 您可以在 github 上抓取源码编译:
24 | ```
25 | git clone https://github.com/xuminic/subsync.git
26 | cd subsync
27 | make
28 | ```
29 | 如果一切正常,应该能够编译出 `subsync`。 您可以把它移动到路径上的任何地方。
30 |
31 | 编译 Windows 程序可以用 MinGW64 或 Cygwin 在 Windows 系统上直接编译,
32 | 也可以在 Linux 系统上用 MinGW 交叉编译,例如
33 | ```
34 | apt install mingw-w64
35 | ```
36 | 然后在 `subsync` 目录下
37 | ```
38 | make allwin
39 | ```
40 | make 会联网下载 `libiconv-1.18.tar.gz` 源程序,然后分别编译产生 Win32 和 Win64
41 | 的 `subsync` 可执行文件。
42 | - 可以预先把 `libiconv-1.18.tar.gz` 存到 `subsync` 目录下,实现离线编译
43 | - Windows 程序长这个样子: subsync_i686.exe 和 subsync_x86_64.exe
44 | - 在 github 的 Release 区可以直接下载编译好的 Windows 程序
45 | - 没有 Windows 上的安装程序,请直接拷贝使用
46 |
47 |
48 | # 命令行选项
49 | - 不指定文件名的话, `subsync` 读取 `stdin` 并且输出到 `stdout`,例如:
50 | ```
51 | subsync +12000 < source.ass > target.ass
52 | ````
53 |
54 | - 只要不是命令行选项的参数,都当作输入文件名。
55 | 输出文件名用 `-w` 选项指定。例如:
56 | ```
57 | subsync +12000 -w target.ass source.ass
58 | ```
59 | 该命令等同于上一条命令。
60 |
61 | - 一条命令行上可以指定多个文件名。例如
62 | ```
63 | subsync +12000 source1.ass source2.ass source3.ass
64 | ```
65 | 但如此一来,所有输出内容全部汇总到 stdout。
66 | 用 `-w` 选项也是一样:
67 | ```
68 | subsync +12000 -w target.ass source1.ass source2.ass source3.ass
69 | ```
70 | 所有输出内容全部汇总到 target.ass 。
71 |
72 | - 覆盖选项是 `-o` 或 `--overwrite`,修改时间轴后直接覆盖原文件:
73 | ```
74 | subsync +12000 -o source1.ass source2.ass source3.ass
75 | ```
76 | 因此输出文件还是 `source1.ass`, `source2.ass` 和 `source3.ass`。
77 |
78 | - `--overwrite` 和 `-o` 稍许不同,区别是前者产生一个备份文件。
79 | ```
80 | subsync +12000 --overwrite source1.ass source2.ass source3.ass
81 | ```
82 | 修改后的内容覆盖 `source1.ass`, `source2.ass` 和 `source3.ass`,
83 | 同时产生备份文件 `source1.ass.bak`, `source2.ass.bak` 和
84 | `source3.ass.bak`。如果操作失误,可以从备份文件中回退。
85 |
86 | - 偏移时间戳选项: `-/+OFFSET` 用于把字幕时间提前或延后。
87 | - `+` 增加时间戳,等于延后显示字幕。
88 | - `-` 减少时间戳,等于提前显示字幕。
89 | - `OFFSET`是针对字幕时间的偏移量,其格式可以是
90 | - 毫秒,如 `19700`
91 | - `srt` 时间戳格式,如 `0:0:10,190`
92 | - `ass/ssa` 时间戳格式,如 `0:0:10.19`
93 | - 时间戳算术格式,如`01:44:31,660-01:44:36,290`
94 | - 详见后面的 [HOWTO: 偏移时间戳](#howto:-偏移时间戳) 节。
95 |
96 | - 缩放时间戳选项: `-SCALE` 用于按比例缩放时间戳。可以指定为
97 | - 浮点数,如 `1.1988`
98 | - 预定义常数 `N-P`,等于 `1.1988`。
99 | - 预定义常数 `P-N`,等于 `0.83417`。
100 | - 预定义常数 `N-C`,等于 `1.25`。
101 | - 预定义常数 `C-N`,等于 `0.8`。
102 | - 预定义常数 `P-C`,等于 `1.04271`。
103 | - 预定义常数 `C-P`,等于 `0.95904`。
104 | - 该命令行选项不会和 `-OFFSET` 选项混淆,缩放必须是浮点数。
105 | - 详见后面的 [HOWTO: 缩放时间戳](#howto:-缩放时间戳) 节
106 |
107 | - 删除一定范围的字幕 `-c N:M` 或 `--chop N:M`
108 |
109 | `N:M` 是字幕的序列号,用于 `srt` 文件。
110 |
111 | - 重排 `srt` 文件的序列号用 `-r [NUM]` 或 `--reorder [NUM]`
112 |
113 | 用于整理 `.srt` 文件内的字幕序列号,尤其经过分拆或合并字幕的时候,
114 | 往往造成乱序,虽然不影响使用,但是很难看。 `-r` 选项可以从 1 开始、
115 | 重排序列号。 `NUM` 是可选参数,如果设置了 `NUM`, 重排的序列号
116 | 将从 `NUM` 开始。
117 |
118 | - 指定时间戳范围 `-s TIME` 或 `--span TIME`
119 |
120 | 如果需要修改一定范围内的时间戳,而不是整个字幕文件,则通过这个选项指定时间范围。
121 | 时间范围看上去是这个样子 `-s 00:00:52,570 0:11:00,140` 。
122 | 如果只指定第一部分,如 `-s 00:00:52,570` ,则默认结束时间是文件结尾处。
123 |
124 | - 指定输出文件名 `-w FILENAME` 或 `--write FILENAME`
125 |
126 | 如果不指定输出文件名,默认输出到标准输出 `stdout` 。
127 | 除非用 `-o` 或 `--overwrite` 覆盖原文件。
128 |
129 |
130 |
131 | # 时间格式
132 | `subsync` 支持下面的时间格式:
133 | - 整数,单位是毫秒,如 `19700`
134 | - `srt` 时间戳格式,如 `0:0:10,190`。
135 |
136 | 冒号之间分别是: 小时,分钟,秒钟。可以依次省略,例如 `1:20` 表示 1 分 20 秒。
137 | 逗号后面是毫秒, `190` 就是 190 毫秒。
138 | - `ass/ssa` 时间戳格式,如 `0:0:10.19`。
139 |
140 | 冒号之间分别是: 小时,分钟,秒钟。可以依次省略,例如 `1:20` 表示 1 分 20 秒。
141 | 句号后面是百分秒, 百分之 `19` 秒就是 190 毫秒。
142 | - 时间戳的算术差,用于偏移时间戳,例如 `01:44:31,660-01:44:36,290`。
143 | `subsync` 将把前后两个时间戳转换为毫秒,并相减。其结果可以是正数,也可以是负数。
144 | - 如果结果为负数,则导致字幕时间提前
145 | - 如果结果为正数,则导致字幕时间延后
146 | - 时间戳格式可以是任何支持的格式,例如 `01:44:31,660-12700`
147 | - 便于引用不同语言的字幕时间数据,获取差值
148 | - 时间戳的比例表达式,用于缩放时间戳,例如 `01:44:30,290/01:44:31,660`。
149 | `subsync` 将把前后两个时间戳转换为毫秒,并相除。其结果是一个浮点数。
150 | - 如果结果小于 1,则导致字幕间隔缩短
151 | - 如果结果大于 1,则导致字幕间隔拉长
152 | - 时间戳格式可以是任何支持的格式,例如 `01:44:31,660/12700`
153 | - 用最后一条字幕的时间除以对应语音的时间,可以快速获得缩放比例
154 | - 注意: 省略到秒分位时,秒和毫秒的区别。
155 | 例如 `20` 表示 20 毫秒,而不是 20 秒。`20,0` 或 `20.0`才是 20 秒。
156 | - 注意: 上例的 `20.0` 会和时间戳缩放比例混淆。
157 | 而时间戳缩放比例优先于偏移量。例如命令行上
158 | ```
159 | subsync -20.0 -w target.ass source.ass
160 | ```
161 | 将扩大 `source.ass` 的时间戳间隔 20 倍,而不是提前 20 秒显示。
162 | 如果要提前 20 秒显示,要用下面任何一种格式:
163 | ```
164 | subsync -20,0 -w target.ass source.ass
165 | subsync -20000 -w target.ass source.ass
166 | subsync -0:20 -w target.ass source.ass
167 | subsync -0:20.0 -w target.ass source.ass
168 | ```
169 |
170 | # HOWTO: 偏移时间戳
171 | 偏移时间戳是最常见操作,把字幕的时间戳加上或减去一个固定值,导致字幕被推迟或提前显示。
172 | 大多数错位字幕源于片头增加或去掉了一部分内容,偏移时间戳可以有效的纠正这个误差。
173 |
174 | 例如这个命令:
175 | ```
176 | subsync +12000 < source.ass > target.ass
177 | ```
178 | 把 `source.ass` 里面所有的字幕时间戳增加了 12000 毫秒,导致每一条字幕推迟 12 秒显示。
179 | 这个参数 `+12000` 中的 `+` 指增加,对应的,`-` 指减少,因此
180 | ```
181 | subsync -12000 < source.ass > target.ass
182 | ```
183 | 把 `source.ass` 里面所有的字幕时间戳减少了 12000 毫秒,导致每一条字幕提前 12 秒显示。
184 |
185 | 时间格式可以是整数,代表毫秒,也可以是常见的 `HH:MM:SS` 格式。
186 | 注意,毫秒位有两种不同格式, `srt` 的 `HH:MM:SS,mmm` 和 `ass` 的 `HH:MM:SS.nn`。
187 | 详见前面 [时间格式](#时间格式) 节。
188 |
189 | 为了简化计算, `subsync` 支持时间戳的算术差作为偏移参数。
190 | 例如,你可以在字幕文件中,随便选取一个错位字幕的时间,然后去视频中,
191 | 找到对应台词的起始时间,用台词时间减去错位的时间:
192 | ```
193 | subsync +00:00:52,570-0:11:00,140 source.ass > target.ass
194 | ```
195 | 这个偏移参数 `+00:00:52,570-0:11:00,140` 中,
196 | - `+` 号被忽略,换成 `-` 号也是一样的
197 | - `00:00:52,570` 是视频里面,对应台词的起始时间
198 | - `0:11:00,140` 是字幕文件里面,对应字幕的起始时间
199 | - `subsync` 计算两个时间戳的差值,代替手工计算
200 | - 计算公式是: `期待时间 - 错位时间`
201 | - `期待时间` 和 `错位时间` 可以是不同的时间格式。
202 |
203 | # HOWTO: 缩放时间戳
204 | 某些字幕无法通过偏移时间戳实现同步,这类视频的特点是,视频起始处的字幕是同步的,
205 | 随着播放时间增长,字幕时间开始飘移,有的是字幕逐渐延迟,有的是字幕逐渐提前。
206 | 这类视频问题源自帧率,例如视频来自 NTSC 版本,而字幕来自 PAL 版本,那么就会
207 | 出现 30 vs 25 的差异,有些电影直接用了 24 帧的影院版本,无论是 PAL 字幕
208 | 还是 NTSC 字幕,都会出现失步现象。
209 |
210 | 解决方法是 `subsync` 的缩放时间戳功能。缩放时间戳通过对字幕的每一个时间戳乘上
211 | 一个系数,来补偿时间轴的飘移。例如
212 | ```
213 | subsync -1.000955 source.ass > target.ass
214 | ```
215 | 如果参数大于 1 ,表示字幕时间逐渐延迟,如果参数小于 1 ,表示字幕时间逐渐提前。
216 | 缩放时间戳的参数开关和正负无关,因此, `-1.000955` 和 `+1.000955` 完全等效。
217 |
218 | 因为该参数是浮点数,因此通常不会和偏移时间戳参数混淆。
219 | 出现混淆的情况下,缩放时间戳参数的优先级高于偏移时间戳参数。
220 |
221 | 缩放根据这个公式计算: `期待时间 / 错位时间`。
222 | 例如,有一部电影,刚开始字幕是同步的,但是影片最后一条字幕时间是 `1:35:26,690`,
223 | 而对应的台词出现在 `01:35:32,160`,所以字幕实际上在逐渐提前。因此:
224 | - 期待时间是 `01:35:32,160`,等于 5732160 毫秒
225 | - 错位时间是 `1:35:26,690`,等于 5726690 毫秒
226 | - 缩放系数是 `5732160 / 5726690 ~= 1.000955`
227 |
228 | 为了简化计算, `subsync` 支持时间戳的除法算式作为缩放参数。以上面情况为例:
229 | ```
230 | subsync -01:35:32,160/1:35:26,690 source.ass > target.ass
231 | ```
232 | 这个缩放参数 `-01:35:32,160/1:35:26,690` 中,
233 | - `-` 号被忽略,换成 `+` 号也是一样的
234 | - `01:35:32,160` 是视频里面,对应台词的起始时间
235 | - `1:35:26,690` 是字幕文件里面,对应字幕的起始时间
236 | - `subsync` 计算两个时间戳的比例,代替手工计算
237 | - 计算公式是: `期待时间 / 错位时间`
238 | - `期待时间` 和 `错位时间` 可以是不同的时间格式。
239 |
240 |
241 | # HOWTO: 非线形编辑
242 | `subsync` 支持简单的非线形编辑,用来处理局部字幕偏移或飘移。
243 | 如果应用得当,尤其是处理电视连续剧,一旦找到第一集的处理公式,
244 | 通常可以把同样的参数用在其他集上,可以大幅度提高操作效率。
245 |
246 | `subsync` 用 `-s` 开关选取处理范围,命令行参数是:
247 | ```
248 | -s start-time-stamp [end-time-stamp]
249 | ```
250 | 这里的 `start-time-stamp` 是起始时间, `end-time-stamp` 是结束时间。
251 | 结束时间是可选项,如果忽略,则处理到文件结尾。例如
252 | ```
253 | subsync -s 0:01:15.00 1:23:34.00 -00:01:38,880-0:03:02.50 source.ass > target.ass
254 | ```
255 | 该命令要求从 1 分 15 秒开始,到 1 小时 23 分 34 秒,
256 | 这段区间的字幕提前 83.62 秒显示。
257 | 提前 83.62 秒来自 `-00:01:38,880-0:03:02.5` 的计算结果。
258 |
259 | # HOWTO: 批处理
260 | `subsync` 很容易集成在 shell 脚本里面,例如
261 | ```
262 | for i in *.srt; do subsync +12000 $i > $i.new; done
263 | ```
264 | 也可以单独使用,通过 `-o` 选项覆盖原文件
265 | ```
266 | subsync -o +12000 *.srt
267 | ```
268 | 或者用 `--overwrite` 覆盖原文件的同时保留一个备份:
269 | ```
270 | subsync --overwrite +12000 *.srt
271 | ```
272 |
273 |
274 | # 案例分析
275 | 这里也有 [here](https://quickthinknotes.blogspot.com/2018/01/linux.html).
276 |
277 | ## EVA3.3 Theatrical Edition
278 | `EVA3.3 Theatrical Edition` 的视频长度是 1:32:52,但是字幕显示非常混乱。
279 | 打开字幕文件 `00002.v1.11_FINAL.ass`,发现最后一条字幕是
280 | ```
281 | Dialogue: 0,1:45:42.51,1:45:46.48,Comment,,0,0,0,,♫Peace in time we've never had it so good\N安享和平 生活从未如此美好
282 | ```
283 | 字幕比视频多出来 10 分钟,推测应该是来自巨神兵故事的场景。
284 |
285 | 不管怎样,首先找到第一句台词,发生在 52 秒处,好在英语字幕正确,可以拿到精确时间
286 | ```
287 | 00:00:52,570 Tracking team, report current Eva unit positions.
288 | ```
289 | 对应的中文字幕是
290 | ```
291 | Dialogue: 0,0:11:00.14,0:11:02.98,Default,,0,0,0,,追踪班 报告两机体现在的位置
292 | ```
293 | 这次我们手工操作,用 `subsync` 的帮助工具计算时间差:
294 | ```
295 | $ subsync --help-sub 00:00:52,570 0:11:00.14
296 | Time difference is -00:10:07,570 (-607570 ms)
297 | ```
298 | 先用偏移时间戳补偿这 10 分钟场景:
299 | ```
300 | $ subsync -00:10:07,570 00002.v1.11_FINAL.ass > 001.ass
301 | ```
302 | 试了试这个字幕,前面同步了,后面开始飘移,果然帧率也需要调整。
303 |
304 | 回到视频本身,拉到片尾,找到最后一句台词,发生在
305 | ```
306 | 01:35:32,160 The Wunder streaks through the sky
307 | ```
308 | 对应的中文字幕是
309 | ```
310 | Dialogue: 0,1:35:26.69,1:35:28.07,Default,,0,0,0,,划破天际的Wunder
311 | ```
312 | 我们还是手工操作,用 `subsync` 的帮助工具计算时间差率:
313 | ```
314 | $ subsync --help-div 01:35:32,160 1:35:26.69
315 | Time scale factor is 1.000955
316 | ```
317 | 这个比例接近 `24 / 23.976`,估计就是飘移原因了,我们用这个系数再加工一下
318 | 刚才产生的 `001.ass` 字幕:
319 | ```
320 | $ subsync -1.000955 001.ass > 002.ass
321 | ```
322 | 视频连上 `002.ass` 字幕,这次显示正常了。
323 |
324 | 顺便验证了一下自动计算命令:
325 | ```
326 | subsync +00:00:52,570-0:11:00,140 -01:35:32,160/1:35:26,690 00002.v1.11_FINAL.ass > 002.ass
327 | ```
328 | 验证结果显示,和手工操作一样。
329 |
330 |
331 | ## 不可思议的海之娜蒂亚
332 | 这个剧有 39 集,字幕全部失步。视频是 `mkv` 格式,自带英语字幕。
333 | 我们先用 `ffmepg` 把英语字幕抽出来看看:
334 | ```
335 | ffmpeg -i "Nadia Ep 01.mkv" -map 0:s:0 subs.srt
336 | ```
337 | 可见一开始字幕是同步的,`subs.srt` 中:
338 | ```
339 | 1
340 | 00:00:03,200 --> 00:00:05,900
341 | Are you adventurers,
342 | ```
343 | 对应的中文字幕:
344 | ```
345 | Dialogue: 0,0:00:03.50,0:00:05.50,*Default,,0000,0000,0000,,你是一位冒险家吗
346 | ```
347 | 但是从第 18 条字幕开始失步:
348 | ```
349 | 18
350 | 00:01:09,630 --> 00:01:12,460 as the threat of a world war loomed ever closer.
351 | 19
352 | 00:01:38,880 --> 00:01:41,040
353 | Paris... Paris...
354 | ```
355 | 对应的中文字幕:
356 | ```
357 | Dialogue: 0,0:01:08.00,0:01:11.80,*Default,,0000,0000,0000,,但是人们生活在 即将来临的世界阴影中
358 | Dialogue: 0,0:03:02.50,0:03:04.90,*Default,,0000,0000,0000,,巴黎…巴黎…
359 | ```
360 | 视频间隔约 30 秒,字幕间隔却有 2 分钟,很可能来自不同的片头主题曲。
361 | 用 `subsync` 局部修理一下试试:
362 | ```
363 | subsync -s 0:01:15.00 -00:01:38,880-0:03:02.50 "Nadia Ep 01.ass" > 01.ass
364 | ```
365 | 结果显示正常,不需要缩放时间轴。但问题是第一集和后面的剧集不一样,有开场白。
366 | 该开场白在后面的剧集里不再出现,而中文字幕保留了这段场景。
367 | 因此从第 2 集开始, 用文本编辑器手工删除前 18 条字幕,然后用 `subsync` 修正时间轴:
368 | ```
369 | mv 01.ass 01.bak
370 | subsync -00:00:01,710-00:01:25,510 -o *.ass
371 | mv 01.bak 01.ass
372 | ```
373 |
374 | ## 工作细胞 Black
375 | 大概预览一下,觉得字幕提早了一秒,所以先用 `subsync` 调整一下:
376 | ```
377 | $ subsync +1000 -o *.ass
378 | ```
379 | 仔细查看视频,发现前 10 分钟左右是同步的,但后面字幕提早了 6 秒左右。
380 | 反正定位后发现,10 分 38 秒处有一段过场黑屏镜头,刚好是 6 秒,于是
381 | ```
382 | $ subsync -s 10:38 +6000 --overwrite 'Hataraku Saibou Black_-_01.ass'
383 | ```
384 | 再检查视频,这次完全同步了。
385 |
386 | 比较麻烦的地方是,虽然黑屏镜头时长都一样,但每集的位置不一样,只能一集一集找过去。
387 | 步骤是:
388 | - 观看视频,进度条直接拖到中间
389 | - 如果字幕同步就向后搜索,如果字幕失步就向前搜索
390 | - 找到黑屏镜头开始地点,记住时间
391 | - 用 `subsync -s 黑屏起始时间 +6000` 调整
392 |
393 | 所以命令行上看起来是这样的:
394 | ```
395 | $ subsync -s 10:38 +6000 --overwrite 'Hataraku Saibou Black_-_01.ass'
396 | $ subsync -s 10:44 +6000 -o 'Hataraku Saibou Black_-_02.ass'
397 | $ subsync -s 11:20 +6000 -o 'Hataraku Saibou Black_-_03.ass'
398 | $ subsync -s 11:32 +6000 -o 'Hataraku Saibou Black_-_04.ass'
399 | ```
400 |
401 |
402 | ## NaNa (2005)/娜娜/世上的另一个我
403 | `娜娜` 的前 10 分钟字幕正常,后 10 分钟基本都要延迟 3 到 4 秒。
404 | 比较麻烦的两个地方是
405 | - 没有过场镜头,似乎被直接删除了
406 | - 延迟时间也不一样,从 3 到 5 秒都有可能。
407 |
408 | 这种情况下只能手工寻找字幕延迟的起始地点,并来回调整延迟时间。
409 |
410 | 例如第三集,12 分 57 秒的时候字幕还同步,13 分 17 秒的时候已经滞后了 2 秒多。
411 | 来回查看,发现 13 分 9 秒有一个镜头切换,确认这里是删除点。先提前 3 秒试试:
412 | ```
413 | $ subsync -s 13:10 -3000 -o 'S01E03-Nana and Shoji, Love'\''s Whereabouts [030F3BD0].ass'
414 | ```
415 | 结果显示字幕有一点点超前,因此在同样位置,延后 0.5 秒试试:
416 | ```
417 | $ subsync -s 13:10 +500 -o 'S01E03-Nana and Shoji, Love'\''s Whereabouts [030F3BD0].ass'
418 | ```
419 | 这次字幕看上去同步了。一般来说,字幕显示误差在 0.5 秒内基本可以接受。
420 |
421 | 前三集处理起来比较费劲,要来回调整,一旦摸到规律,处理起来就比较快。
422 | 实际操作情况是这样的:
423 | ```
424 | $ subsync -s 8:20 -3500 -o S01E04-*.ass
425 | $ subsync -s 10:43 -3500 -o S01E05-*.ass
426 | $ subsync -s 11:13 -3500 -o S01E06-*.ass
427 | $ subsync -s 11:13 -500 -o S01E06-*.ass
428 | $ subsync -s 10:36 -4000 -o S01E07-*.ass
429 | $ subsync -s 10:36 +500 -o S01E07-*.ass
430 | $ subsync -s 10:36 +500 -o S01E07-*.ass
431 | $ subsync -s 11:20 -3500 -o S01E08-*.ass
432 | $ subsync -s 11:00 -3000 -o S01E09-*.ass
433 | $ subsync -s 11:00 +500 -o S01E09-*.ass
434 | $ subsync -s 12:16 -2500 -o S01E10-*.ass
435 | $ subsync -s 13:04 -3500 -o S01E11-*.ass
436 | $ subsync -s 13:04 +500 -o S01E11-*.ass
437 | $ subsync -s 10:12 -3500 -o S01E12-*.ass
438 | $ subsync -s 10:12 500 -o S01E12-*.ass
439 | ```
440 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Adjust Subtitle Timing with Subsync
2 | Subsync is a command-line tool used to adjust the timeline of
3 | subtitle files so that audio and subtitles stay synchronized.
4 | It can:
5 | - Shift subtitle display times forward or backward by
6 | a specified offset
7 | - Scale subtitle timing proportionally to adjust drift
8 | - Adjust subtitles only within a specified time range
9 | - Support `.srt`, `.ass`, and `.ssa` subtitle formats
10 | - A filtering program written in C — simple and fast
11 | - Rely only on the standard C runtime library,
12 | making it portable to all operating systems
13 | - A command-line tool, making it easy to integrate into scripts
14 |
15 | # Why Subsync Is Needed
16 | I have some old TV series and `.srt` subtitle files found online,
17 | but many of them are out of sync.
18 | Since the structure of `.srt` is very simple, in theory you only need
19 | to extract the timestamps, adjust them slightly, and write them back.
20 |
21 | Surprisingly, all I could find were large GUI programs,
22 | not the lightweight command-line tool I wanted.
23 | Even worse, I couldn’t find any tool that could scale the subtitle
24 | timeline. Some videos run at 30 fps, while the matching subtitles
25 | were created for 25 fps. At the beginning, the video and subtitles
26 | are in sync, but the further you go, the larger the drift becomes.
27 | A simple time offset won’t fix this. So I wrote this small program
28 | to fill that gap.
29 |
30 | # Build and Install
31 | You can fetch the source code from GitHub and compile it:
32 | ```
33 | git clone https://github.com/xuminic/subsync.git
34 | cd subsync
35 | make
36 | ```
37 | If everything goes well, it should produce the subsync executable.
38 | You can move it anywhere on your system path.
39 |
40 | To build the Windows version, you can compile directly on Windows
41 | using `MinGW64` or `Cygwin`, or cross-compile on Linux using MinGW,
42 | for example:
43 | ```
44 | apt install mingw-w64
45 | ```
46 | Then, inside the subsync directory:
47 | ```
48 | make allwin
49 | ```
50 | make will automatically download `libiconv-1.18.tar.gz`, compile it,
51 | and generate both Win32 and Win64 versions of the subsync executable.
52 | - You can place `libiconv-1.18.tar.gz` in the subsync directory
53 | in advance to allow offline compilation
54 | - The Windows executables are named as:
55 | `subsync_i686.exe` and `subsync_x86_64.exe`
56 | - Prebuilt Windows binaries are available directly in the GitHub
57 | Releases section
58 | - There is no installer for Windows — simply copy and use the program
59 |
60 |
61 | # Command Line Options
62 | - If no filename is specified, `subsync` reads from `stdin` and
63 | writes to `stdout`, for example:
64 | ```
65 | subsync +12000 < source.ass > target.ass
66 | ```
67 |
68 | - Any argument that is not a command-line option is treated as
69 | an input filename. You mau use the `-w` option to specify the
70 | output filename. For example:
71 | ```
72 | subsync +12000 -w target.ass source.ass
73 | ```
74 | This command is equivalent to the previous one.
75 |
76 | - You may specify multiple filenames in a single command,
77 | for example:
78 | ```
79 | subsync +12000 source1.ass source2.ass source3.ass
80 | ```
81 | However, all output will be combined and written to `stdout`.
82 | The same applies when using `-w`:
83 | ```
84 | subsync +12000 -w target.ass source1.ass source2.ass source3.ass
85 | ```
86 | All output will be combined into `target.ass`.
87 |
88 | - To overwrite files directly, use the `-o` or `--overwrite` option:
89 | ```
90 | subsync +12000 -o source1.ass source2.ass source3.ass
91 | ```
92 | The output files will therefore be `source1.ass`, `source2.ass`,
93 | and `source3.ass`.
94 |
95 | - There is a slight difference between `--overwrite` and `-o`:
96 | `--overwrite` creates a backup file:
97 | ```
98 | subsync +12000 --overwrite source1.ass source2.ass source3.ass
99 | ```
100 | The modified content overwrites `source1.ass`, `source2.ass`,
101 | and `source3.ass`, while backup files `source1.ass.bak`,
102 | `source2.ass.bak`, and `source3.ass.bak` are created.
103 | If something goes wrong, you can restore them.
104 |
105 | - Time-offset option: `-/+OFFSET` is used to shift subtitle timing
106 | forward or backward.
107 | - `+` increases timestamps, meaning subtitles appear later.
108 | - `-` decreases timestamps, meaning subtitles appear earlier.
109 | - `OFFSET` is the amount of shift applied to subtitle timestamps.
110 | Supported formats include:
111 | - Milliseconds, e.g. 19700
112 | - `.srt` format timestamps, e.g. `0:0:10,190`
113 | - `.ass/.ssa` format timestamps, e.g. `0:0:10.19`
114 | - Timestamp arithmetic, e.g. `01:44:31,660-01:44:36,290`
115 | - See the [HOWTO: Time Offset](#howto:-time-offset) section below.
116 |
117 | - Time-scaling option: `-SCALE` scales subtitle timestamps by a factor.
118 | It may be:
119 | - A floating-point number, such as `1.1988`
120 | - Predefined constant `N-P` (equals `1.1988`)
121 | - Predefined constant `P-N` (equals `0.83417`)
122 | - Predefined constant `N-C` (equals `1.25`)
123 | - Predefined constant `C-N` (equals `0.8`)
124 | - Predefined constant `P-C` (equals `1.04271`)
125 | - Predefined constant `C-P` (equals `0.95904`)
126 |
127 | This option cannot be confused with `-OFFSET`, since scaling
128 | must be a floating-point value.
129 | See the [HOWTO: Time Scale](#howto:-time-scale) section below.
130 |
131 | - To delete subtitles within a specific range,
132 | use `-c N:M` or `--chop N:M`.
133 |
134 | `N:M` refers to subtitle sequence numbers for `.srt` files.
135 |
136 | - To reorder `.srt` sequence numbers,
137 | use `-r [NUM]` or `--reorder [NUM]`.
138 |
139 | This is used to tidy up subtitle numbering, especially after
140 | splitting or merging subtitles, which often results in messy
141 | numbering.
142 |
143 | Although this does not affect playback, it looks untidy.
144 | The `-r` option will renumber subtitles starting from 1.
145 | If `NUM` is provided, numbering will start from `NUM`.
146 |
147 | - To specify a timestamp range,
148 | use `-s TIME` or `--span TIME`.
149 |
150 | If you only want to modify subtitle timestamps within a certain
151 | range rather than the whole file, use this option.
152 | A range looks like this: `-s 00:00:52,570 0:11:00,140`.
153 | If only the start time is given, e.g. `-s 00:00:52,570`,
154 | the default end time is the end of the file.
155 |
156 | - Specify an output filename using `-w FILENAME` or `--write FILENAME`.
157 |
158 | If no output filename is provided, output goes to `stdout`,
159 | unless `-o` or `--overwrite` is used to modify files directly.
160 |
161 | # Time Formats
162 | `subsync` supports the following time formats:
163 | - Integer value, in milliseconds, e.g. `19700`
164 | - `.srt` timestamp format, e.g. `0:0:10,190`
165 | - The fields separated by colons represent hours, minutes, and seconds.
166 | - They may be omitted from the left, e.g. 1:20 means 1 minute 20 seconds.
167 | - The part after the comma is milliseconds: 190 means 190 ms.
168 |
169 | - `.ass/.ssa` timestamp format, e.g. `0:0:10.19`
170 | - The fields separated by colons represent hours, minutes, and seconds,
171 | and may also be shortened the same way.
172 | - The part after the dot represents centiseconds:
173 | `.19` means 19 centiseconds = 190 ms.
174 |
175 | - Timestamp arithmetic, used for offsetting timestamps,
176 | e.g. `01:44:31,660-01:44:36,290`.
177 | `subsync` converts both timestamps to milliseconds and subtracts them.
178 | The result may be positive or negative:
179 | - A negative result shifts subtitles earlier
180 | - A positive result shifts subtitles later
181 | - Formats may be mixed, e.g. `01:44:31,660-12700`
182 | - This helps compute differences between timestamps from subtitles
183 | in different languages
184 |
185 | - Timestamp ratio, used for scaling timestamps,
186 | e.g. `01:44:30,290/01:44:31,660`.
187 | `subsync` converts both timestamps to milliseconds and divides them,
188 | producing a floating-point scaling factor:
189 | - If the result is less than 1, subtitle intervals become shorter
190 | - If the result is greater than 1, subtitle intervals become longer
191 | - Formats may be mixed, e.g. `01:44:31,660/12700`
192 | - Dividing the last subtitle’s time by the corresponding audio time
193 | is a quick way to obtain a scale factor
194 |
195 | - Note: When omitting hour/minute fields, be careful with
196 | seconds vs. milliseconds.
197 |
198 | For example, 20 means 20 milliseconds, not 20 seconds.
199 | `20,0` or `20.0` means 20 seconds.
200 |
201 | - Note: The value `20.0` is ambiguous and matches a scaling ratio,
202 | not a timestamp. Scaling has higher priority than offset.
203 | For example:
204 | ```
205 | subsync -20.0 -w target.ass source.ass
206 | ```
207 | This will enlarge the subtitle timing by 20×, not shift it earlier
208 | by 20 seconds.
209 |
210 | To shift by 20 seconds, use any of the following:
211 | ```
212 | subsync -20,0 -w target.ass source.ass
213 | subsync -20000 -w target.ass source.ass
214 | subsync -0:20 -w target.ass source.ass
215 | subsync -0:20.0 -w target.ass source.ass
216 | ```
217 |
218 | # HOWTO: Time Offset
219 | Shifting timestamps is the most common operation:
220 | adding or subtracting a fixed value from subtitle timestamps
221 | causes the subtitles to appear later or earlier.
222 | Most subtitle misalignment issues occur because some content
223 | was added to or removed from the opening of the video.
224 | A simple timestamp offset can effectively correct this problem.
225 |
226 | For example:
227 | ```
228 | subsync +12000 < source.ass > target.ass
229 | ```
230 | This increases all subtitle timestamps in `source.ass` by 12,000
231 | milliseconds, causing every subtitle to appear 12 seconds later.
232 | In the parameter `+12000`, the `+` indicates an increase.
233 | Conversely, `-` indicates a decrease:
234 | ```
235 | subsync -12000 < source.ass > target.ass
236 | ```
237 | This decreases all timestamps by 12,000 milliseconds, causing every
238 | subtitle to appear 12 seconds earlier.
239 |
240 | The time format may be an integer (milliseconds),
241 | or a standard `HH:MM:SS` format. Note that milliseconds
242 | in `HH:MM:SS` format appear in two different formats:
243 | - `.srt`: `HH:MM:SS,mmm`
244 | - `.ass/.ssa`: `HH:MM:SS.nn`
245 | See the earlier [Time Formats](#time-formats) section for details.
246 |
247 | To simplify calculations, `subsync` supports timestamp arithmetic
248 | as the offset parameter.
249 | For example, you can pick any misaligned subtitle in the subtitle
250 | file, then find the correct line’s start time in the video, and
251 | subtract the subtitle timestamp from the correct video timestamp:
252 | ```
253 | subsync +00:00:52,570-0:11:00,140 source.ass > target.ass
254 | ```
255 | In this offset parameter `+00:00:52,570-0:11:00,140`:
256 | - The leading `+` is ignored; using `-` makes no difference
257 | - `00:00:52,570` is the correct start time of the dialogue in the video
258 | - `0:11:00,140` is the incorrect start time in the subtitle file
259 | - `subsync` calculates the difference automatically,
260 | saving you the manual calculation
261 | - The calculation formula is: `expected time − misaligned time`
262 | - `expected time` and `misaligned time` can use different time formats.
263 |
264 | # HOWTO: Time Scale
265 | Some subtitles cannot be corrected by simply shifting timestamps.
266 | In these cases, the key symptom is that subtitles are synchronized
267 | at the beginning, but gradually drift as the video progresses,
268 | sometimes appearing later, sometimes earlier.
269 | This type of mismatch is usually caused by frame rate differences:
270 | for example, the video may be from an NTSC source while the subtitles
271 | were created for a PAL source, creating a 30 fps vs. 25 fps mismatch.
272 | Some films even use the original 24 fps cinema master, causing
273 | desynchronization with both PAL and NTSC subtitles.
274 |
275 | The solution is subsync’s timestamp scaling feature.
276 | Timestamp scaling multiplies every subtitle timestamp by a correction
277 | factor to compensate for the drift. For example:
278 | ```
279 | subsync -1.000955 source.ass > target.ass
280 | ```
281 | If the scaling factor is greater than 1, subtitles drift later over time.
282 | If the factor is less than 1, subtitles drift earlier over time.
283 |
284 | The plus/minus sign does not affect scaling, `-1.000955` and
285 | `+1.000955` are equivalent.
286 |
287 | Since scaling parameters are floating-point numbers, they rarely conflict
288 | with offset parameters. If a conflict does occur, scaling takes precedence
289 | over offset.
290 |
291 | The scaling coefficient is computed as:
292 | ```
293 | expected_time / misaligned_time
294 | ```
295 | For example, suppose the subtitles are synchronized at the beginning,
296 | but the last subtitle appears at `1:35:26,690`, while the actual
297 | spoken line occurs at `1:35:32,160`.
298 | This means the subtitles gradually drift earlier.
299 | - Expected time: `1:35:32,160 = 5,732,160 ms`
300 | - Misaligned time: `1:35:26,690 = 5,726,690 ms`
301 | - Scaling factor: `5732160 / 5726690 ≈ 1.000955`
302 |
303 | To simplify the calculation, subsync supports timestamp division
304 | expressions directly as scaling parameters.
305 |
306 | Using the example above:
307 | ```
308 | subsync -1:35:32,160/1:35:26,690 source.ass > target.ass
309 | ```
310 | In this scaling parameter `-01:35:32,160/1:35:26,690`:
311 | - The `-` sign is ignored; `+` works the same
312 | - `01:35:32,160` is the correct dialogue start time in the video
313 | - `1:35:26,690` is the incorrect subtitle start time
314 | - subsync computes the ratio automatically, replacing manual calculation
315 | - The formula used is: `expected_time / misaligned_time`
316 | - The timestamps may mix with different formats:
317 | `-1:35:32,160/5726690` works the same
318 |
319 |
320 | # HOWTO: Non-linear Editing
321 | `subsync` supports basic non-linear editing, which is useful for
322 | fixing local subtitle offsets or gradual drift.
323 | When working with TV series, once you determine the correct adjustment
324 | formula for the first episode, you can often reuse the same parameters
325 | for other episodes, greatly improving efficiency.
326 |
327 | Use the `-s` option to specify the subtitle time range to process:
328 | ```
329 | -s start-time-stamp [end-time-stamp]
330 | ```
331 | - `start-time-stamp`: the starting point
332 | - `end-time-stamp`: the ending point (optional;
333 | if omitted, processing continues to the end of the file)
334 |
335 | Example
336 | ```
337 | subsync -s 0:01:15.00 1:23:34.00 -00:01:38,880-0:03:02.50 source.ass > target.ass
338 | ```
339 | - The processing range is `0:01:15.00` to `1:23:34.00`
340 | - The adjustment value comes from the expression `-00:01:38,880-0:03:02.50`
341 | - This expression evaluates to `–83.62` seconds, meaning the subtitles
342 | in the selected range are shifted `83.62` seconds `earlier`.
343 |
344 |
345 | # HOWTO: Batch Process
346 | `subsync` is easy to integrate into shell scripts. For example:
347 | ```
348 | for i in *.srt; do subsync +12000 "$i" > "$i.new"; done
349 | ```
350 | You can also use it directly. Use the `-o` option to overwrite
351 | the original file:
352 | ```
353 | subsync -o +12000 *.srt
354 | ```
355 | Or use `--overwrite` to overwrite the original file while keeping a backup:
356 | ```
357 | subsync --overwrite +12000 *.srt
358 | ```
359 |
360 |
361 | # Case Study
362 | ## EVA3.3 Theatrical Edition
363 | The video length of `EVA3.3 Theatrical Edition` is `1:32:52`.
364 | The subtitle file from Internet is a mess.
365 | Opening the subtitle file `00002.v1.11_FINAL.ass` we find the last
366 | dialogue line is:
367 | ```
368 | Dialogue: 0,1:45:42.51,1:45:46.48,Comment,,0,0,0,,♫Peace in time we've never had it so good\N安享和平 生活从未如此美好
369 | ```
370 | The subtitles extend about 10 minutes beyond the video;
371 | it likely contains scenes from the 巨神兵 segment.
372 |
373 | Anyway, we locate the first line of speech, which occurs at `00:00:52,570`. The English subtitle is accurate, giving us a precise timestamp:
374 | ```
375 | 00:00:52,570 Tracking team, report current Eva unit positions.
376 | ```
377 | The corresponding Chinese subtitle is:
378 | ```
379 | Dialogue: 0,0:11:00.14,0:11:02.98,Default,,0,0,0,,追踪班 报告两机体现在的位置
380 | ```
381 | We use subsync’s helper to compute the time difference:
382 | ```
383 | $ subsync --help-sub 00:00:52,570 0:11:00.14
384 | Time difference is -00:10:07,570 (-607570 ms)
385 | ```
386 |
387 | First, compensate that ~10-minute offset with a timestamp shift:
388 | ```
389 | $ subsync -00:10:07,570 00002.v1.11_FINAL.ass > 001.ass
390 | ```
391 | Testing `001.ass`: the front is now synchronized, but drift appears later;
392 | so frame rate adjustment is also needed.
393 |
394 | Jumping to the end of the video, we find the last line of dialogue at:
395 | ```
396 | 01:35:32,160 The Wunder streaks through the sky
397 | ```
398 | The matching Chinese line is:
399 | ```
400 | Dialogue: 0,1:35:26.69,1:35:28.07,Default,,0,0,0,,划破天际的Wunder
401 | ```
402 | Using subsync’s helper to compute the scale factor:
403 | ```
404 | $ subsync --help-div 01:35:32,160 1:35:26.69
405 | Time scale factor is 1.000955
406 | ```
407 | This ratio is close to 24 / 23.976, which likely explains the drift.
408 | Apply the scale factor to the previously produced 001.ass:
409 | ```
410 | $ subsync -1.000955 001.ass > 002.ass
411 | ```
412 |
413 | Playing the video with `002.ass` now shows correct synchronization.
414 |
415 | As a final check, the combined automatic command also produces
416 | the same result:
417 | ```
418 | subsync +00:00:52,570-0:11:00,140 -01:35:32,160/1:35:26,690 00002.v1.11_FINAL.ass > 002.ass
419 | ```
420 | The verification shows the automatic calculation matches the manual steps.
421 |
422 | ## 不可思议的海之娜蒂亚
423 | This series has 39 episodes, and all of the subtitles are out of sync.
424 | The video files are in MKV format and include embedded English subtitles.
425 |
426 | First, extract the English subtitle track using `ffmpeg` for reference:
427 | ```
428 | ffmpeg -i "Nadia Ep 01.mkv" -map 0:s:0 subs.srt
429 | ```
430 | We can see the subtitles are correctly synchronized at the beginning.
431 | In English subtitle track `subs.srt`:
432 | ```
433 | 1
434 | 00:00:03,200 --> 00:00:05,900
435 | Are you adventurers,
436 | ```
437 | The corresponding Chinese subtitle:
438 | ```
439 | Dialogue: 0,0:00:03.50,0:00:05.50,*Default,,0000,0000,0000,,你是一位冒险家吗
440 | ```
441 | However, starting from subtitle line 18, the sync breaks:
442 | ```
443 | 18
444 | 00:01:09,630 --> 00:01:12,460
445 | as the threat of a world war loomed ever closer.
446 |
447 | 19
448 | 00:01:38,880 --> 00:01:41,040
449 | Paris... Paris...
450 | ```
451 |
452 | The corresponding Chinese subtitles:
453 | ```
454 | Dialogue: 0,0:01:08.00,0:01:11.80,*Default,,0000,0000,0000,,但是人们生活在 即将来临的世界阴影中
455 | Dialogue: 0,0:03:02.50,0:03:04.90,*Default,,0000,0000,0000,,巴黎…巴黎…
456 | ```
457 |
458 | The video interval is only 30 seconds, but the subtitle interval is
459 | over 2 minutes, which strongly suggests the Chinese subtitles were
460 | created from a version with a different opening sequence.
461 |
462 | We can try repairing this section using `subsync`:
463 | ```
464 | subsync -s 0:01:15.00 -00:01:38,880-0:03:02.50 "Nadia Ep 01.ass" > 01.ass
465 | ```
466 |
467 | The result displays correctly; no timeline scaling is required.
468 |
469 | However, Episode 1 is different from later episodes: it contains
470 | an opening monologue that does not appear again, but the Chinese
471 | subtitles still include those lines.
472 |
473 | Therefore, starting from Episode 2, we manually delete the first
474 | 18 subtitle lines in a text editor, then fix the timeline with `subsync`:
475 | ```
476 | mv 01.ass 01.bak
477 | subsync -00:00:01,710-00:01:25,510 -o *.ass
478 | mv 01.bak 01.ass
479 | ```
480 |
481 | This corrects the timing for all episodes except the first, which uses
482 | its own specific adjustment.
483 |
484 |
485 | ## 工作细胞 Black
486 | I did a quick preview and felt the subtitles were about 1 second early,
487 | so I first adjusted them with:
488 | ```
489 | $ subsync +1000 -o *.ass
490 | ```
491 |
492 | After closer inspection I found the first ~10 minutes were synced,
493 | but later the subtitles were about 6 seconds early. I located a 6-second
494 | black cutaround at 10:38, so I applied a localized fix:
495 | ```
496 | $ subsync -s 10:38 +6000 --overwrite 'Hataraku Saibou Black_-_01.ass'
497 | ```
498 |
499 | Re-checking the video, the subtitles were then fully synchronized.
500 |
501 | The annoying part is that, although the black-screen segment is the
502 | same duration in every episode, its position differs per episode;
503 | so you must locate it episode by episode. The procedure is:
504 | - Play the episode and jump roughly to the middle.
505 | - If subtitles are in sync there, search forward;
506 | if they are out of sync, search backward.
507 | - Find the start time of the black-screen segment and note its time.
508 | - Run `subsync -s +6000` on that episode.
509 |
510 | So the command-line pattern looks like this:
511 | ```
512 | $ subsync -s 10:38 +6000 --overwrite 'Hataraku Saibou Black_-_01.ass'
513 | $ subsync -s 10:44 +6000 -o 'Hataraku Saibou Black_-_02.ass'
514 | $ subsync -s 11:20 +6000 -o 'Hataraku Saibou Black_-_03.ass'
515 | $ subsync -s 11:32 +6000 -o 'Hataraku Saibou Black_-_04.ass'
516 | ```
517 |
518 | ## NaNa (2005)/娜娜/世上的另一个我
519 | For `Nana`, the first 10 minutes of subtitles are correct,
520 | but in the last 10 minutes they lag by 3 to 4 seconds.
521 | Two aspects make this case more troublesome:
522 | - There is no transition cut; it seems to have been removed entirely.
523 | - The delay amount is inconsistent, anywhere from 3 to 5 seconds.
524 |
525 | In this situation, you must manually locate where the subtitle
526 | delay starts, and then fine-tune the offset.
527 |
528 | For example, in Episode 3, the subtitles are still in sync at `12:57`,
529 | but at `13:17` they already lag by more than 2 seconds.
530 | After checking back and forth, I found a scene cut at `13:09`,
531 | confirming this is the deletion point.
532 |
533 | First, I tried shifting subtitles 3 seconds earlier:
534 | ```
535 | $ subsync -s 13:10 -3000 -o 'S01E03-Nana and Shoji, Love'\''s Whereabouts [030F3BD0].ass'
536 | ```
537 | The result was slightly ahead of the video, so I tried delaying
538 | by 0.5 second at the same position:
539 | ```
540 | $ subsync -s 13:10 +500 -o 'S01E03-Nana and Shoji, Love'\''s Whereabouts [030F3BD0].ass'
541 | ```
542 | This time the subtitles appeared properly synchronized.
543 | In general, a timing error within 0.5s is acceptable.
544 |
545 | The first three episodes take more effort because you must adjust
546 | repeatedly. Once you understand the pattern, later episodes become
547 | much faster to process.
548 |
549 | Here is the actual command sequence I used:
550 | ```
551 | $ subsync -s 8:20 -3500 -o S01E04-*.ass
552 | $ subsync -s 10:43 -3500 -o S01E05-*.ass
553 | $ subsync -s 11:13 -3500 -o S01E06-*.ass
554 | $ subsync -s 11:13 -500 -o S01E06-*.ass
555 | $ subsync -s 10:36 -4000 -o S01E07-*.ass
556 | $ subsync -s 10:36 +500 -o S01E07-*.ass
557 | $ subsync -s 10:36 +500 -o S01E07-*.ass
558 | $ subsync -s 11:20 -3500 -o S01E08-*.ass
559 | $ subsync -s 11:00 -3000 -o S01E09-*.ass
560 | $ subsync -s 11:00 +500 -o S01E09-*.ass
561 | $ subsync -s 12:16 -2500 -o S01E10-*.ass
562 | $ subsync -s 13:04 -3500 -o S01E11-*.ass
563 | $ subsync -s 13:04 +500 -o S01E11-*.ass
564 | $ subsync -s 10:12 -3500 -o S01E12-*.ass
565 | $ subsync -s 10:12 +500 -o S01E12-*.ass
566 | ```
567 |
568 |
--------------------------------------------------------------------------------
/subsync.c:
--------------------------------------------------------------------------------
1 |
2 | /* subsync.c -- resync the subtitle's time stamps
3 | Copyright (C) 2009-2025 "Andy Xuming"
4 |
5 | This file is part of Subsync, a utility to resync subtitle files
6 |
7 | Subsync is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | Subsync is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 |
28 | struct ScRate {
29 | char *id;
30 | double fact;
31 | } srtbl[6] = {
32 | { "N-P", 1.1988 }, /* NTSC to PAL frame rate 29.97/25 */
33 | { "P-N", 0.83417 }, /* PAL to NTSC frame rate 25/29.97 */
34 | { "N-C", 1.25 }, /* NTSC to Cinematic frame rate 29.97/23.976 */
35 | { "C-N", 0.8 }, /* Cinematic to NTSC frame rate 23.976/29.97 */
36 | { "P-C", 1.04271 }, /* PAL to Cinematic frame rate 25/23.976 */
37 | { "C-P", 0.95904 }, /* Cinematic to PAL frame rate 23.976/25 */
38 | };
39 |
40 |
41 |
42 | static char bom_overflow[8]; /* [0]: number [1]: contents */
43 | static char bom_user_defined[64];
44 | static int utf_index = -1;
45 | static iconv_t utf_iconv = (iconv_t) -1;
46 |
47 | static struct CodePG {
48 | char magic[4];
49 | int magic_len;
50 | char *iconv_name;
51 | int width;
52 | int endian; /* 0: LE 1: BE */
53 | } bom_codepage[] = {
54 | { "\xEF\xBB\xBF", 3, "UTF-8", 1, 0 },
55 | { "\xFE\xFF", 2, "UTF-16BE", 2, 1 },
56 | { "\xFF\xFE", 2, "UTF-16LE", 2, 0 },
57 | { "\x00\x00\xFE\xFF", 4, "UTF-32BE", 4, 1 },
58 | { "\xFF\xFE\x00\x00", 4, "UTF-32LE", 4, 0 },
59 | { "\x2B\x2F\x76", 3, "UTF-7", 1, 0 },
60 | { "\xF7\x64\x4C", 3, "UTF-1", 1, 0 },
61 | { "\xDD\x73\x66\x73", 4, "UTF-EBCDIC", 1, 0 },
62 | { "\x84\x31\x95\x33", 4, "GB18030", 1, 0 },
63 | { "", 0, bom_user_defined, 1, 0 }
64 | };
65 | #define BOMLEN (sizeof(bom_codepage)/sizeof(struct CodePG) - 1)
66 |
67 |
68 | char *subsync_help = "\
69 | usage: subsync [OPTION] [sutitle_file]\n\
70 | OPTION:\n\
71 | -c, --chop N:M chop the specified number of subtitles (from 1)\n\
72 | -e, --encoding ENCODE default encoding (iconv name)\n\
73 | -o overwrite the original file (no backup file)\n\
74 | --overwrite overwrite the original file (has backup file)\n\
75 | -r, --reorder [NUM] reorder the serial number (SRT only)\n\
76 | -s, --span TIME [TIME] specifies the span of the time stamps for processing\n\
77 | -w, --write FILENAME write to the specified file\n\
78 | -/+OFFSET specifies the offset of the time stamps\n\
79 | -SCALE specifies the scale ratio of the time stamps\n\
80 | --help, --version\n\
81 | --help-example\n\
82 | TIME:\n\
83 | Two time stamp formats are recognizable:\n\
84 | SRT format HH:MM:SS,mmm, for example, 0:0:10,199\n\
85 | ASS format HH:MM:SS.mm, for example, 1:0:12.66\n\
86 | Note that all 4 time sections are required. Can be filled 0 like 0:0:12,000\n\
87 | OFFSET:\n\
88 | Time stamp offset; the prefix '+' or '-' defines delay or bring forward.\n\
89 | It can be defined by milliseconds: +19700, -10000\n\
90 | or by time stamp noting HH:MM:SS.MS: -0:0:10,199, +1:0:12.66\n\
91 | or by time stamp subtraction, the expect time stamp minus the actual\n\
92 | time stamp, for example: +01:44:31,660-01:44:36,290\n\
93 | SCALE:\n\
94 | Time stamp scaling ratio; tweak the time stamp from different frame rates,\n\
95 | for example, between PAL(25), NTSC(29.97) and Cinematic(23.976).\n\
96 | It can be defined by real number: 1.1988; or by predefined identifiers:\n\
97 | N-P(1.1988), P-N(0.83417), N-C(1.25), C-N(0.8), P-C(1.04271), C-P(0.95904)\n\
98 | or by time stamp dividing, the expect time stamp divided by the actual\n\
99 | time stamp, for example: -01:44:30,290/01:44:31,660\n\
100 | ";
101 |
102 | char *subsync_help_extra = "\
103 | Debug Options:\n\
104 | --help-subtract calculate the time offset\n\
105 | --help-divide calculate the scale ratio of time stamps\n\
106 | --help-strtoms test reading the time stamps\n\
107 | --help-debug display the internal arguments\n\
108 | --help-example display the example\n\
109 | ";
110 |
111 | char *subsync_help_example = "\
112 | Examples:\n\
113 | Delay the subtitles for 12 seconds:\n\
114 | subsync +12000 source.ass > target.ass\n\
115 | Bring forward the subtitles for 607570 milliseconds:\n\
116 | subsync -00:10:07,570 source.ass > target.ass\n\
117 | Shifting the subtitles by (expected - actual) time stamps:\n\
118 | subsync +00:00:52,570-0:11:00,140 source.ass > target.ass\n\
119 | Which is identical to:\n\
120 | subsync -00:00:52,570-0:11:00,140 -w target.ass source.ass\n\
121 | Zooming the time stamps of the subtitles with a scale ratio of 1.000955:\n\
122 | subsync -1.000955 -w target.ass source.ass\n\
123 | Which is identical to (expected / actual) time stamps:\n\
124 | subsync -01:35:32,160/1:35:26,690 source.ass > target.ass\n\
125 | Shifting the subtitles and zoom its intervals, print in screen:\n\
126 | subsync +00:00:52,570-0:11:00,140 -01:35:32,160/1:35:26,690 source.ass\n\
127 | Shifting the subtitles from 1 minute 15 seconds to the end:\n\
128 | subsync -s 0:01:15.00 -00:01:38,880-0:03:02.50 source.ass > target.ass\n\
129 | Batch shifting the subtitles and overwrite the original files:\n\
130 | subsync -00:00:01,710-00:01:25,510 -o *.srt\n\
131 | ";
132 |
133 | char *subsync_version = "Subsync %s\n\
134 | Copyright (C) 2009-2025 \"Andy Xuming\" \n\
135 | This program comes with ABSOLUTELY NO WARRANTY.\n\
136 | This is free software, and you are welcome to redistribute it under certain\n\
137 | conditions. For details see see `LICENSE'.\n";
138 |
139 | time_t tm_offset = 0;
140 | double tm_scale = 0.0;
141 | time_t tm_range[2] = { -1, -1 };
142 | int tm_chop[2] = { -1, -1 };
143 | int tm_srtsn = -1; /* -1: not to orderize SRT sn */
144 | int tm_overwrite = 0; /* 1: overwrite 2: overwrite and backup */
145 |
146 |
147 | static int retiming(FILE *fin, FILE *fout);
148 | static int utf_open(FILE *fin, FILE *fout, int cp);
149 | static int utf_readline(FILE *fin, char *buf, int len);
150 | static int utf_lr(char *s);
151 | static int utf_bom_detect(FILE *fin);
152 | static int utf_bom_user_defined(char *s);
153 | static time_t tweaktime(time_t ms);
154 | static int chop_filter(char *s, int *magic);
155 | static time_t strtoms(char *s, int *len, int *style);
156 | static char *mstostr(time_t ms, int style);
157 | static time_t timetoms(int hour, int min, int sec, int msec);
158 | static double arg_scale(char *s);
159 | static time_t arg_offset(char *s);
160 | static int isnumber(char *s);
161 | static int mocker(FILE *fin, char *argv);
162 | static int help_tools(int argc, char **argv);
163 | static void test_str_to_ms(void);
164 |
165 | #define MOREARG(c,v) { \
166 | --(c), ++(v); \
167 | if (((c) == 0) || (**(v) == '-') || (**(v) == '+')) { \
168 | fprintf(stderr, "missing parameters\n"); \
169 | return -1; \
170 | }\
171 | }
172 |
173 |
174 | int main(int argc, char **argv)
175 | {
176 | FILE *fin = NULL, *fout = NULL;
177 | char *oname, mock_option[32] = "";
178 |
179 | while (--argc && ((**++argv == '-') || (**argv == '+'))) {
180 | if (!strcmp(*argv, "-V") || !strcmp(*argv, "--version")) {
181 | printf(subsync_version, VERSION);
182 | return 0;
183 | } else if (!strcmp(*argv, "-H") || !strcmp(*argv, "--help")) {
184 | puts(subsync_help);
185 | return 0;
186 | } else if (!strncmp(*argv, "--help-", 7)) {
187 | return help_tools(argc, argv);
188 | } else if (!strncmp(*argv, "--mock-", 7)) {
189 | strncpy(mock_option, *argv, sizeof(mock_option)-1);
190 | } else if (!strcmp(*argv, "-o")) {
191 | tm_overwrite = 1; /* no backup */
192 | } else if (!strcmp(*argv, "--overwrite")) {
193 | tm_overwrite = 2; /* has backup */
194 | } else if (!strcmp(*argv, "-c") || !strcmp(*argv, "--chop")) {
195 | MOREARG(argc, argv);
196 | if (sscanf(*argv, "%d : %d", tm_chop, tm_chop + 1) != 2) {
197 | tm_chop[0] = tm_chop[1] = -1;
198 | }
199 | } else if (!strcmp(*argv, "-e") || !strcmp(*argv, "--encoding")) {
200 | MOREARG(argc, argv);
201 | utf_index = utf_bom_user_defined(*argv);
202 | } else if (!strcmp(*argv, "-r") || !strcmp(*argv, "--reorder")) {
203 | if ((argc > 0) && isnumber(argv[1])) {
204 | --argc; tm_srtsn = (int)strtol(*++argv, NULL, 0);
205 | } else {
206 | tm_srtsn = 1; /* set as default */
207 | }
208 | } else if (!strcmp(*argv, "-s") || !strcmp(*argv, "--span")) {
209 | MOREARG(argc, argv);
210 | tm_range[0] = arg_offset(*argv);
211 | /* the second parameter is optional, must begin in number */
212 | if ((argc > 0) && isdigit(argv[1][0])) {
213 | --argc; tm_range[1] = arg_offset(*++argv);
214 | }
215 | } else if (!strcmp(*argv, "-w") || !strcmp(*argv, "--write")) {
216 | MOREARG(argc, argv);
217 | if ((fout = fopen(*argv, "w")) == NULL) {
218 | perror(*argv);
219 | }
220 | } else if (!strcmp(*argv, "--")) {
221 | break;
222 | } else if (arg_scale(*argv) != 0) {
223 | tm_scale = arg_scale(*argv);
224 | } else if (arg_offset(*argv) != -1) {
225 | tm_offset = arg_offset(*argv);
226 | } else {
227 | fprintf(stderr, "%s: unknown parameter.\n", *argv);
228 | return -1;
229 | }
230 | }
231 | if ((tm_offset == 0) && (tm_scale == 0) && (tm_srtsn < 0) &&
232 | (tm_chop[0] < 0) && (tm_chop[1] < 0)) {
233 | puts(subsync_help);
234 | return 0;
235 | }
236 |
237 | /* input from stdin */
238 | if ((argc == 0) || !strcmp(*argv, "--")) {
239 | if (mock_option[0]) {
240 | mocker(stdin, mock_option);
241 | } else if (fout == NULL) {
242 | retiming(stdin, stdout);
243 | } else {
244 | retiming(stdin, fout);
245 | fclose(fout);
246 | }
247 | return 0;
248 | }
249 |
250 | /* don't overwrite but still batch processing
251 | * what's the point of this ??? */
252 | if (!tm_overwrite) {
253 | if (fout == NULL) {
254 | fout = stdout;
255 | }
256 | for ( ; argc; argc--, argv++) {
257 | if ((fin = fopen(*argv, "r")) == NULL) {
258 | perror(*argv);
259 | continue;
260 | }
261 | if (mock_option[0]) {
262 | mocker(fin, mock_option);
263 | } else {
264 | retiming(fin, fout);
265 | }
266 | fclose(fin);
267 | }
268 | if (fout != stdout) {
269 | fclose(fout);
270 | }
271 | return 0;
272 | }
273 |
274 | /* the overwrite option override the write option */
275 | if (fout != NULL) {
276 | fclose(fout);
277 | }
278 |
279 | for ( ; argc; argc--, argv++) {
280 | if ((oname = malloc(strlen(*argv)+16)) == NULL) {
281 | break;
282 | }
283 | strcpy(oname, *argv);
284 | strcat(oname, ".bak");
285 | rename(*argv, oname); /* backup the original first */
286 |
287 | if ((fin = fopen(oname, "r")) == NULL) {
288 | perror(oname);
289 | free(oname);
290 | continue;
291 | }
292 |
293 | /* create the output file by its original name */
294 | if ((fout = fopen(*argv, "w")) == NULL) {
295 | perror(*argv);
296 | free(oname);
297 | fclose(fin);
298 | continue;
299 | }
300 | if (mock_option[0]) {
301 | mocker(fin, mock_option);
302 | } else {
303 | retiming(fin, fout);
304 | }
305 | fclose(fout);
306 | fclose(fin);
307 |
308 | if (tm_overwrite == 1) { /* not required to backup */
309 | unlink(oname);
310 | }
311 | free(oname);
312 | }
313 | return 0;
314 | }
315 |
316 | static int retiming(FILE *fin, FILE *fout)
317 | {
318 | char buf[4096], *s;
319 | time_t ms;
320 | int n, style, srtsn;
321 | int magic = -1; /* -1: uncertain 0: SRT 1: SSA */
322 |
323 | utf_open(fin, fout, 0);
324 |
325 | srtsn = tm_srtsn;
326 | while (utf_readline(fin, buf, sizeof(buf)-1) > 0) {
327 | if (chop_filter(buf, &magic)) {
328 | continue; /* skip the specified subtitles */
329 | }
330 |
331 | /* skip and output the whitespaces */
332 | for (s = buf; (*s > 0) && (*s <= 0x20); s++) fputc(*s, fout);
333 |
334 | /* SRT: 00:02:17,440 --> 00:02:20,375
335 | * ASS: Dialogue: Marked=0,0:02:42.42,0:02:44.15,Wolf main,
336 | * autre,0000,0000,0000,,Toujours rien. */
337 | if (!strncmp(s, "Dialogue:", 9)) { /* ASS/SSA timestamp */
338 | /* output everything before the first timestamp */
339 | while (*s != ',') fputc(*s++, fout);
340 | /* output the ',' also */
341 | fputc(*s++, fout);
342 | /* read and skip the first timestamp */
343 | ms = strtoms(s, &n, &style);
344 | s += n;
345 | /* output the tweaked timestamp */
346 | fputs(mstostr(tweaktime(ms), style), fout);
347 | /* output everything before the second timestamp */
348 | while (*s != ',') fputc(*s++, fout);
349 | /* output the ',' also */
350 | fputc(*s++, fout);
351 | /* read and skip the second timestamp */
352 | ms = strtoms(s, &n, &style);
353 | s += n;
354 | /* output the tweaked timestamp */
355 | fputs(mstostr(tweaktime(ms), style), fout);
356 | } else if ((ms = strtoms(s, &n, &style)) != -1) { /* SRT timestamp */
357 | /* skip the first timestamp */
358 | s += n;
359 | /* output the tweaked timestamp */
360 | fputs(mstostr(tweaktime(ms), style), fout);
361 |
362 | /* output everything before the second timestamp */
363 | while (*s && !isdigit(*s)) fputc(*s++, fout);
364 | /* read and skip the second timestamp */
365 | ms = strtoms(s, &n, &style);
366 | s += n;
367 | /* output the tweaked timestamp */
368 | fputs(mstostr(tweaktime(ms), style), fout);
369 | } else if ((srtsn > 0) && isnumber(s)) {
370 | /* SRT serial numbers to be re-ordered */
371 | fprintf(fout, "%d", srtsn++);
372 | while (isdigit(*s)) s++;
373 | }
374 | /* output rest of things */
375 | fputs(s, fout);
376 | }
377 |
378 | if (utf_iconv != (iconv_t) -1) {
379 | iconv_close(utf_iconv);
380 | }
381 | return 0;
382 | }
383 |
384 | static int utf_open(FILE *fin, FILE *fout, int cp)
385 | {
386 | int n;
387 |
388 | if ((n = utf_bom_detect(fin)) >= 0) {
389 | utf_index = n;
390 | }
391 | if (utf_index < 0) {
392 | /* no codepage specified: default IO */
393 | return utf_index;
394 | }
395 | //printf("utf_open: %d %d\n", utf_index, cp);
396 | if (utf_index != cp) {
397 | /* different input/output codepage: need iconv */
398 | utf_iconv = iconv_open(bom_codepage[cp].iconv_name,
399 | bom_codepage[utf_index].iconv_name);
400 | if (utf_iconv == (iconv_t) -1) {
401 | return utf_index;
402 | }
403 | }
404 |
405 | /* same input/output codepage or successful iconv,
406 | * probably need to write a BOM explicity
407 | * except UTF-8 and User defined codepage */
408 | if ((cp > 0) && (cp < BOMLEN)) {
409 | fwrite(bom_codepage[cp].magic, 1,
410 | bom_codepage[cp].magic_len, fout);
411 | }
412 | return utf_index;
413 | }
414 |
415 | static int utf_readline(FILE *fin, char *buf, int len)
416 | {
417 | size_t in_bytes_left, out_bytes_left;
418 | char rbuf[4090], *in_buf;
419 | int i;
420 |
421 | if ((utf_index < 0) || (bom_codepage[utf_index].width == 1)) {
422 | if (bom_overflow[0]) {
423 | i = bom_overflow[0];
424 | bom_overflow[0] = 0; /* free the overflow buffer */
425 | /* 0xa would stop BOM searching anyway so it must be
426 | * the last item in the BOM overflow buffer */
427 | if (bom_overflow[i] == 0xa) {
428 | memcpy(buf, &bom_overflow[1], i);
429 | buf[i] = 0;
430 | return i;
431 | }
432 | memcpy(rbuf, &bom_overflow[1], i);
433 | rbuf[i] = 0;
434 | fgets(&rbuf[i], sizeof(rbuf)-i-1, fin);
435 | } else if (fgets(rbuf, sizeof(rbuf)-1, fin) == NULL) {
436 | return -1;
437 | }
438 | if (utf_iconv == (iconv_t) -1) {
439 | strncpy(buf, rbuf, len - 1);
440 | return strlen(buf);
441 | }
442 | in_bytes_left = strlen(rbuf);
443 | out_bytes_left = len;
444 | in_buf = (char*)rbuf;
445 | iconv(utf_iconv, &in_buf, &in_bytes_left, &buf, &out_bytes_left);
446 | len -= (int)out_bytes_left;
447 | *buf = 0;
448 | return len;
449 | }
450 |
451 | i = 0;
452 | while (fread(&rbuf[i], bom_codepage[utf_index].width, 1, fin)) {
453 | if (utf_lr(&rbuf[i])) {
454 | i += bom_codepage[utf_index].width;
455 | break;
456 | }
457 | i += bom_codepage[utf_index].width;
458 | }
459 | if (utf_iconv == (iconv_t) -1) {
460 | perror("iconv");
461 | return 0;
462 | }
463 |
464 | in_bytes_left = i;
465 | out_bytes_left = len;
466 | in_buf = (char*)rbuf;
467 | iconv(utf_iconv, &in_buf, &in_bytes_left, &buf, &out_bytes_left);
468 | len -= (int)out_bytes_left;
469 | *buf = 0;
470 | return len;
471 | }
472 |
473 | static int utf_lr(char *s)
474 | {
475 | if ((utf_index < 0) || (bom_codepage[utf_index].width == 1)) {
476 | return (*s == 0xa);
477 | }
478 | if (bom_codepage[utf_index].width == 2) {
479 | if (bom_codepage[utf_index].endian == 0) { /* LE */
480 | return (!memcmp(s, "\xa\0", 2));
481 | } else {
482 | return (!memcmp(s, "\0\xa", 2));
483 | }
484 | }
485 | if (bom_codepage[utf_index].endian == 0) { /* LE */
486 | return (!memcmp(s, "\xa\0\0\0", 4));
487 | }
488 | return (!memcmp(s, "\0\0\0\xa", 4));
489 | }
490 |
491 | static int utf_bom_detect(FILE *fin)
492 | {
493 | char buf[8];
494 | int i, k, n;
495 |
496 | for (i = 0; i < 4; i++) {
497 | buf[i] = (char) fgetc(fin);
498 | n = i + 1;
499 | for (k = 0; k < BOMLEN; k++) {
500 | if (!memcmp(bom_codepage[k].magic, buf, n)) {
501 | if (bom_codepage[k].magic_len > n) {
502 | break; /* partial matching */
503 | }
504 | return k; /* the matching codepage */
505 | }
506 | }
507 | if (k == BOMLEN) { /* no match found */
508 | memcpy(&bom_overflow[1], buf, n);
509 | bom_overflow[0] = n;
510 | break;
511 | }
512 | }
513 | return -1;
514 | }
515 |
516 | static int utf_bom_user_defined(char *s)
517 | {
518 | int i;
519 |
520 | for (i = 0; i < BOMLEN; i++) {
521 | if (!strcasecmp(bom_codepage[i].iconv_name, s)) {
522 | return i;
523 | }
524 | }
525 | strncpy(bom_codepage[i].iconv_name, s, sizeof(bom_user_defined));
526 | if (strstr(s, "16")) {
527 | bom_codepage[i].width = 2;
528 | } else if (strstr(s, "32")) {
529 | bom_codepage[i].width = 4;
530 | }
531 | if (strstr(s, "BE") || strstr(s, "be")) {
532 | bom_codepage[i].endian = 1;
533 | }
534 | return i;
535 | }
536 |
537 | static time_t tweaktime(time_t ms)
538 | {
539 | if (tm_range[0] > -1) { /* check the time stamp range */
540 | if (ms < tm_range[0]) {
541 | return ms;
542 | }
543 | if ((tm_range[1] > -1) && (ms > tm_range[1])) {
544 | return ms;
545 | }
546 | }
547 | if (tm_offset) {
548 | ms += tm_offset;
549 | }
550 | if (tm_scale != 0.0) {
551 | ms *= tm_scale;
552 | }
553 | return ms;
554 | }
555 |
556 | static int chop_filter(char *s, int *magic)
557 | {
558 | static int subidx;
559 |
560 | if ((tm_chop[0] < 0) && (tm_chop[1] < 0)) {
561 | return 0; /* disabled */
562 | }
563 |
564 | switch (*magic) {
565 | case 0: /* subrip */
566 | if (isnumber(s)) {
567 | subidx++;
568 | }
569 | //printf("SRT %d\n", subidx);
570 | if ((tm_chop[0] > 0) && (subidx < tm_chop[0])) {
571 | break; /* no chop */
572 | }
573 | if ((tm_chop[1] > 0) && (subidx > tm_chop[1])) {
574 | break; /* no chop */
575 | }
576 | return 1;
577 | case 1: /* ASS/SSA */
578 | if (strncmp(s, "Dialogue:", 9)) {
579 | break;;
580 | }
581 | subidx++;
582 | //printf("ASS %d\n", subidx);
583 | if ((tm_chop[0] > 0) && (subidx < tm_chop[0])) {
584 | break; /* no chop */
585 | }
586 | if ((tm_chop[1] > 0) && (subidx > tm_chop[1])) {
587 | break; /* no chop */
588 | }
589 | return 1;
590 | default:
591 | if (*magic > 0) {
592 | break; /* something wrong */
593 | }
594 | if (isnumber(s)) {
595 | *magic = 0;
596 | subidx++;
597 | } else if (strtoms(s, NULL, NULL) != -1) { /* SRT timestamp */
598 | *magic = 0;
599 | subidx++;
600 | } else if (!strncmp(s, "[Events]", 8)) {
601 | *magic = 1;
602 | break;
603 | } else if (!strncmp(s, "[Script Info]", 13)) {
604 | *magic = 1;
605 | break;
606 | } else if (!strncmp(s, "Dialogue:", 9)) {
607 | *magic = 1;
608 | subidx++;
609 | } else {
610 | break;
611 | }
612 | if ((tm_chop[0] > 0) && (subidx < tm_chop[0])) {
613 | break; /* no chop */
614 | }
615 | if ((tm_chop[1] > 0) && (subidx > tm_chop[1])) {
616 | break; /* no chop */
617 | }
618 | return 1;
619 | }
620 | return 0; /* no skip */
621 | }
622 |
623 | /* "%d : %d : %d , %d%n", SRT
624 | * "%d : %d : %d . %d%n", ASS/SSA
625 | * "%d : %d : %d : %d%n",
626 | * "%d . %d . %d . %d%n",
627 | * "%d - %d - %d - %d%n",
628 | */
629 | #define ISTMSEP(n) (((n) == ':') || ((n) == '-') || ((n) == '.') || ((n) == ','))
630 |
631 | static time_t strtoms(char *s, int *len, int *style)
632 | {
633 | time_t rc;
634 | char *sign, *lastpc, *begin = s;
635 | int i, tm[4];
636 |
637 | tm[0] = tm[1] = tm[2] = tm[3] = 0;
638 | while (isspace(*s)) s++; /* skip the front whitespace */
639 | sign = lastpc = s;
640 | if ((*s == '+') || (*s == '-')) { /* if the sign exists */
641 | s++;
642 | }
643 | for (i = 0; i < 4; i++, s++) {
644 | while (isspace(*s)) s++;
645 | if (ISTMSEP(*s)) {
646 | tm[i] = 0;
647 | } else if (isdigit(*s)) {
648 | tm[i] = (int) strtol(s, &s, 10);
649 | } else {
650 | break;
651 | }
652 | if (len) {
653 | *len = (int)(s - begin);
654 | }
655 | while (isspace(*s)) s++; /* skip the space between number and puncture */
656 | if (!ISTMSEP(*s)) {
657 | i++;
658 | break;
659 | } else if (i < 3) {
660 | lastpc = s;
661 | }
662 | }
663 |
664 | //printf("%s: %d-%d-%d-%d (%d)(%c)\n", sign, tm[0], tm[1], tm[2], tm[3], i, *lastpc);
665 | switch (i) {
666 | case 0: /* No number, like "abc" */
667 | rc = -1;
668 | break;
669 | case 1: /* one number with bad ending like "12-B" */
670 | rc = tm[0]; /* one number been defined as millisecond */
671 | break;
672 | case 2: /* could be 2:3 or 2.3 */
673 | if (*lastpc == ':') { /* Min : Sec */
674 | rc = timetoms(0, tm[0], tm[1], 0);
675 | } else {
676 | rc = timetoms(0, 0, tm[0],
677 | (*lastpc == '.') ? tm[1] * 10 : tm[1]);
678 | }
679 | break;
680 | case 3: /* could be 1:2:3 or 1:2.3 */
681 | if (*lastpc == ':') { /* Hour : Min : Sec */
682 | rc = timetoms(tm[0], tm[1], tm[2], 0);
683 | } else {
684 | rc = timetoms(0, tm[0], tm[1],
685 | (*lastpc == '.') ? tm[2] * 10 : tm[2]);
686 | }
687 | break;
688 | case 4: /* assumed being Hour : Min : Sec [?] Msec */
689 | rc = timetoms(tm[0], tm[1], tm[2],
690 | (*lastpc == '.') ? tm[3] * 10 : tm[3]);
691 | break;
692 | }
693 |
694 | if (style) {
695 | *style = (*lastpc == '.') ? 1 : 0;
696 | }
697 |
698 | if (*sign == '-') {
699 | rc = - rc;
700 | }
701 | return rc;
702 | }
703 |
704 | static char *mstostr(time_t ms, int style)
705 | {
706 | static char stmp[32];
707 | char *buf = stmp;
708 | int hh, mm, ss;
709 |
710 | if (ms < 0) {
711 | ms = -ms;
712 | *buf++ = '-';
713 | }
714 |
715 | hh = (int)(ms / 3600000L);
716 | ms %= 3600000L;
717 | mm = (int)(ms / 60000);
718 | ms %= 60000;
719 | ss = (int)(ms / 1000);
720 | ms %= 1000;
721 |
722 | switch (style) {
723 | case 1: /* ASS */
724 | sprintf(buf, "%d:%02d:%02d.%02ld", hh, mm, ss, (long)(ms / 10));
725 | break;
726 | case 2:
727 | sprintf(buf, "%02d:%02d:%02d:%03ld", hh, mm, ss, (long)ms);
728 | break;
729 | case 3:
730 | sprintf(buf, "%02d.%02d.%02d.%03ld", hh, mm, ss, (long)ms);
731 | break;
732 | case 4:
733 | sprintf(buf, "%02d-%02d-%02d-%03ld", hh, mm, ss, (long)ms);
734 | break;
735 | case 0: /* SRT */
736 | default:
737 | sprintf(buf, "%02d:%02d:%02d,%03ld", hh, mm, ss, (long)ms);
738 | break;
739 | }
740 | return stmp;
741 | }
742 |
743 |
744 | static time_t timetoms(int hour, int min, int sec, int msec)
745 | {
746 | time_t ms;
747 |
748 | if (hour < 0) {
749 | return -1;
750 | }
751 |
752 | ms = hour * 3600 * 1000;
753 |
754 | /* if hour not given, min is reasonable larger than 60 */
755 | if ((min < 0) || ((hour > 0) && (min > 59))) {
756 | return -1;
757 | } else {
758 | ms += min * 60 * 1000;
759 | }
760 |
761 | /* if hour and min not given, sec is reasonable larger than 60 */
762 | if ((sec < 0) || (((hour > 0) || (min > 0)) && (sec > 59))) {
763 | return -1;
764 | } else {
765 | ms += sec * 1000;
766 | }
767 |
768 | if (msec < 0) {
769 | return -1;
770 | }
771 | return ms + msec;
772 | }
773 |
774 |
775 | /* valid parameters:
776 | * [+-]N-P, [+-]P-N, [+-]N-C, [+-]C-N, [+-]P-C, [+-]C-P, [+-]0.1234
777 | * [+-]01:44:30,290/01:44:31,660
778 | * Note that all leading '+' and '-' are ignored because ratio is a scalar.
779 | */
780 | static double arg_scale(char *s)
781 | {
782 | int i;
783 | double tmp;
784 |
785 | /* skip the leading '+' or '-' */
786 | if ((*s == '+') || (*s == '-')) {
787 | s++;
788 | }
789 | /* search the identity table first for something like "N-P" */
790 | for (i = 0; i < sizeof(srtbl)/sizeof(struct ScRate); i++) {
791 | if (!strcmp(s, srtbl[i].id)) {
792 | return srtbl[i].fact;
793 | }
794 | }
795 | /* or calculate the scale ratio by the form of
796 | * 01:44:30,290/01:44:31,660 */
797 | if (strchr(s, '/')) {
798 | time_t mf, mt;
799 |
800 | if ((mf = strtoms(s, NULL, NULL)) == -1) {
801 | return 0.0;
802 | }
803 | s = strchr(s, '/');
804 | if ((mt = strtoms(++s, NULL, NULL)) == -1) {
805 | return 0.0;
806 | }
807 | return (double)mf / (double)mt;
808 | }
809 | /* or it's just a simple real number: 1.2345E12 */
810 | if (!strchr(s, ':') && strchr(s, '.')) {
811 | char *endp;
812 |
813 | tmp = strtod(s, &endp);
814 | if (*endp == 0) {
815 | return tmp;
816 | }
817 | }
818 | return 0.0;
819 | }
820 |
821 | /* valid parameters:
822 | * [+-]01:44:30,290, [+-]134600, [+-]01:44:31,660-01:44:30,290
823 | * Note that all leading '+' and '-' are required for vectoring
824 | */
825 | static time_t arg_offset(char *s)
826 | {
827 | char *endp;
828 | time_t ms;
829 |
830 | /* ignore the form of 01:44:30,290/01:44:31,660 because it's for scaling */
831 | if (strchr(s, '/')) {
832 | return -1;
833 | }
834 | /* seperate the form -01:44:31,660-01:44:30,290 from -01:44:31,660 */
835 | if (strchr(s+1, '-')) {
836 | s++; /* ignore the switch charactor '+' or '-' */
837 | if ((ms = strtoms(s, NULL, NULL)) == -1) {
838 | return -1;
839 | }
840 | s = strchr(s, '-');
841 | if (strtoms(++s, NULL, NULL) == -1) {
842 | return -1;
843 | }
844 | ms -= strtoms(s, NULL, NULL);
845 | return ms;
846 | }
847 | /* process the form of [+-]01:44:31,660 */
848 | if ((ms = strtoms(s, NULL, NULL)) != -1) {
849 | return ms;
850 | }
851 | /* or it's simply a number by milliseconds [+-]134600 */
852 | ms = strtol(s, &endp, 0);
853 | if (*endp == 0) {
854 | return ms;
855 | }
856 | return -1;
857 | }
858 |
859 | static int isnumber(char *s)
860 | {
861 | if (!isdigit(*s)) {
862 | return 0;
863 | }
864 | while (isdigit(*s)) s++;
865 | return (*s > 0x20) ? 0 : 1;
866 | }
867 |
868 | static void utf_dump(void)
869 | {
870 | int n;
871 |
872 | if ((n = utf_index) < 0) {
873 | printf("encoding not defined\n");
874 | } else {
875 | printf("%d_ %d %8s W:%d E:%d\n", n, bom_codepage[n].magic_len,
876 | bom_codepage[n].iconv_name, bom_codepage[n].width,
877 | bom_codepage[n].endian);
878 | }
879 | }
880 |
881 | static int mocker(FILE *fin, char *argv)
882 | {
883 | char buf[1024];
884 | int n;
885 |
886 | if (!strcmp(argv, "--mock-bom")) {
887 | n = utf_bom_detect(fin);
888 | if (n < 0) {
889 | printf("BOM not detected\n");
890 | } else {
891 | printf("BOM %s\n", bom_codepage[n].iconv_name);
892 | }
893 | } else if (!strcmp(argv, "--mock-encoding")) {
894 | utf_dump();
895 | } else if (!strcmp(argv, "--mock-open")) {
896 | utf_open(fin, stdout, 0);
897 | utf_dump();
898 | utf_open(fin, stdout, 1);
899 | utf_dump();
900 | } else if (!strcmp(argv, "--mock-lr")) {
901 | char *lrlst[] = { "\xa", "\xa\0", "\xa\0\0\0", "\0\xa", "\0\0\0\xa" };
902 | for (n = 0; n < sizeof(lrlst)/sizeof(char*); n++) {
903 | printf("LR: %02x (%ld): %s\n", *lrlst[n], (long)sizeof(lrlst[n]),
904 | utf_lr(lrlst[n]) ? "true" : "false");
905 | }
906 | } else if (!strcmp(argv, "--mock-readline")) {
907 | utf_open(fin, stdout, 0);
908 | utf_dump();
909 | n = utf_readline(fin, buf, sizeof(buf)-1);
910 | printf("%d %s\n", n, buf);
911 | }
912 | return 0;
913 | }
914 |
915 | static int help_tools(int argc, char **argv)
916 | {
917 | time_t ms;
918 | double tmp;
919 |
920 | if (!strcmp(*argv, "--help-strtoms")) {
921 | test_str_to_ms();
922 | } else if (!strncmp(*argv, "--help-subtract", 10)) {
923 | if (argc < 3) {
924 | fprintf(stderr, "Two time stamps required.\n");
925 | return 1;
926 | }
927 | ms = arg_offset(argv[1]);
928 | ms -= arg_offset(argv[2]);
929 | printf("Time difference is %s (%ld ms)\n",
930 | mstostr(ms, 0), (long)ms);
931 | } else if (!strncmp(*argv, "--help-divide", 10)) {
932 | if (argc < 3) {
933 | fprintf(stderr, "Two time stamps required.\n");
934 | return 1;
935 | }
936 | ms = arg_offset(argv[1]);
937 | tmp = (double)ms / (double)arg_offset(argv[2]);
938 | printf("Time scale ratio is %f\n", tmp);
939 | } else if (!strcmp(*argv, "--help-debug")) {
940 | printf("Time Stamp Offset: %ld\n", (long)tm_offset);
941 | printf("Time Stamp Scaling: %f\n", tm_scale);
942 | printf("Time Stamp range: from %ld to %ld\n",
943 | (long)tm_range[0], (long)tm_range[1]);
944 | printf("SRT serial Number: from %d\n", tm_srtsn);
945 | printf("Subtitle chopping: from %d to %d\n", tm_chop[0], tm_chop[1]);
946 | } else if (!strcmp(*argv, "--help-example")) {
947 | puts(subsync_help_example);
948 | } else {
949 | puts(subsync_help_extra);
950 | }
951 | return 0;
952 | }
953 |
954 | static void test_str_to_ms(void)
955 | {
956 | int i, n, style;
957 | time_t ms;
958 | char *testbl[] = {
959 | "00:02:09,996",
960 | "12:34:56,789",
961 | "1,2;3-456",
962 | "::5:123",
963 | "1:2:3",
964 | "12",
965 | "12,3",
966 | "12.3",
967 | "12,,,345",
968 | " 12 : 34 : 56 : 789 ",
969 | " +12:34:56,789",
970 | " + 12:34:56,789",
971 | " -12:34:56,789",
972 | "+0:0:2.0",
973 | "+0:0:2.0:9",
974 | "abc",
975 | "12abc",
976 | "::::",
977 | NULL
978 | };
979 |
980 | for (i = 0; testbl[i]; i++) {
981 | ms = strtoms(testbl[i], &n, &style);
982 | printf("%s(%d): %s =%ld\n", testbl[i], n, mstostr(ms, style), (long)ms);
983 | }
984 | }
985 |
986 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------