-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkv2flac.py
122 lines (99 loc) 路 3.04 KB
/
mkv2flac.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
import os
import re
import sys
import subprocess as sp
from os.path import basename
# TODO: use `ffprobe -i fname -print_format json -show_chapters -loglevel error` instead
def parse_chapters(filename):
chapters = []
command = ["ffmpeg", "-i", filename]
output = ""
m = None
title = None
chapter_match = None
try:
# ffmpeg requires an output file and so it errors
# when it does not get one so we need to capture stderr,
# not stdout.
output = sp.check_output(command, stderr=sp.STDOUT, universal_newlines=True)
except sp.CalledProcessError as e:
output = e.output
num = 1
for line in iter(output.splitlines()):
x = re.match(r".*title.*: (.*)", line)
if x == None:
m1 = re.match(
r".*Chapter #(\d+:\d+): start (\d+\.\d+), end (\d+\.\d+).*", line
)
title = None
else:
title = x.group(1)
if m1 != None:
chapter_match = m1
if title != None and chapter_match != None:
m = chapter_match
else:
m = None
if m != None:
chapters.append(
{
"name": str(num) + " - " + title,
"start": m.group(2),
"end": m.group(3),
}
)
num += 1
return chapters
def get_chapters():
filename = sys.argv[1]
chapters = parse_chapters(filename)
newdir, fext = os.path.splitext(basename(filename))
workingdir = os.path.join(os.getcwd(), newdir)
if os.path.exists(workingdir):
print("Working dir already exists.")
else:
os.mkdir(workingdir)
print("Working dir created.")
for chap in chapters:
chap["name"] = chap["name"].replace("/", ":")
chap["name"] = chap["name"].replace("'", "'")
print("start:" + chap["start"])
chap["output"] = os.path.join(
workingdir, re.sub("[^-a-zA-Z0-9_.():' ]+", "", chap["name"]) + ".flac"
)
chap["input"] = os.path.join(os.getcwd(), filename)
print(chap["output"])
return chapters
def convert_chapters(chapters):
for chap in chapters:
print("start:" + chap["start"])
print(chap)
if os.path.isfile(chap["output"]):
continue
command = [
"ffmpeg",
"-i",
chap["input"],
"-ss",
chap["start"],
"-to",
chap["end"],
"-c:a",
"flac",
chap["output"],
]
try:
sp.check_output(command, stderr=sp.STDOUT, universal_newlines=True)
except sp.CalledProcessError as e:
raise RuntimeError(
"command '{}' return with error (code {}): {}".format(
e.cmd, e.returncode, e.output
)
)
# TODO: select TrueHD stream
def main():
chapters = get_chapters()
convert_chapters(chapters)
if __name__ == "__main__":
main()