-
Notifications
You must be signed in to change notification settings - Fork 2
/
mkvtool.py
105 lines (93 loc) · 3.41 KB
/
mkvtool.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
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
#!/usr/bin/python3
from subprocess import Popen, PIPE
import re
import os
import os.path
MKVMERGE = "mkvmerge"
MKVINFO = "mkvinfo"
mkvinfo_out_fmt = re.compile(r"(.) frame, track (\d+), time[a-z]* ([0-9:\.]+), .*")
mkvmerge_out_fmt = re.compile(r"Progress: (\d+\%).*")
def ts2secs(ts):
split = ts.split(":")
s = float(split[-1])
s += float(split[-2] * 60)
s += float(split[-3] * 3600)
return s
class mkvtool:
def get_i_frames(filename, report):
report("reading I-Frames", "0%")
duration = 0
cmd = [MKVINFO, filename]
mkvinfo = Popen(cmd, stdout=PIPE)
segment_flag = False
line = str(mkvinfo.stdout.readline())[2:-1].rstrip()
while line != "":
if segment_flag and "Duration" in line:
pattern = re.compile(r".*\(([0-9:\.]*)\).*")
try:
duration = ts2secs(pattern.match(line).group(1))
except:
pass
if "Segment" in line:
segment_flag = True
line = str(mkvinfo.stdout.readline())[2:-1].rstrip()
mkvinfo.stdout.close()
mkvinfo.wait()
cmd = [MKVINFO, "-s", filename]
i_frames = []
mkvinfo = Popen(cmd, stdout=PIPE)
line = str(mkvinfo.stdout.readline())[2:-1].rstrip()
while line != "":
#print(line)
match = mkvinfo_out_fmt.match(line)
try:
frame = match.groups()
if frame[0] == 'I' and frame[1] == '1':
i_frames.append(frame[2])
secs = ts2secs(frame[2])
percent = str(int(secs * 90 / duration)) + "%"
report("reading I-Frames", percent)
except:
print(line)
line = str(mkvinfo.stdout.readline())[2:-1].rstrip()
mkvinfo.stdout.close()
mkvinfo.wait()
print(i_frames)
report("reading I-Frames", "100%")
return i_frames
def split(orig, chunks, splits, report):
report("splitting", "0%")
chunk_dir = os.path.dirname(chunks)
chunk_name = re.sub(".mkv", "", os.path.basename(chunks))
cmd = [MKVMERGE, "-o", chunks, "--split", "timestamps:" + ",".join(splits), orig]
print(cmd)
mkvmerge = Popen(cmd, stdout=PIPE)
line = str(mkvmerge.stdout.readline())[2:-1].rstrip()
while line != "":
match = mkvmerge_out_fmt.match(line)
try:
report("splitting", match.group(1))
except:
pass
line = str(mkvmerge.stdout.readline())[2:-1].rstrip()
mkvmerge.stdout.close()
mkvmerge.wait()
report("splitting", "100%")
return sorted([f for f in os.listdir(chunk_dir) if chunk_name in f])
def merge(files, out, report):
report("merging", "0%")
cmd = [MKVMERGE, "-o", out] + " + ".join(files).split()
print(cmd)
mkvmerge = Popen(cmd, stdout=PIPE)
line = str(mkvmerge.stdout.readline())[2:-1].rstrip()
while line != "":
match = mkvmerge_out_fmt.match(line)
try:
report("merging", match.group(1))
except:
pass
line = str(mkvmerge.stdout.readline())[2:-1].rstrip()
mkvmerge.stdout.close()
mkvmerge.wait()
report("finished", "100%")
return out