ts2mkv.py

Fri, 24 Nov 2017 23:11:58 +0100

author
mdd
date
Fri, 24 Nov 2017 23:11:58 +0100
changeset 9
1bf778001041
parent 8
92409c985e0d
child 10
f436a7f94c6a
permissions
-rwxr-xr-x

added option parser and automatic file rename feature

7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
1 #!/usr/bin/env python
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
2
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
3 import subprocess
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
4 from eit import readeit, eitinfo
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
5 import os, shlex
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
6
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
7 def filter_lines(data, search):
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
8 ret = []
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
9 for line in data.split("\n"):
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
10 if line.find(search) == -1:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
11 continue
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
12 ret.append(line)
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
13 return "\n".join(ret)
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
14
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
15 def run_command(command):
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
16 process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
17 while True:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
18 output = process.stdout.readline()
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
19 if output == '' and process.poll() is not None:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
20 break
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
21 if output:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
22 print output.strip()
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
23 rc = process.poll()
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
24 return rc
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
25
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
26 class ts2mkv(object):
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
27 def __init__(self, crf=19, tune='film', scaleto_720p=True, rename=False):
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
28 self.msg_prepare = ""
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
29 self.msg_eit = ""
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
30 self.msg_ffmpeg = ""
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
31 self.command = None
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
32
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
33 self.scaleto_720p = scaleto_720p
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
34 self.rename = rename
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
35
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
36 self.video_options = [
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
37 "-c:v libx264",
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
38 "-preset faster", # slow
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
39 "-tune %s" % tune, # film / animation
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
40 "-crf %i" % crf # 21, better 19
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
41 ]
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
42 self.audio_options = [
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
43 "-c:a copy",
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
44 ]
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
45
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
46 def get_stream_index(self, data):
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
47 idx = data.find("Stream #")
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
48 if idx == -1:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
49 return ""
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
50 idx += 8
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
51 self.msg_prepare += "Selecting: %s\n" % data
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
52 return data[idx:idx+3]
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
53
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
54 def get_movie_description(self):
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
55 if not self.filename:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
56 return
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
57 # read the EIT file
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
58 # TODO: fallback to meta file if no EIT
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
59 # TODO: is there a way to get the imdb for the movie automagically?
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
60 # http://www.omdbapi.com/apikey.aspx
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
61 filename = os.path.splitext(self.filename)[0] + ".eit"
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
62 self.msg_eit = readeit(filename)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
63 if not self.rename or not self.msg_eit:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
64 return
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
65 info = eitinfo(filename)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
66 name = info.eit.get("name")
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
67 if name == "":
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
68 # cancel rename, no movie title found!
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
69 return
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
70 genre = info.eit.get("genre")
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
71 if genre != "":
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
72 name = "%s (%s)" % (name, genre)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
73 # build new filename
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
74 name = name.replace(' : ', ' - ')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
75 name = name.replace(': ', ' - ')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
76 name = name.replace(':', '-')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
77 name = name.replace('/', '')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
78 name = name.replace('\\', '')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
79 name = name.replace('?', '')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
80 name = name.replace('*', '')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
81 name = name.replace('\"', '\'')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
82
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
83 self.outfilebase = os.path.join(
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
84 os.path.dirname(filename),
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
85 name
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
86 )
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
87
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
88
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
89 def get_crop_option(self):
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
90 lines = filter_lines(self.msg_ffmpeg, "[Parsed_cropdetect").split("\n")
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
91 option = None
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
92 for line in lines:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
93 tmp = line[line.find(" crop="):].strip()
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
94 #print "DEBUG: " + tmp
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
95 if not option:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
96 option = tmp
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
97 else:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
98 if option != tmp:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
99 self.msg_prepare += "WARNING: cropdetect inconsistent over scan time, disabling autocrop\n"
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
100 return None
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
101 self.msg_prepare += "Crop detected: %s\n" % option
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
102 return option
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
103
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
104 def get_ffmpeg_command(self):
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
105 if not self.filename:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
106 return None
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
107
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
108 commands = []
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
109 fn = "\\'".join(p for p in self.filename.split("'"))
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
110 fn = fn.replace(" ", "\\ ")
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
111 outfn = self.outfilebase + ".mkv"
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
112 outfn = "\\'".join(p for p in outfn.split("'"))
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
113 outfn = outfn.replace(" ", "\\ ")
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
114
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
115 # ffmpeg -ss 00:05:00 -t 2 -i testfiles/chappie.ts -vf "cropdetect=24:16:0" -f null -
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
116
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
117 cmd = ["ffmpeg",
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
118 "-ss 00:05:00", "-t 1", # search to 5 minutes, analyze 1 second
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
119 "-i %s" % fn,
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
120 "-vf \"cropdetect=24:2:0\"", # detect black bar crop on top and bottom
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
121 "-f null", "-" # no output file
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
122 ]
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
123 print " ".join(cmd)
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
124 p = subprocess.Popen(shlex.split(" ".join(cmd)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
125 out, err = p.communicate()
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
126 print "Command return code: ", p.poll()
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
127 self.msg_ffmpeg = out + "\n" + err
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
128 self.msg_ffmpeg = self.msg_ffmpeg[self.msg_ffmpeg.find("Input #0"):]
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
129
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
130 # find "Stream #0:" lines
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
131 info = filter_lines(self.msg_ffmpeg, "Stream #0:")
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
132
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
133
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
134 # CRAP! ffmpeg cannot decode dvb_teletext streams to srt :(
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
135 # are there any subtitle streams?!
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
136 #s = filter_lines(info, "Subtitle:")
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
137 #s = filter_lines(s, "(deu):")
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
138 #if s != "":
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
139 # s = self.get_stream_index(s.split("\n")[0])
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
140 # commands.append([
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
141 # "ffmpeg",
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
142 # "-txt_format text",
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
143 # "-i %s" % fn,
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
144 # "-map %s" % s,
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
145 # "%s.srt" % os.path.splitext(fn)[0]
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
146 # ])
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
147
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
148 v = self.get_stream_index(
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
149 filter_lines(info, "Video:"))
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
150 if v == "":
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
151 print "No video stream found"
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
152 return None
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
153
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
154 audiomap = []
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
155 audioall = filter_lines(info, "Audio:")
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
156 audio = filter_lines(audioall, "(deu):")
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
157 a = self.get_stream_index(
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
158 filter_lines(audio, "ac3"))
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
159 # TODO: wenn kein ac3 stream dann dts oder mpeg fallback
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
160 if a == "":
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
161 print audioall
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
162 print "No AC3 german audio stream found"
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
163 # try to find the first german audio stream
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
164 a = self.get_stream_index(audio.split("\n")[0])
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
165 if a == "":
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
166 print "No other german audio streams, trying english ac3..."
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
167 else:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
168 print "Selecting first german stream."
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
169 audiomap.append(a)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
170 else:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
171 audiomap.append(a)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
172
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
173 audio = filter_lines(audioall, "(eng):")
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
174 a = self.get_stream_index(
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
175 filter_lines(audio, "ac3"))
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
176 if a != "":
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
177 # append english audio too!
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
178 print "Selecting english ac3 stream."
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
179 audiomap.append(a)
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
180
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
181 if len(audiomap) == 0:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
182 print "No suitable audio stream found, aborting."
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
183 return None
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
184
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
185
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
186 self.msg_prepare += "Video Stream selected: Stream #%s\n" % v
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
187 cmd = [
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
188 "ffmpeg",
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
189 "-i %s" % fn,
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
190 "-map %s" % v,
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
191 ]
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
192 flt = []
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
193 crop = self.get_crop_option()
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
194 if crop:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
195 flt.append(crop)
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
196 if self.scaleto_720p:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
197 flt.append("scale='min(1280,iw)':-2'") # -2 ensures division by two for codec
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
198 self.msg_prepare += "Scaling cropped output stream to 720p\n"
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
199 if len(flt) > 0:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
200 # append video filters
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
201 cmd.append('-filter:v "%s"' % ",".join(flt))
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
202 for a in audiomap:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
203 self.msg_prepare += "Audio Stream selected: Stream #%s\n" % a
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
204 cmd.append("-map %s" % a)
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
205 cmd.extend(self.video_options)
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
206 cmd.extend(self.audio_options)
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
207 cmd.append(outfn)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
208
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
209 commands.append(" ".join(cmd))
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
210 return commands
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
211
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
212 def load(self, filename):
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
213 self.filename = filename
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
214 self.outfilebase = os.path.splitext(filename)[0]
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
215 self.get_movie_description()
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
216 self.command = self.get_ffmpeg_command()
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
217
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
218 def convert(self):
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
219 if not self.command:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
220 return None
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
221 fd = open(self.outfilebase + ".txt", "wb")
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
222 fd.write(self.msg_eit)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
223 fd.write("\n\n# ---DEBUG---\n\n")
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
224 fd.write(self.msg_prepare)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
225 fd.write(self.msg_ffmpeg)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
226 fd.close()
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
227 #print self.msg_ffmpeg
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
228
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
229 for cmd in mkv.command:
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
230 print "Executing ffmpeg:\n%s\n" % cmd
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
231 return run_command(cmd)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
232
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
233
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
234
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
235 if __name__ == "__main__":
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
236 # parse command line options
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
237 import argparse, sys, glob
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
238
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
239 parser = argparse.ArgumentParser(description = 'DVB-TS to MKV kung-fu')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
240 parser.add_argument('--crf', type=int, default=19,
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
241 help='h264 crf (default 19)')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
242 parser.add_argument('--tune', default='film',
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
243 help='ffmpeg tune preset [film, animation] (default is film)')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
244 parser.add_argument('--ns', action='store_true', default=False,
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
245 help='no rescaling (default is scale to 720p)')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
246 parser.add_argument('--rename', action='store_true', default=False,
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
247 help='rename file basename to name and genre from EIT file if present')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
248 parser.add_argument('input', metavar='input', nargs='+',
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
249 help='one or more files, glob style syntax')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
250
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
251 args = parser.parse_args()
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
252 mkv = ts2mkv(
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
253 crf = args.crf,
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
254 tune = args.tune,
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
255 scaleto_720p = (not args.ns),
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
256 rename = args.rename
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
257 )
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
258
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
259 #os.system('cls' if os.name == 'nt' else 'clear')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
260
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
261 for srcstr in args.input:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
262 src = glob.glob(srcstr)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
263 for filename in src:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
264 print filename
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
265 mkv.load(filename)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
266 mkv.convert()
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
267
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
268

mercurial