ts2mkv.py

Fri, 24 Nov 2017 23:42:30 +0100

author
mdd
date
Fri, 24 Nov 2017 23:42:30 +0100
changeset 10
f436a7f94c6a
parent 9
1bf778001041
child 11
821c02fa7070
permissions
-rwxr-xr-x

pylint

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

mercurial