1 import subprocess
, shutil
, os
, re
, sys
, ConfigParser
, time
, lrucache
4 info_cache
= lrucache
.LRUCache(1000)
7 debug
= Config
.getDebug()
8 TIVO_WIDTH
= Config
.getTivoWidth()
9 TIVO_HEIGHT
= Config
.getTivoHeight()
10 MAX_VIDEO_BR
= Config
.getMaxVideoBR()
11 BUFF_SIZE
= Config
.getBuffSize()
13 FFMPEG
= Config
.get('Server', 'ffmpeg')
15 def debug_write(data
):
19 debug_out
.append(str(x
))
20 fdebug
= open('debug.txt', 'a')
21 fdebug
.write(' '.join(debug_out
))
25 # subprocess is broken for me on windows so super hack
26 def patchSubprocess():
27 o
= subprocess
.Popen
._make
_inheritable
29 def _make_inheritable(self
, handle
):
30 if not handle
: return subprocess
.GetCurrentProcess()
31 return o(self
, handle
)
33 subprocess
.Popen
._make
_inheritable
= _make_inheritable
34 mswindows
= (sys
.platform
== "win32")
38 def output_video(inFile
, outFile
, tsn
=''):
39 if tivo_compatable(inFile
):
40 debug_write(['output_video: ', inFile
, ' is tivo compatible\n'])
41 f
= file(inFile
, 'rb')
42 shutil
.copyfileobj(f
, outFile
)
45 debug_write(['output_video: ', inFile
, ' is not tivo compatible\n'])
46 transcode(inFile
, outFile
, tsn
)
48 def transcode(inFile
, outFile
, tsn
=''):
49 audio_br
= Config
.getAudioBR(tsn
)
50 video_br
= Config
.getVideoBR(tsn
)
54 '-vcodec', 'mpeg2video',
57 '-maxrate', MAX_VIDEO_BR
,
59 ] + select_aspect(inFile
, tsn
) + [
60 '-comment', 'pyTivo.py',
67 debug_write(['transcode: ffmpeg command is ', ''.join(cmd
), '\n'])
68 ffmpeg
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
70 shutil
.copyfileobj(ffmpeg
.stdout
, outFile
)
74 def select_aspect(inFile
, tsn
= ''):
75 type, width
, height
, fps
, millisecs
= video_info(inFile
)
77 debug_write(['tsn:', tsn
, '\n'])
79 aspect169
= Config
.get169Setting(tsn
)
81 debug_write(['aspect169:', aspect169
, '\n'])
84 ratio
= (width
*100)/height
85 rheight
, rwidth
= height
/d
, width
/d
87 debug_write(['select_aspect: File=', inFile
, ' Type=', type, ' width=', width
, ' height=', height
, ' fps=', fps
, ' millisecs=', millisecs
, ' ratio=', ratio
, ' rheight=', rheight
, ' rwidth=', rwidth
, '\n'])
89 multiplier16by9
= (16.0 * TIVO_HEIGHT
) / (9.0 * TIVO_WIDTH
)
90 multiplier4by3
= (4.0 * TIVO_HEIGHT
) / (3.0 * TIVO_WIDTH
)
92 if (rwidth
, rheight
) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)]:
93 debug_write(['select_aspect: File is within 4:3 list.\n'])
94 return ['-aspect', '4:3', '-s', str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
)]
95 elif ((rwidth
, rheight
) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)]) and aspect169
:
96 debug_write(['select_aspect: File is within 16:9 list and 16:9 allowed.\n'])
97 return ['-aspect', '16:9', '-s', str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
)]
100 #If video is wider than 4:3 add top and bottom padding
101 if (ratio
> 133): #Might be 16:9 file, or just need padding on top and bottom
102 if aspect169
and (ratio
> 135): #If file would fall in 4:3 assume it is supposed to be 4:3
103 if (ratio
> 177):#too short needs padding top and bottom
104 endHeight
= int(((TIVO_WIDTH
*height
)/width
) * multiplier16by9
)
105 settings
.append('-aspect')
106 settings
.append('16:9')
109 if endHeight
< TIVO_HEIGHT
* 0.99:
110 settings
.append('-s')
111 settings
.append(str(TIVO_WIDTH
) + 'x' + str(endHeight
))
113 topPadding
= ((TIVO_HEIGHT
- endHeight
)/2)
117 settings
.append('-padtop')
118 settings
.append(str(topPadding
))
119 bottomPadding
= (TIVO_HEIGHT
- endHeight
) - topPadding
120 settings
.append('-padbottom')
121 settings
.append(str(bottomPadding
))
122 else: #if only very small amount of padding needed, then just stretch it
123 settings
.append('-s')
124 settings
.append(str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
))
125 debug_write(['select_aspect: 16:9 aspect allowed, file is wider than 16:9 padding top and bottom\n', ' '.join(settings
), '\n'])
126 else: #too skinny needs padding on left and right.
127 endWidth
= int((TIVO_HEIGHT
*width
)/(height
*multiplier16by9
))
128 settings
.append('-aspect')
129 settings
.append('16:9')
132 if endWidth
< (TIVO_WIDTH
-10):
133 settings
.append('-s')
134 settings
.append(str(endWidth
) + 'x' + str(TIVO_HEIGHT
))
136 leftPadding
= ((TIVO_WIDTH
- endWidth
)/2)
140 settings
.append('-padleft')
141 settings
.append(str(leftPadding
))
142 rightPadding
= (TIVO_WIDTH
- endWidth
) - leftPadding
143 settings
.append('-padright')
144 settings
.append(str(rightPadding
))
145 else: #if only very small amount of padding needed, then just stretch it
146 settings
.append('-s')
147 settings
.append(str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
))
148 debug_write(['select_aspect: 16:9 aspect allowed, file is narrower than 16:9 padding left and right\n', ' '.join(settings
), '\n'])
149 else: #this is a 4:3 file or 16:9 output not allowed
150 settings
.append('-aspect')
151 settings
.append('4:3')
152 endHeight
= int(((TIVO_WIDTH
*height
)/width
) * multiplier4by3
)
155 if endHeight
< TIVO_HEIGHT
* 0.99:
156 settings
.append('-s')
157 settings
.append(str(TIVO_WIDTH
) + 'x' + str(endHeight
))
159 topPadding
= ((TIVO_HEIGHT
- endHeight
)/2)
163 settings
.append('-padtop')
164 settings
.append(str(topPadding
))
165 bottomPadding
= (TIVO_HEIGHT
- endHeight
) - topPadding
166 settings
.append('-padbottom')
167 settings
.append(str(bottomPadding
))
168 else: #if only very small amount of padding needed, then just stretch it
169 settings
.append('-s')
170 settings
.append(str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
))
171 debug_write(['select_aspect: File is wider than 4:3 padding top and bottom\n', ' '.join(settings
), '\n'])
174 #If video is taller than 4:3 add left and right padding, this is rare. All of these files will always be sent in
175 #an aspect ratio of 4:3 since they are so narrow.
177 endWidth
= int((TIVO_HEIGHT
*width
)/(height
*multiplier4by3
))
178 settings
.append('-aspect')
179 settings
.append('4:3')
182 if endWidth
< (TIVO_WIDTH
* 0.99):
183 settings
.append('-s')
184 settings
.append(str(endWidth
) + 'x' + str(TIVO_HEIGHT
))
186 leftPadding
= ((TIVO_WIDTH
- endWidth
)/2)
190 settings
.append('-padleft')
191 settings
.append(str(leftPadding
))
192 rightPadding
= (TIVO_WIDTH
- endWidth
) - leftPadding
193 settings
.append('-padright')
194 settings
.append(str(rightPadding
))
195 else: #if only very small amount of padding needed, then just stretch it
196 settings
.append('-s')
197 settings
.append(str(TIVO_WIDTH
) + 'x' + str(TIVO_HEIGHT
))
199 debug_write(['select_aspect: File is taller than 4:3 padding left and right\n', ' '.join(settings
), '\n'])
203 def tivo_compatable(inFile
):
204 suportedModes
= [[720, 480], [704, 480], [544, 480], [480, 480], [352, 480]]
205 type, width
, height
, fps
, millisecs
= video_info(inFile
)
206 #print type, width, height, fps, millisecs
208 if (inFile
[-5:]).lower() == '.tivo':
209 debug_write(['tivo_compatible: ', inFile
, ' ends with .tivo\n'])
212 if not type == 'mpeg2video':
213 #print 'Not Tivo Codec'
214 debug_write(['tivo_compatible: ', inFile
, ' is not mpeg2video it is ', type, '\n'])
217 if not fps
== '29.97':
218 #print 'Not Tivo fps'
219 debug_write(['tivo_compatible: ', inFile
, ' is not correct fps it is ', fps
, '\n'])
222 for mode
in suportedModes
:
223 if (mode
[0], mode
[1]) == (width
, height
):
225 debug_write(['tivo_compatible: ', inFile
, ' has correct width of ', width
, ' and height of ', height
, '\n'])
227 #print 'Not Tivo dimensions'
230 def video_info(inFile
):
231 mtime
= os
.stat(inFile
).st_mtime
232 if inFile
in info_cache
and info_cache
[inFile
][0] == mtime
:
233 debug_write(['video_info: ', inFile
, ' cache hit!', '\n'])
234 return info_cache
[inFile
][1]
236 if (inFile
[-5:]).lower() == '.tivo':
237 info_cache
[inFile
] = (mtime
, (True, True, True, True, True))
238 debug_write(['video_info: ', inFile
, ' ends in .tivo.\n'])
239 return True, True, True, True, True
241 cmd
= [FFMPEG
, '-i', inFile
]
242 ffmpeg
= subprocess
.Popen(cmd
, stderr
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
244 # wait 4 sec if ffmpeg is not back give up
247 if not ffmpeg
.poll() == None:
250 if ffmpeg
.poll() == None:
252 info_cache
[inFile
] = (mtime
, (None, None, None, None, None))
253 return None, None, None, None, None
255 output
= ffmpeg
.stderr
.read()
256 debug_write(['video_info: ffmpeg output=', output
, '\n'])
258 durre
= re
.compile(r
'.*Duration: (.{2}):(.{2}):(.{2})\.(.),')
259 d
= durre
.search(output
)
261 rezre
= re
.compile(r
'.*Video: ([^,]+),.*')
262 x
= rezre
.search(output
)
266 info_cache
[inFile
] = (mtime
, (None, None, None, None, None))
267 debug_write(['video_info: failed at codec\n'])
268 return None, None, None, None, None
270 rezre
= re
.compile(r
'.*Video: .+, (\d+)x(\d+),.*')
271 x
= rezre
.search(output
)
273 width
= int(x
.group(1))
274 height
= int(x
.group(2))
276 info_cache
[inFile
] = (mtime
, (None, None, None, None, None))
277 debug_write(['video_info: failed at width/height\n'])
278 return None, None, None, None, None
280 rezre
= re
.compile(r
'.*Video: .+, (.+) fps.*')
281 x
= rezre
.search(output
)
285 info_cache
[inFile
] = (mtime
, (None, None, None, None, None))
286 debug_write(['video_info: failed at fps\n'])
287 return None, None, None, None, None
289 # Allow override only if it is mpeg2 and frame rate was doubled to 59.94
290 if (not fps
== '29.97') and (codec
== 'mpeg2video'):
291 # First look for the build 7215 version
292 rezre
= re
.compile(r
'.*film source: 29.97.*')
293 x
= rezre
.search(output
.lower() )
295 debug_write(['video_info: film source: 29.97 setting fps to 29.97\n'])
299 rezre
= re
.compile(r
'.*frame rate differs from container frame rate: 29.97.*')
300 debug_write(['video_info: Bug in VideoReDo\n'])
301 x
= rezre
.search(output
.lower() )
305 millisecs
= ((int(d
.group(1))*3600) + (int(d
.group(2))*60) + int(d
.group(3)))*1000 + (int(d
.group(4))*100)
306 info_cache
[inFile
] = (mtime
, (codec
, width
, height
, fps
, millisecs
))
307 debug_write(['video_info: Codec=', codec
, ' width=', width
, ' height=', height
, ' fps=', fps
, ' millisecs=', millisecs
, '\n'])
308 return codec
, width
, height
, fps
, millisecs
310 def suported_format(inFile
):
311 if video_info(inFile
)[0]:
314 debug_write(['supported_format: ', inFile
, ' is not supported\n'])
318 debug_write(['kill: killing pid=', str(pid
), '\n'])
323 os
.kill(pid
, signal
.SIGKILL
)
327 handle
= ctypes
.windll
.kernel32
.OpenProcess(1, False, pid
)
328 ctypes
.windll
.kernel32
.TerminateProcess(handle
, -1)
329 ctypes
.windll
.kernel32
.CloseHandle(handle
)