This repository was archived by the owner on Apr 18, 2023. It is now read-only.
forked from Secksdendfordff/Anime4K-Encoder-4.0.1-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_subs.py
68 lines (58 loc) · 1.96 KB
/
extract_subs.py
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
import subprocess
import sys
from pymkv import MKVFile
from utils import current_date
def genExt(codec):
if "PGS" in codec:
return "sup"
elif "ASS" in codec or "SubStationAlpha" in codec:
return "ass"
elif "SRT" in codec or "SubRip" in codec:
return "srt"
elif "VobSub" in codec:
return "idx"
else:
return None
def extract_subs(debug: bool, fn: str, out_dir: str) -> bool:
"""
Extract subtitles from a media file.
Args:
fn: input media file path
out_dir: directory where subtitles are extracted to
Returns:
True if the subtitles were successfully extracted
"""
if out_dir is None:
out_dir = ""
print("Loading file={0}".format(fn))
mkv = MKVFile(fn)
print("Subtitle extraction start time: " + current_date())
tracks = mkv.get_track()
success = True
for track in tracks:
if track.track_type == 'subtitles':
ext = genExt(track._track_codec)
if ext is None:
print(
"WARNING: Skipping unknown subtitle with id={0}, codec={1}".format(
str(track._track_id),
str(track._track_codec or "Unknown")))
continue
lang = track._language
id = str(track._track_id)
return_code = -1
try:
return_code = subprocess.call([
'mkvextract', 'tracks', fn,
id + ':' + str(out_dir) + str(lang) + '_' + id + '.' + ext
])
except KeyboardInterrupt:
print("Cancelled subtitles extraction.")
print("Exiting program...")
sys.exit(-1)
if debug:
print("mkvextract exited with return code={}", return_code)
if return_code != 0:
success = False
print("Subtitle extraction end time: " + current_date())
return success