ts2mkv.py

Tue, 28 Nov 2017 19:02:01 +0100

author
mdd
date
Tue, 28 Nov 2017 19:02:01 +0100
changeset 15
82361ad7b3fe
parent 14
b398ae388328
child 16
ace8005f02cf
permissions
-rwxr-xr-x

some changes, also implemented ffmpeg progress info and added force overwrite mode

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
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
12 import pexpect
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
13 from eit import readeit, eitinfo
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
14 import os, shlex, sys, time
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
15
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
16 def filter_lines(data, search):
10
mdd
parents: 9
diff changeset
17 """
mdd
parents: 9
diff changeset
18 input: data = \n separated string
mdd
parents: 9
diff changeset
19 output: tuple containing all lines where search is found
mdd
parents: 9
diff changeset
20 """
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
21 ret = []
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
22 for line in data.split("\n"):
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
23 if line.find(search) == -1:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
24 continue
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
25 ret.append(line)
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
26 return "\n".join(ret)
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
27
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
28 def run_command(command):
10
mdd
parents: 9
diff changeset
29 """
mdd
parents: 9
diff changeset
30 run command as blocking subprocess, returns exit code
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
31 if total_frames > 0 parse ffmpeg status line and insert ETA at line start before output
10
mdd
parents: 9
diff changeset
32 """
mdd
parents: 9
diff changeset
33 process = subprocess.Popen(shlex.split(command), \
mdd
parents: 9
diff changeset
34 stdout=subprocess.PIPE)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
35 while True:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
36 output = process.stdout.readline()
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
37 if output == '' and process.poll() is not None:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
38 break
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
39 if output:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
40 print output.strip()
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
41 rc = process.poll()
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
42 return rc
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
43
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
44 def run_ffmpeg_watch(command, frames_total = 0):
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
45 """
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
46 run command as blocking subprocess, returns exit code
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
47 if total_frames > 0 parse ffmpeg status line and insert ETA at line start before output
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
48 """
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
49 thread = pexpect.spawn(command)
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
50 cpl = thread.compile_pattern_list([
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
51 pexpect.EOF,
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
52 "frame= *(\d+)",
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
53 '(.+)'
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
54 ])
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
55 percent = 0
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
56 eta = 0
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
57 time_start = time.time() - 0.1 # start in the past
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
58 while True:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
59 i = thread.expect_list(cpl, timeout=None)
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
60 if i == 0: # EOF
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
61 print "the sub process exited"
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
62 break
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
63 elif i == 1:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
64 try:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
65 frame_number = int(thread.match.group(1))
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
66 if frames_total > 0:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
67 percent = frame_number * 100.00 / frames_total
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
68 eta = frame_number / (time.time() - time_start)
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
69 # eta is frames per second so far
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
70 eta = (frames_total - frame_number) / eta / 60
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
71 sys.stdout.write("\rFrame %i of %i, %.1f%% done, ETA %.0f minutes, " % (
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
72 frame_number, frames_total, percent, eta
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
73 ))
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
74 except:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
75 sys.stdout.write(thread.match.group(0))
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
76 sys.stdout.flush()
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
77 thread.close
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
78 elif i == 2:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
79 unknown_line = thread.match.group(0)
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
80 sys.stdout.write(unknown_line)
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
81 sys.stdout.flush()
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
82 pass
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
83
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
84 def ffmpeg_filename(filename):
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
85 """
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
86 Escape filename path contents for ffmpeg shell command
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
87 """
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
88 #fn = "\\'".join(p for p in filename.split("'"))
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
89 fn = filename.replace("'", "\\'")
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
90 fn = fn.replace(" ", "\\ ")
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
91 return fn
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
92
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
93 class ts2mkv(object):
10
mdd
parents: 9
diff changeset
94 """
mdd
parents: 9
diff changeset
95 Main worker class, contains all the magic & ffmpeg voodoo
mdd
parents: 9
diff changeset
96 """
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
97 def __init__(self, crf=19, tune='film', scaleto_720p=True, rename=False):
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
98 self.msg_prepare = ""
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
99 self.msg_eit = ""
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
100 self.msg_ffmpeg = ""
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
101 self.command = None
13
cf5c5cec1b2b bugfix: cleanup status messages when processing multiple files
mdd
parents: 12
diff changeset
102 self.filename = None
cf5c5cec1b2b bugfix: cleanup status messages when processing multiple files
mdd
parents: 12
diff changeset
103 self.outfilebase = None
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
104 self.fps = 0
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
105 self.frames_total = 0
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
106 self.overwrite = False
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
107
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
108 self.scaleto_720p = scaleto_720p
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
109 self.rename = rename
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
110
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
111 self.video_options = [
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
112 "-c:v libx264",
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
113 "-preset faster", # slow
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
114 "-tune %s" % tune, # film / animation
10
mdd
parents: 9
diff changeset
115 "-crf %i" % crf, # 21, better 19
mdd
parents: 9
diff changeset
116 ]
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
117 self.audio_options = [
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
118 "-c:a copy",
10
mdd
parents: 9
diff changeset
119 ]
mdd
parents: 9
diff changeset
120
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
121
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
122 def get_stream_index(self, data):
10
mdd
parents: 9
diff changeset
123 """
mdd
parents: 9
diff changeset
124 input: ffmpeg stream info string
mdd
parents: 9
diff changeset
125 output: ffmpeg stream mapping part
mdd
parents: 9
diff changeset
126 """
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
127 idx = data.find("Stream #")
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
128 if idx == -1:
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
129 return ""
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
130 idx += 8
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
131 self.msg_prepare += "Selecting: %s\n" % data
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
132 return data[idx:idx+3]
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
133
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
134 def get_movie_description(self):
10
mdd
parents: 9
diff changeset
135 """
mdd
parents: 9
diff changeset
136 looks for eit file with same basename of current filename
mdd
parents: 9
diff changeset
137 parse the eit file for txt infofile and optional build new
mdd
parents: 9
diff changeset
138 output filename base with movie name and genre
mdd
parents: 9
diff changeset
139
mdd
parents: 9
diff changeset
140 output: nothing, manipulates internal variables
mdd
parents: 9
diff changeset
141 """
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
142 if not self.filename:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
143 return
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
144 # read the EIT file
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
145 filename = os.path.splitext(self.filename)[0] + ".eit"
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
146 self.msg_eit = readeit(filename)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
147 if not self.rename or not self.msg_eit:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
148 return
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
149 info = eitinfo(filename)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
150 name = info.eit.get("name")
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
151 if name == "":
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
152 # cancel rename, no movie title found!
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
153 return
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
154 genre = info.eit.get("genre")
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
155 if genre != "":
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
156 name = "%s (%s)" % (name, genre)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
157 # build new filename
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
158 name = name.replace(' : ', ' - ')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
159 name = name.replace(': ', ' - ')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
160 name = name.replace(':', '-')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
161 name = name.replace('/', '')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
162 name = name.replace('\\', '')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
163 name = name.replace('?', '')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
164 name = name.replace('*', '')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
165 name = name.replace('\"', '\'')
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
166
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
167 self.outfilebase = os.path.join(
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
168 os.path.dirname(filename),
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
169 name
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
170 )
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
171
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
172
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
173 def get_crop_option(self):
10
mdd
parents: 9
diff changeset
174 """
mdd
parents: 9
diff changeset
175 parse the ffmpeg analyze output cropdetect lines
mdd
parents: 9
diff changeset
176 returns None or valid crop string for ffmpeg video filter
mdd
parents: 9
diff changeset
177 """
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
178 lines = filter_lines(self.msg_ffmpeg, "[Parsed_cropdetect").split("\n")
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
179 option = None
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
180 failcount = 0
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
181 for line in lines:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
182 tmp = line[line.find(" crop="):].strip()
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
183 #print "DEBUG: " + tmp
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
184 if not option:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
185 option = tmp
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
186 else:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
187 if option != tmp:
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
188 failcount += 1
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
189 if failcount > 12:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
190 print "!!! Crop detect is inconsistent"
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
191 self.msg_prepare += "WARNING: cropdetect >50% inconsistent over scan time, disabling autocrop\n"
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
192 return None
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
193 self.msg_prepare += "Crop detected: %s\n" % option
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
194 return option
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
195
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
196 def __get_audiomap(self, info):
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
197 """
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
198 Select the wanted german and english audio streams from ffmpeg info
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
199 output: mapping list
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
200 """
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
201 audiomap = []
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
202 audioall = filter_lines(info, "Audio:")
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
203 audio = filter_lines(audioall, "(deu):")
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
204 aidx = self.get_stream_index(
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
205 filter_lines(audio, "ac3"))
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
206 if aidx == "":
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
207 print audioall
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
208 print "No AC3 german audio stream found"
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
209 # try to find the first german audio stream
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
210 aidx = self.get_stream_index(audio.split("\n")[0])
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
211 if aidx == "":
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
212 print "No other german audio streams, trying english..."
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
213 else:
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
214 print "Selecting first german stream."
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
215 audiomap.append(aidx)
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
216 else:
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
217 audiomap.append(aidx)
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
218
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
219 audio = filter_lines(audioall, "(eng):")
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
220 aidx = self.get_stream_index(
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
221 filter_lines(audio, "ac3"))
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
222 if aidx != "" and filter_lines(audio, "ac3").find(" 0 channels ") < 1:
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
223 # append english audio too!
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
224 print "Selecting english ac3 stream."
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
225 audiomap.append(aidx)
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
226 return audiomap
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
227
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
228 def get_ffmpeg_command(self):
10
mdd
parents: 9
diff changeset
229 """
mdd
parents: 9
diff changeset
230 Too complex to describe, this does all the magic
mdd
parents: 9
diff changeset
231 output: produces internal ffmpeg command list (empty command list on error)
mdd
parents: 9
diff changeset
232 """
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
233 if not self.filename:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
234 return None
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
235
11
821c02fa7070 do not overwrite output file if exists
mdd
parents: 10
diff changeset
236
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
237 commands = []
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
238 fn = ffmpeg_filename(self.filename)
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
239 outfn = self.outfilebase + ".mkv"
11
821c02fa7070 do not overwrite output file if exists
mdd
parents: 10
diff changeset
240 # double-check: pull the kill switch and exit if outfile exists already!
821c02fa7070 do not overwrite output file if exists
mdd
parents: 10
diff changeset
241 # we do not want to overwrite files in accident (caused by automatic file naming)
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
242 if not self.overwrite and len(glob.glob(outfn)) > 0:
11
821c02fa7070 do not overwrite output file if exists
mdd
parents: 10
diff changeset
243 print "Output file exists: %s" % outfn
821c02fa7070 do not overwrite output file if exists
mdd
parents: 10
diff changeset
244 print "NOT overwriting it!"
821c02fa7070 do not overwrite output file if exists
mdd
parents: 10
diff changeset
245 return None
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
246 outfn = ffmpeg_filename(outfn)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
247
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
248 cmd = [
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
249 "ffmpeg", "-hide_banner",
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
250 "-ss 00:05:00", "-t 2", # search to 5 minutes, analyze 2 seconds
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
251 "-i %s" % fn,
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
252 "-vf \"cropdetect=24:2:0\"", # detect black bar crop on top and bottom
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
253 "-f null", "-" # no output file
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
254 ]
10
mdd
parents: 9
diff changeset
255 p = subprocess.Popen(shlex.split(" ".join(cmd)), \
mdd
parents: 9
diff changeset
256 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
257 out, err = p.communicate()
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
258 self.msg_ffmpeg = out + "\n" + err
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
259 self.msg_ffmpeg = self.msg_ffmpeg[self.msg_ffmpeg.find("Input #0"):]
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
260
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
261 # find "Stream #0:" lines
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
262 info = filter_lines(self.msg_ffmpeg, "Stream #0:")
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
263
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
264 v = self.get_stream_index(
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
265 filter_lines(info, "Video:"))
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
266 if v == "":
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
267 print "No video stream found"
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
268 return None
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
269
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
270 # get total duration and fps from input stream
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
271 # Input #0, mpegts, from '/srv/storage0/DREAMBOX/Action/Transporter/20101201 0630 - Sky Action HD - Transporter 3.ts':
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
272 # Duration: 01:39:59.88, start: 93674.825111, bitrate: 9365 kb/s
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
273 # Stream #0:1[0x1ff]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 50 tbr, 90k tbn, 50 tbc
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
274 self.frames_total = filter_lines(self.msg_ffmpeg, "Duration:").strip()[10:]
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
275 self.frames_total = self.frames_total[0:self.frames_total.find(",")].strip()
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
276 print "Input duration: %s" % self.frames_total
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
277 try:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
278 self.frames_total = int(self.frames_total[0:2]) * 3600 + \
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
279 int(self.frames_total[3:5]) * 60 + int(self.frames_total[6:8])
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
280 except ValueError:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
281 self.frames_total = 0
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
282
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
283 tmp = filter_lines(info, "Video:").split(",")
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
284 for fps in tmp:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
285 if fps.strip().endswith('fps'):
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
286 try:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
287 self.fps = float(fps.strip().split(' ')[0])
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
288 except ValueError:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
289 self.fps = 0
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
290 break
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
291 self.frames_total = round(self.frames_total * self.fps, 0)
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
292 print "Input framerate: %f fps" % self.fps
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
293 print "Total frames of input file: %i" % (self.frames_total)
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
294
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
295 # copy ALL subtitle streams if present!
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
296 # Stream #0:0[0x20](deu): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006), 492x250
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
297 submap = []
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
298 for tmp in filter_lines(info, "Subtitle: dvb_teletext").split("\n"):
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
299 if self.get_stream_index(tmp):
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
300 submap.append(self.get_stream_index(tmp))
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
301 # Subtitles disabled, that doesnt work as expected, dreambox crashes on copied subtitle stream
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
302 submap = []
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
303
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
304 # select audio streams
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
305 audiomap = self.__get_audiomap(info)
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
306 if len(audiomap) == 0:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
307 print "No suitable audio stream found, aborting."
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
308 return None
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
309
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
310 # Old dreambox images did a file split: .ts .ts.001 .ts.002 etc.
11
821c02fa7070 do not overwrite output file if exists
mdd
parents: 10
diff changeset
311 # Find all these files and join them!
12
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
312 inputs = [fn]
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
313 if os.path.splitext(fn)[1].lower() == '.ts':
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
314 for fpart in glob.glob(self.filename + '.' + ('[0-9]' * 3)):
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
315 fn = "\\'".join(p for p in fpart.split("'"))
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
316 fn = fn.replace(" ", "\\ ")
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
317 inputs.append(fn)
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
318
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
319 if len(inputs) > 1:
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
320 # use ffmpeg input concat function
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
321 # attention, ffmpeg doesnt like escape sequences
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
322 fn = "\"concat:" + \
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
323 "|".join(inputs)\
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
324 .replace('\ ', ' ')\
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
325 .replace("\'", "'")\
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
326 + "\""
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
327 # no ETA calculation possible since we have only the length of first file
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
328 # TODO: we COULD estimate by multiplying with factor generated by input file sizes
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
329 print "NO ETA POSSIBLE"
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
330 self.frames_total = 0
12
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
331
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
332 idx = 0
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
333 for tmp in inputs:
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
334 self.msg_prepare += "Input file #%i: %s\n" % (
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
335 idx, os.path.basename(tmp))
f0c7db3b239d ffmpeg concatenate multiple input files .ts .ts.NNN
mdd
parents: 11
diff changeset
336 idx += 1
11
821c02fa7070 do not overwrite output file if exists
mdd
parents: 10
diff changeset
337
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
338 cmd = [
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
339 "ffmpeg", "-hide_banner",
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
340 "-i %s" % fn,
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
341 ]
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
342
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
343 if self.overwrite:
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
344 cmd.append("-y")
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
345
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
346 for tmp in submap:
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
347 self.msg_prepare += "Subtitle Stream selected: Stream #%s\n" % tmp
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
348 cmd.append("-map %s" % tmp)
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
349
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
350 cmd.append("-map %s" % v)
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
351 self.msg_prepare += "Video Stream selected: Stream #%s\n" % v
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
352
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
353 flt = []
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
354 crop = self.get_crop_option()
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
355 if crop:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
356 flt.append(crop)
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
357 if self.scaleto_720p:
10
mdd
parents: 9
diff changeset
358 # -2 ensures division by two for codec
mdd
parents: 9
diff changeset
359 flt.append("scale='min(1280,iw)':-2'")
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
360 self.msg_prepare += "Scaling output stream to 720p if width >1280\n"
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
361 if len(flt) > 0:
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
362 # append video filters
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
363 cmd.append('-filter:v "%s"' % ",".join(flt))
14
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
364 for tmp in audiomap:
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
365 self.msg_prepare += "Audio Stream selected: Stream #%s\n" % tmp
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
366 cmd.append("-map %s" % tmp)
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
367 if len(submap) > 0:
b398ae388328 added support for dvb_teletext subtitles
mdd
parents: 13
diff changeset
368 cmd.append("-c:s dvdsub")
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
369 cmd.extend(self.video_options)
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
370 cmd.extend(self.audio_options)
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
371 cmd.append(outfn)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
372
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
373 commands.append(" ".join(cmd))
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
374 return commands
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
375
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
376 def load(self, filename):
10
mdd
parents: 9
diff changeset
377 """
mdd
parents: 9
diff changeset
378 First step: setup, analyze & prepare for conversion
mdd
parents: 9
diff changeset
379 """
13
cf5c5cec1b2b bugfix: cleanup status messages when processing multiple files
mdd
parents: 12
diff changeset
380 self.msg_prepare = ""
cf5c5cec1b2b bugfix: cleanup status messages when processing multiple files
mdd
parents: 12
diff changeset
381 self.msg_eit = ""
cf5c5cec1b2b bugfix: cleanup status messages when processing multiple files
mdd
parents: 12
diff changeset
382 self.msg_ffmpeg = ""
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
383 self.fps = 0
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
384 self.frames_total = 0
13
cf5c5cec1b2b bugfix: cleanup status messages when processing multiple files
mdd
parents: 12
diff changeset
385
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
386 self.filename = filename
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
387 self.outfilebase = os.path.splitext(filename)[0]
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
388 self.get_movie_description()
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
389 self.command = self.get_ffmpeg_command()
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
390
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
391 def convert(self):
10
mdd
parents: 9
diff changeset
392 """
mdd
parents: 9
diff changeset
393 Second step: write info text file and start ffmpeg conversion
mdd
parents: 9
diff changeset
394 requires successful load as first step
mdd
parents: 9
diff changeset
395 returns ffmpeg conversion exit status
mdd
parents: 9
diff changeset
396 """
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
397 if not self.command:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
398 return None
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
399 fd = open(self.outfilebase + ".txt", "wb")
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
400 fd.write(self.msg_eit)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
401 fd.write("\n\n# ---DEBUG---\n\n")
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
402 fd.write(self.msg_prepare)
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
403 fd.write(self.msg_ffmpeg)
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
404 fd.close()
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
405 #print self.msg_ffmpeg
7
0d021d47eca5 added ts2mkv converter
mdd
parents:
diff changeset
406
10
mdd
parents: 9
diff changeset
407 for cmd in self.command:
8
92409c985e0d added autocrop and 720p rescaler
mdd
parents: 7
diff changeset
408 print "Executing ffmpeg:\n%s\n" % cmd
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
409 #return run_command(cmd, self.total_frames)
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
410 return run_ffmpeg_watch(cmd, frames_total=self.frames_total)
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
411
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
412
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
413
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
414 if __name__ == "__main__":
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
415 # parse command line options
10
mdd
parents: 9
diff changeset
416 import argparse, glob
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
417
10
mdd
parents: 9
diff changeset
418 parser = argparse.ArgumentParser(description='DVB-TS to MKV kung-fu')
mdd
parents: 9
diff changeset
419 parser.add_argument('--crf', type=int, default=19, \
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
420 help='h264 crf (default 19)')
10
mdd
parents: 9
diff changeset
421 parser.add_argument('--tune', default='film', \
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
422 help='ffmpeg tune preset [film, animation] (default is film)')
10
mdd
parents: 9
diff changeset
423 parser.add_argument('--ns', action='store_true', default=False, \
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
424 help='no rescaling (default is scale to 720p)')
10
mdd
parents: 9
diff changeset
425 parser.add_argument('--rename', action='store_true', default=False, \
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
426 help='rename file basename to name and genre from EIT file if present')
10
mdd
parents: 9
diff changeset
427 parser.add_argument('input', metavar='input', nargs='+', \
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
428 help='one or more files, glob style syntax')
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
429 parser.add_argument('-f', action='store_true', default=False, \
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
430 help='force overwrite of existing file')
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
431
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
432 args = parser.parse_args()
10
mdd
parents: 9
diff changeset
433 processor = ts2mkv(crf=args.crf, tune=args.tune, scaleto_720p=(not args.ns), \
mdd
parents: 9
diff changeset
434 rename=args.rename)
15
82361ad7b3fe some changes, also implemented ffmpeg progress info and added force overwrite mode
mdd
parents: 14
diff changeset
435 processor.overwrite = args.f
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
436
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
437 for srcstr in args.input:
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
438 src = glob.glob(srcstr)
10
mdd
parents: 9
diff changeset
439 for srcfile in src:
mdd
parents: 9
diff changeset
440 print "Processing: %s" % srcfile
mdd
parents: 9
diff changeset
441 processor.load(srcfile)
mdd
parents: 9
diff changeset
442 processor.convert()
9
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
443
1bf778001041 added option parser and automatic file rename feature
mdd
parents: 8
diff changeset
444

mercurial