1 import subprocess
, shutil
, os
, re
, sys
3 SCRIPTDIR
= os
.path
.dirname(__file__
)
4 FFMPEG
= os
.path
.join(SCRIPTDIR
, 'ffmpeg_mp2.exe')
7 # subprocess is broken for me on windows so super hack
9 o
= subprocess
.Popen
._make
_inheritable
11 def _make_inheritable(self
, handle
):
12 if not handle
: return subprocess
.GetCurrentProcess()
13 return o(self
, handle
)
15 subprocess
.Popen
._make
_inheritable
= _make_inheritable
16 mswindows
= (sys
.platform
== "win32")
20 def output_video(inFile
, outFile
):
21 if tivo_compatable(inFile
):
22 f
= file(inFile
, 'rb')
23 shutil
.copyfileobj(f
, outFile
)
26 transcode(inFile
, outFile
)
28 def transcode(inFile
, outFile
):
30 cmd
= [FFMPEG
, '-i', inFile
, '-vcodec', 'mpeg2video', '-r', '29.97', '-b', '4096'] + select_aspect(inFile
) + ['-ac', '2', '-ab', '192', '-f', 'vob', '-' ]
31 ffmpeg
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
33 shutil
.copyfileobj(ffmpeg
.stdout
, outFile
)
37 def select_aspect(inFile
):
38 type, height
, width
, fps
= video_info(inFile
)
42 rheight
, rwidth
= height
/d
, width
/d
44 if (rheight
, rwidth
) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)]:
45 return ['-aspect', '4:3', '-s', '720x480']
46 elif (rheight
, rwidth
) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)]:
47 return ['-aspect', '16:9', '-s', '720x480']
50 settings
.append('-aspect')
51 settings
.append('16:9')
53 endHeight
= (720*width
)/height
58 settings
.append('720x' + str(endHeight
))
60 topPadding
= ((480 - endHeight
)/2)
64 settings
.append('-padtop')
65 settings
.append(str(topPadding
))
66 bottomPadding
= (480 - endHeight
) - topPadding
67 settings
.append('-padbottom')
68 settings
.append(str(bottomPadding
))
72 def tivo_compatable(inFile
):
73 suportedModes
= [[720, 480], [704, 480], [544, 480], [480, 480], [352, 480]]
74 type, height
, width
, fps
= video_info(inFile
)
76 if not type == 'mpeg2video':
79 if not fps
== '29.97':
82 for mode
in suportedModes
:
83 if (mode
[0], mode
[1]) == (height
, width
):
87 def video_info(inFile
):
88 cmd
= [FFMPEG
, '-i', inFile
]
89 ffmpeg
= subprocess
.Popen(cmd
, stderr
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
90 output
= ffmpeg
.stderr
.read()
92 rezre
= re
.compile(r
'.*Video: (.+), (\d+)x(\d+), (.+) fps.*')
93 m
= rezre
.search(output
)
95 return m
.group(1), int(m
.group(2)), int(m
.group(3)), m
.group(4)
97 return None, None, None, None
99 def suported_format(inFile
):
100 if video_info(inFile
)[0]:
107 handle
= ctypes
.windll
.kernel32
.OpenProcess(1, False, pid
)
108 ctypes
.windll
.kernel32
.TerminateProcess(handle
, -1)
109 ctypes
.windll
.kernel32
.CloseHandle(handle
)