S3 mpeg settings
[pyTivo.git] / plugins / video / transcode.py
blob692a8b838cec96ca8e1dd6301106f20afa050a0b
1 import subprocess, shutil, os, re, sys, ConfigParser, time, lrucache
2 import config
4 info_cache = lrucache.LRUCache(1000)
7 debug = config.getDebug()
8 MAX_VIDEO_BR = config.getMaxVideoBR()
9 BUFF_SIZE = config.getBuffSize()
11 FFMPEG = config.get('Server', 'ffmpeg')
13 def debug_write(data):
14 if debug:
15 debug_out = []
16 for x in data:
17 debug_out.append(str(x))
18 fdebug = open('debug.txt', 'a')
19 fdebug.write(' '.join(debug_out))
20 fdebug.close()
22 # XXX BIG HACK
23 # subprocess is broken for me on windows so super hack
24 def patchSubprocess():
25 o = subprocess.Popen._make_inheritable
27 def _make_inheritable(self, handle):
28 if not handle: return subprocess.GetCurrentProcess()
29 return o(self, handle)
31 subprocess.Popen._make_inheritable = _make_inheritable
32 mswindows = (sys.platform == "win32")
33 if mswindows:
34 patchSubprocess()
36 def output_video(inFile, outFile, tsn=''):
37 if tivo_compatable(inFile, tsn):
38 debug_write(['output_video: ', inFile, ' is tivo compatible\n'])
39 f = file(inFile, 'rb')
40 shutil.copyfileobj(f, outFile)
41 f.close()
42 else:
43 debug_write(['output_video: ', inFile, ' is not tivo compatible\n'])
44 transcode(inFile, outFile, tsn)
46 def transcode(inFile, outFile, tsn=''):
48 settings = {}
49 settings['audio_br'] = config.getAudioBR(tsn)
50 settings['video_br'] = config.getVideoBR(tsn)
51 settings['max_video_br'] = MAX_VIDEO_BR
52 settings['buff_size'] = BUFF_SIZE
53 settings['aspect_ratio'] = ' '.join(select_aspect(inFile, tsn))
55 cmd_string = config.getFFMPEGTemplate(tsn) % settings
57 cmd = [FFMPEG, '-i', inFile] + cmd_string.split()
59 debug_write(['transcode: ffmpeg command is ', ' '.join(cmd), '\n'])
60 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)
61 try:
62 shutil.copyfileobj(ffmpeg.stdout, outFile)
63 except:
64 kill(ffmpeg.pid)
66 def select_aspect(inFile, tsn = ''):
67 TIVO_WIDTH = config.getTivoWidth(tsn)
68 TIVO_HEIGHT = config.getTivoHeight(tsn)
70 type, width, height, fps, millisecs = video_info(inFile)
72 debug_write(['tsn:', tsn, '\n'])
74 aspect169 = config.get169Setting(tsn)
76 debug_write(['aspect169:', aspect169, '\n'])
78 optres = config.getOptres()
80 debug_write(['optres:', optres, '\n'])
82 if optres:
83 optHeight = Config.nearestTivoHeight(height)
84 optWidth = Config.nearestTivoWidth(width)
85 if optHeight < TIVO_HEIGHT:
86 TIVO_HEIGHT = optHeight
87 if optWidth < TIVO_WIDTH:
88 TIVO_WIDTH = optWidth
90 d = gcd(height,width)
91 ratio = (width*100)/height
92 rheight, rwidth = height/d, width/d
94 debug_write(['select_aspect: File=', inFile, ' Type=', type, ' width=', width, ' height=', height, ' fps=', fps, ' millisecs=', millisecs, ' ratio=', ratio, ' rheight=', rheight, ' rwidth=', rwidth, ' TIVO_HEIGHT=', TIVO_HEIGHT, 'TIVO_WIDTH=', TIVO_WIDTH, '\n'])
96 multiplier16by9 = (16.0 * TIVO_HEIGHT) / (9.0 * TIVO_WIDTH)
97 multiplier4by3 = (4.0 * TIVO_HEIGHT) / (3.0 * TIVO_WIDTH)
99 if (rwidth, rheight) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)]:
100 debug_write(['select_aspect: File is within 4:3 list.\n'])
101 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
102 elif ((rwidth, rheight) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)]) and aspect169:
103 debug_write(['select_aspect: File is within 16:9 list and 16:9 allowed.\n'])
104 return ['-aspect', '16:9', '-s', str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT)]
105 else:
106 settings = []
107 #If video is wider than 4:3 add top and bottom padding
108 if (ratio > 133): #Might be 16:9 file, or just need padding on top and bottom
109 if aspect169 and (ratio > 135): #If file would fall in 4:3 assume it is supposed to be 4:3
110 if (ratio > 177):#too short needs padding top and bottom
111 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier16by9)
112 settings.append('-aspect')
113 settings.append('16:9')
114 if endHeight % 2:
115 endHeight -= 1
116 if endHeight < TIVO_HEIGHT * 0.99:
117 settings.append('-s')
118 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
120 topPadding = ((TIVO_HEIGHT - endHeight)/2)
121 if topPadding % 2:
122 topPadding -= 1
124 settings.append('-padtop')
125 settings.append(str(topPadding))
126 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
127 settings.append('-padbottom')
128 settings.append(str(bottomPadding))
129 else: #if only very small amount of padding needed, then just stretch it
130 settings.append('-s')
131 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
132 debug_write(['select_aspect: 16:9 aspect allowed, file is wider than 16:9 padding top and bottom\n', ' '.join(settings), '\n'])
133 else: #too skinny needs padding on left and right.
134 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier16by9))
135 settings.append('-aspect')
136 settings.append('16:9')
137 if endWidth % 2:
138 endWidth -= 1
139 if endWidth < (TIVO_WIDTH-10):
140 settings.append('-s')
141 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
143 leftPadding = ((TIVO_WIDTH - endWidth)/2)
144 if leftPadding % 2:
145 leftPadding -= 1
147 settings.append('-padleft')
148 settings.append(str(leftPadding))
149 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
150 settings.append('-padright')
151 settings.append(str(rightPadding))
152 else: #if only very small amount of padding needed, then just stretch it
153 settings.append('-s')
154 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
155 debug_write(['select_aspect: 16:9 aspect allowed, file is narrower than 16:9 padding left and right\n', ' '.join(settings), '\n'])
156 else: #this is a 4:3 file or 16:9 output not allowed
157 settings.append('-aspect')
158 settings.append('4:3')
159 endHeight = int(((TIVO_WIDTH*height)/width) * multiplier4by3)
160 if endHeight % 2:
161 endHeight -= 1
162 if endHeight < TIVO_HEIGHT * 0.99:
163 settings.append('-s')
164 settings.append(str(TIVO_WIDTH) + 'x' + str(endHeight))
166 topPadding = ((TIVO_HEIGHT - endHeight)/2)
167 if topPadding % 2:
168 topPadding -= 1
170 settings.append('-padtop')
171 settings.append(str(topPadding))
172 bottomPadding = (TIVO_HEIGHT - endHeight) - topPadding
173 settings.append('-padbottom')
174 settings.append(str(bottomPadding))
175 else: #if only very small amount of padding needed, then just stretch it
176 settings.append('-s')
177 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
178 debug_write(['select_aspect: File is wider than 4:3 padding top and bottom\n', ' '.join(settings), '\n'])
180 return settings
181 #If video is taller than 4:3 add left and right padding, this is rare. All of these files will always be sent in
182 #an aspect ratio of 4:3 since they are so narrow.
183 else:
184 endWidth = int((TIVO_HEIGHT*width)/(height*multiplier4by3))
185 settings.append('-aspect')
186 settings.append('4:3')
187 if endWidth % 2:
188 endWidth -= 1
189 if endWidth < (TIVO_WIDTH * 0.99):
190 settings.append('-s')
191 settings.append(str(endWidth) + 'x' + str(TIVO_HEIGHT))
193 leftPadding = ((TIVO_WIDTH - endWidth)/2)
194 if leftPadding % 2:
195 leftPadding -= 1
197 settings.append('-padleft')
198 settings.append(str(leftPadding))
199 rightPadding = (TIVO_WIDTH - endWidth) - leftPadding
200 settings.append('-padright')
201 settings.append(str(rightPadding))
202 else: #if only very small amount of padding needed, then just stretch it
203 settings.append('-s')
204 settings.append(str(TIVO_WIDTH) + 'x' + str(TIVO_HEIGHT))
206 debug_write(['select_aspect: File is taller than 4:3 padding left and right\n', ' '.join(settings), '\n'])
208 return settings
210 def tivo_compatable(inFile, tsn = ''):
211 suportedModes = [[720, 480], [704, 480], [544, 480], [480, 480], [352, 480]]
212 type, width, height, fps, millisecs = video_info(inFile)
213 #print type, width, height, fps, millisecs
215 if (inFile[-5:]).lower() == '.tivo':
216 debug_write(['tivo_compatible: ', inFile, ' ends with .tivo\n'])
217 return True
219 if not type == 'mpeg2video':
220 #print 'Not Tivo Codec'
221 debug_write(['tivo_compatible: ', inFile, ' is not mpeg2video it is ', type, '\n'])
222 return False
224 if tsn[:3] in ('648', '652'):
225 debug_write(['tivo_compatible: ', inFile, ' you have a S3 skiping the rest of the tests', '\n'])
226 return True
228 if not fps == '29.97':
229 #print 'Not Tivo fps'
230 debug_write(['tivo_compatible: ', inFile, ' is not correct fps it is ', fps, '\n'])
231 return False
233 for mode in suportedModes:
234 if (mode[0], mode[1]) == (width, height):
235 #print 'Is TiVo!'
236 debug_write(['tivo_compatible: ', inFile, ' has correct width of ', width, ' and height of ', height, '\n'])
237 return True
238 #print 'Not Tivo dimensions'
239 return False
241 def video_info(inFile):
242 mtime = os.stat(inFile).st_mtime
243 if inFile in info_cache and info_cache[inFile][0] == mtime:
244 debug_write(['video_info: ', inFile, ' cache hit!', '\n'])
245 return info_cache[inFile][1]
247 if (inFile[-5:]).lower() == '.tivo':
248 info_cache[inFile] = (mtime, (True, True, True, True, True))
249 debug_write(['video_info: ', inFile, ' ends in .tivo.\n'])
250 return True, True, True, True, True
252 cmd = [FFMPEG, '-i', inFile ]
253 ffmpeg = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
255 # wait 4 sec if ffmpeg is not back give up
256 for i in range(80):
257 time.sleep(.05)
258 if not ffmpeg.poll() == None:
259 break
261 if ffmpeg.poll() == None:
262 kill(ffmpeg.pid)
263 info_cache[inFile] = (mtime, (None, None, None, None, None))
264 return None, None, None, None, None
266 output = ffmpeg.stderr.read()
267 debug_write(['video_info: ffmpeg output=', output, '\n'])
269 rezre = re.compile(r'.*Video: ([^,]+),.*')
270 x = rezre.search(output)
271 if x:
272 codec = x.group(1)
273 else:
274 info_cache[inFile] = (mtime, (None, None, None, None, None))
275 debug_write(['video_info: failed at codec\n'])
276 return None, None, None, None, None
278 rezre = re.compile(r'.*Video: .+, (\d+)x(\d+)[, ].*')
279 x = rezre.search(output)
280 if x:
281 width = int(x.group(1))
282 height = int(x.group(2))
283 else:
284 info_cache[inFile] = (mtime, (None, None, None, None, None))
285 debug_write(['video_info: failed at width/height\n'])
286 return None, None, None, None, None
288 rezre = re.compile(r'.*Video: .+, (.+) (?:fps|tb).*')
289 x = rezre.search(output)
290 if x:
291 fps = x.group(1)
292 else:
293 info_cache[inFile] = (mtime, (None, None, None, None, None))
294 debug_write(['video_info: failed at fps\n'])
295 return None, None, None, None, None
297 # Allow override only if it is mpeg2 and frame rate was doubled to 59.94
298 if (not fps == '29.97') and (codec == 'mpeg2video'):
299 # First look for the build 7215 version
300 rezre = re.compile(r'.*film source: 29.97.*')
301 x = rezre.search(output.lower() )
302 if x:
303 debug_write(['video_info: film source: 29.97 setting fps to 29.97\n'])
304 fps = '29.97'
305 else:
306 # for build 8047:
307 rezre = re.compile(r'.*frame rate differs from container frame rate: 29.97.*')
308 debug_write(['video_info: Bug in VideoReDo\n'])
309 x = rezre.search(output.lower() )
310 if x:
311 fps = '29.97'
313 durre = re.compile(r'.*Duration: (.{2}):(.{2}):(.{2})\.(.),')
314 d = durre.search(output)
315 if d:
316 millisecs = ((int(d.group(1))*3600) + (int(d.group(2))*60) + int(d.group(3)))*1000 + (int(d.group(4))*100)
317 else:
318 millisecs = 0
320 info_cache[inFile] = (mtime, (codec, width, height, fps, millisecs))
321 debug_write(['video_info: Codec=', codec, ' width=', width, ' height=', height, ' fps=', fps, ' millisecs=', millisecs, '\n'])
322 return codec, width, height, fps, millisecs
324 def suported_format(inFile):
325 if video_info(inFile)[0]:
326 return True
327 else:
328 debug_write(['supported_format: ', inFile, ' is not supported\n'])
329 return False
331 def kill(pid):
332 debug_write(['kill: killing pid=', str(pid), '\n'])
333 if mswindows:
334 win32kill(pid)
335 else:
336 import os, signal
337 os.kill(pid, signal.SIGTERM)
339 def win32kill(pid):
340 import ctypes
341 handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
342 ctypes.windll.kernel32.TerminateProcess(handle, -1)
343 ctypes.windll.kernel32.CloseHandle(handle)
345 def gcd(a,b):
346 while b:
347 a, b = b, a % b
348 return a