1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
from typing import Any
import pysubs2
import tempfile
import subprocess
import os.path as op
from io import StringIO
TEST_SRT_FILE = """\
1
00:00:00,000 --> 00:01:00,000
An example subtitle.
2
00:01:00,000 --> 00:02:00,000
Subtitle number
two.
"""
TEST_SRT_FILE_WIN1250 = """\
1
00:00:00,000 --> 00:01:00,000
An example subtitle.
Příliš žluťoučký kůň úpěl ďábelské ódy
"""
TEST_MICRODVD_FILE = """\
{1}{1}1000.0
{0}{60000}An example subtitle.
{60000}{120000}Subtitle number|two.
"""
def test_srt_to_microdvd() -> None:
with tempfile.TemporaryDirectory() as dirpath:
inpath = op.join(dirpath, "test.srt")
with open(inpath, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_FILE)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "microdvd", "--fps", "1000", inpath])
outpath = op.join(dirpath, "test.sub")
with open(outpath, encoding="utf-8") as fp:
out = fp.read()
assert out == TEST_MICRODVD_FILE
def test_srt_to_microdvd_subprocess_pipe() -> None:
cmd = ["python", "-m", "pysubs2", "--to", "microdvd", "--fps", "1000"]
output = subprocess.check_output(cmd, input=TEST_SRT_FILE, text=True)
assert output.strip() == TEST_MICRODVD_FILE.strip()
def test_srt_to_microdvd_multiple_files() -> None:
N = 3
with tempfile.TemporaryDirectory() as dirpath:
inpaths = [op.join(dirpath, f"test-{i}.srt") for i in range(N)]
for inpath in inpaths:
with open(inpath, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_FILE)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "microdvd", "--fps", "1000"] + inpaths)
outpaths = [p.replace(".srt", ".sub") for p in inpaths]
for outpath in outpaths:
with open(outpath, encoding="utf-8") as fp:
out = fp.read()
assert out == TEST_MICRODVD_FILE
def test_microdvd_to_srt() -> None:
with tempfile.TemporaryDirectory() as dirpath:
inpath = op.join(dirpath, "test.sub")
with open(inpath, "w", encoding="utf-8") as fp:
fp.write(TEST_MICRODVD_FILE)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", inpath])
outpath = op.join(dirpath, "test.srt")
with open(outpath, encoding="utf-8") as fp:
out = fp.read()
assert out == TEST_SRT_FILE
TEST_SRT_FILE_SHIFTED = """\
1
01:00:01,500 --> 01:01:01,500
An example subtitle.
2
01:01:01,500 --> 01:02:01,500
Subtitle number
two.
"""
def test_srt_shift() -> None:
with tempfile.TemporaryDirectory() as dirpath:
inpath = outpath = op.join(dirpath, "test.srt")
with open(inpath, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_FILE)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--shift", "1h1.5s", inpath])
with open(outpath, encoding="utf-8") as fp:
out = fp.read()
assert out == TEST_SRT_FILE_SHIFTED
def test_srt_shift_back() -> None:
with tempfile.TemporaryDirectory() as dirpath:
inpath = outpath = op.join(dirpath, "test.srt")
with open(inpath, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_FILE_SHIFTED)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--shift-back", "1h1.5s", inpath])
with open(outpath, encoding="utf-8") as fp:
out = fp.read()
assert out == TEST_SRT_FILE
def test_srt_shift_to_output_dir() -> None:
with tempfile.TemporaryDirectory() as indirpath:
inpath = op.join(indirpath, "test.srt")
with open(inpath, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_FILE)
with tempfile.TemporaryDirectory() as outdirpath:
outdirpath2 = op.join(outdirpath, "subdir-that-must-be-created")
outpath = op.join(outdirpath2, "test.srt")
cli = pysubs2.cli.Pysubs2CLI()
cli(["--shift", "1h1.5s", "-o", outdirpath2, inpath])
with open(outpath, encoding="utf-8") as fp:
out = fp.read()
assert out == TEST_SRT_FILE_SHIFTED
with open(inpath, encoding="utf-8") as fp:
out = fp.read()
assert out == TEST_SRT_FILE
TEST_SUBSTATION_WITH_KARAOKE = r"""
[Script Info]
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
YCbCr Matrix: TV.709
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,Volkhov,78,&H00FFFFFF,&H000000FF,&H00000000,&H96000000,0,0,0,0,95,100,0,0,1,3.5,0,2,225,225,50,1
Style: OP-R,Clearface SSi,70,&H00FFFFFF,&HFFA6A6A6,&H004E4AFB,&H00000000,-1,0,0,0,100,100,0,0,1,3,0,8,40,40,40,1
Style: OP-E,Clearface SSi,55,&H00FFFFFF,&HFFA6A6A6,&H00464646,&H00000000,-1,0,0,0,100,100,0,0,1,3,0,8,40,40,115,1
Style: ED-R,IM FELL English PRO,78,&H57EAFDFE,&H00B6B6B6,&H00182739,&H8CFFFFFF,0,0,0,0,100,100,0,0,1,5.9,0.1,8,10,10,34,1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 10,0:00:07.24,0:00:10.03,OP-E,,0,0,0,,{\an5\pos(655.758,142.500)\1c&H4E4AFB&\3c&H4E4AFB&\blur2.4\bord2.5\fad(300,300)}I
Dialogue: 11,0:00:07.24,0:00:10.03,OP-E,,0,0,0,,{\an5\pos(655.758,142.500)\bord0\blur0.4\fad(300,300)}I
Dialogue: 10,0:00:07.28,0:00:10.07,OP-E,,0,0,0,,{\an5\pos(691.016,142.500)\1c&H4E4AFB&\3c&H4E4AFB&\blur2.4\bord2.5\fad(300,300)}w
Dialogue: 11,0:00:07.28,0:00:10.07,OP-E,,0,0,0,,{\an5\pos(691.016,142.500)\bord0\blur0.4\fad(300,300)}w
Dialogue: 3,0:23:38.12,0:23:38.16,ED-R,,0,0,0,,{\an7\pos(1113.473,73.684)\bord0\blur0.5\1c&H000000&\p4}m 0 0 l 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Dialogue: 10,0:24:06.45,0:24:09.95,Default,Next time,0,0,0,,Next episode: "Warrior."
Dialogue: 5,0:24:06.45,0:24:09.95,Default,Next time,0,0,0,,{\be1}Next episode: "Warrior."{this duplicate line should be deleted}
"""
TEST_SUBSTATION_WITH_KARAOKE_SRT_CLEAN_OUTPUT = """
1
00:24:06,450 --> 00:24:09,950
Next episode: "Warrior."
"""
TEST_SUBSTATION_WITH_ITALICS = r"""
[Script Info]
; Script generated by Aegisub 3.2.2
; http://www.aegisub.org/
Title: Default Aegisub file
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
YCbCr Matrix: None
[Aegisub Project Garbage]
Last Style Storage: Default
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1
Style: Default - italic,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,-1,0,0,100,100,0,0,1,2,2,2,10,10,10,1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.00,0:00:05.00,Default - italic,,0,0,0,,Italic from style
Dialogue: 0,0:00:05.00,0:00:07.00,Default,,0,0,0,,Italic from {\i1}override tag{\i0}
"""
TEST_SUBSTATION_WITH_ITALICS_SRT_CLEAN_OUTPUT = """
1
00:00:00,000 --> 00:00:05,000
Italic from style
2
00:00:05,000 --> 00:00:07,000
Italic from override tag
"""
TEST_SUBSTATION_WITH_ITALICS_SRT_OUTPUT = """
1
00:00:00,000 --> 00:00:05,000
<i>Italic from style</i>
2
00:00:05,000 --> 00:00:07,000
Italic from <i>override tag</i>
"""
TEST_SRT_KEEP_SSA_TAGS = """
1
00:00:00,000 --> 00:01:00,000
{\\an7}An example subtitle.
2
00:01:00,000 --> 00:02:00,000
Subtitle {\\b1}number{\\b0}
two.
"""
TEST_SRT_KEEP_SSA_TAGS_MIXED_WITH_HTML = """
1
00:00:00,000 --> 00:01:00,000
{\\an7}An example subtitle.
2
00:01:00,000 --> 00:02:00,000
Subtitle <i>number</i>
two.
"""
TEST_SRT_KEEP_UNKNOWN_HTML_TAGS = """
1
00:00:00,000 --> 00:00:05,000
<i>Italic from style</i>
2
00:00:05,000 --> 00:00:07,000
Some unsupported <blink>tag</blink>
"""
def test_srt_clean() -> None:
# see issue #37
with tempfile.TemporaryDirectory() as dirpath:
inpath = op.join(dirpath, "test.ass")
outpath = op.join(dirpath, "test.srt")
with open(inpath, "w", encoding="utf-8") as fp:
fp.write(TEST_SUBSTATION_WITH_KARAOKE)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", "--clean", inpath])
with open(outpath, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() == TEST_SUBSTATION_WITH_KARAOKE_SRT_CLEAN_OUTPUT.strip()
def test_srt_clean_styling() -> None:
# see issue #39
with tempfile.TemporaryDirectory() as dirpath:
inpath = op.join(dirpath, "test.ass")
outpath = op.join(dirpath, "test.srt")
with open(inpath, "w", encoding="utf-8") as fp:
fp.write(TEST_SUBSTATION_WITH_ITALICS)
# test standard
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", inpath])
with open(outpath, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() == TEST_SUBSTATION_WITH_ITALICS_SRT_OUTPUT.strip()
# test clean
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", "--clean", inpath])
with open(outpath, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() == TEST_SUBSTATION_WITH_ITALICS_SRT_CLEAN_OUTPUT.strip()
def test_srt_keep_ssa_tags() -> None:
# see issue #48
with tempfile.TemporaryDirectory() as dirpath:
path = op.join(dirpath, "test.srt")
# test standard
with open(path, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_KEEP_SSA_TAGS)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", path])
with open(path, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() != TEST_SRT_KEEP_SSA_TAGS.strip()
# test keep tags
with open(path, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_KEEP_SSA_TAGS)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", "--srt-keep-ssa-tags", path])
with open(path, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() == TEST_SRT_KEEP_SSA_TAGS.strip()
def test_srt_keep_ssa_tags_mixed_with_html() -> None:
# see issue #48
with tempfile.TemporaryDirectory() as dirpath:
path = op.join(dirpath, "test.srt")
# test standard - does not pass
with open(path, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_KEEP_SSA_TAGS_MIXED_WITH_HTML)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", path])
with open(path, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() != TEST_SRT_KEEP_SSA_TAGS_MIXED_WITH_HTML.strip()
# test juts keep SSA tags - does not pass
with open(path, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_KEEP_SSA_TAGS_MIXED_WITH_HTML)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", "--srt-keep-ssa-tags", path])
with open(path, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() != TEST_SRT_KEEP_SSA_TAGS_MIXED_WITH_HTML.strip()
# test keep SSA tags and keep HTML tags - should pass
with open(path, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_KEEP_SSA_TAGS_MIXED_WITH_HTML)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", "--srt-keep-ssa-tags", "--srt-keep-html-tags", path])
with open(path, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() == TEST_SRT_KEEP_SSA_TAGS_MIXED_WITH_HTML.strip()
def test_srt_keep_unknown_html_tags() -> None:
with tempfile.TemporaryDirectory() as dirpath:
path = op.join(dirpath, "test.srt")
# test standard
with open(path, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_KEEP_UNKNOWN_HTML_TAGS)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", path])
with open(path, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() != TEST_SRT_KEEP_UNKNOWN_HTML_TAGS.strip()
# test keep tags
with open(path, "w", encoding="utf-8") as fp:
fp.write(TEST_SRT_KEEP_UNKNOWN_HTML_TAGS)
cli = pysubs2.cli.Pysubs2CLI()
cli(["--to", "srt", "--srt-keep-unknown-html-tags", path])
with open(path, encoding="utf-8") as fp:
out = fp.read()
assert out.strip() == TEST_SRT_KEEP_UNKNOWN_HTML_TAGS.strip()
def test_print_help_on_empty_tty_input(capsys: Any, monkeypatch: Any) -> None:
monkeypatch.setattr("sys.stdin", StringIO())
monkeypatch.setattr("sys.stdin.isatty", (lambda: True))
cli = pysubs2.cli.Pysubs2CLI()
cli([])
captured = capsys.readouterr()
assert captured.out.startswith("usage: pysubs2")
def test_empty_notty_input_doesnt_print_help(capsys: Any, monkeypatch: Any) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
path = op.join(temp_dir, "test.srt")
with open(path, "w+") as in_fp:
cmd = ["python", "-m", "pysubs2"]
p = subprocess.run(cmd, stdin=in_fp, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
assert p.returncode == 1
assert not p.stdout.startswith("usage: pysubs2")
assert "FormatAutodetectionError" in p.stderr
def test_win1250_passthrough_with_surrogateescape() -> None:
input_bytes_win1250 = TEST_SRT_FILE_WIN1250.encode("windows-1250")
with tempfile.TemporaryDirectory() as temp_dir:
input_path = op.join(temp_dir, "input.srt")
output_dir = op.join(temp_dir, "output")
output_path = op.join(output_dir, "input.srt")
with open(input_path, "wb") as fp:
fp.write(input_bytes_win1250)
cmd = ["python", "-m", "pysubs2", "-o", output_dir, input_path]
subprocess.check_call(cmd)
with open(output_path, "rb") as fp:
output_bytes = fp.read()
assert input_bytes_win1250 == output_bytes
|