forked from onlyhavecans/HandBreak-It
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandBreak.pyw
executable file
·280 lines (247 loc) · 9.24 KB
/
HandBreak.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
# encoding: utf-8
"""
Hand Break
Bulk encoder frontend for HandBreakCLI.
This is designed to help batch encode chunks of videos without having to
constantly use custom one line bash scripts. It uses presets to simplify the
process.
When run with no flags it gives a nice sparse GUI but you can run it fully
headless by running it with -i and -o flags. Check --help for all flags.
Created by David "BunnyMan" on 2011-08-13.
Copyright (c) 2011 White Rabbit Code. All rights reserved.
"""
from __future__ import print_function
import sys
import os
import re
from subprocess import call, check_output, Popen, STDOUT, PIPE
import argparse
import traceback
class HandbrakeError(Exception):
"""
The exception used for handbrake specific errors.
"""
pass
class HandBrake(object):
"""
API to HandBreakCLI
Locates the binary and offers access to it's presets and encoding
Example;
hb = Handbrake()
validPresetsTuple = hb.presets()
if hb.is_valid_preset('My Preset'):
success = hb.encode('C:\in\MyVid.avi', 'C:\Videos', 'My Preset')
"""
def __init__(self):
self._handBrakeCLI = self.find_handbrake()
self._validPresets = self.get_presets()
def presets(self):
"""
Return a Tuple of the valid presets for this handbrake instance
"""
return self._validPresets
def is_valid_preset(self, preset):
"""
Verify that the preset is valid in the current copy of HandBrakeCLI
Returns True or False
"""
if preset in self._validPresets:
return True
else:
return False
def encode(self, inFile, outFile, preset, output=sys.stdout):
"""
Encode inFile to outFile with preset.
Preset must be a valid preset as listed from self.presets()
Optionally you can pass it a file handle to write all the output too
Raises HandBreakError if preset is not valid
"""
if not self.is_valid_preset(preset):
raise HandbrakeError("Supplied preset not valid")
returnCode = False
process = Popen([self._handBrakeCLI, '-v', '0', '-m',
'-Z', preset,
'-i', inFile, '-o', outFile],
stderr=STDOUT, stdout=PIPE)
while process.returncode is None:
line = process.stdout.readline().strip()
print(line, file=output)
if line == "Rip done!":
returnCode = True
process.poll()
return returnCode
def find_handbrake(self):
"""
Finds and returns the handbrake binary as string.
It uses brute force but it should be OS agnostic.
Should be private.
"""
if sys.platform == 'win32':
if os.path.isfile("HandBrakeCLI.exe"):
return os.path.abspath("HandBrakeCLI.exe")
else:
raise HandbrakeError(
"HandbrakeCLI.exe not installed next to script!")
elif os.path.isfile("/Applications/HandBrakeCLI"):
return "/Applications/HandBrakeCLI"
elif call(['which', '-s', 'HandBrakeCLI']):
output = check_output(['which', 'HandBrakeCLI'])[0]
return output.strip()
else:
raise HandbrakeError("HandbrakeCLI not installed!")
def get_presets(self):
"""
Calls the HandBrakeCLI executable and returns all of it's valid
presets.
It's expensive so it shouldn't really be called more than once in an
objects lifetime.
Should be private.
"""
output = check_output([self._handBrakeCLI, '--preset-list'],
stderr=STDOUT)
pattern = re.compile('\+ ([\w\s]+):')
return tuple(re.findall(pattern, output))
def parse_arguments():
"""
Set up all the arguments the program takes, parse the programs arguments,
and then return the results of parse_args()
"""
parser = argparse.ArgumentParser(description="Batch encode a directory of\
video files using handbrake presets")
parser.add_argument('--in-directory', '-i',
help="Input directory. You need both -in & -out to run headless")
parser.add_argument('--out-directory', '-o',
help="Output directory. You need both -in & -out to run headless")
parser.add_argument('--recursive', '-r', action='store_false',
default=True, help="DISABLE recursive scanning of input directory")
parser.add_argument('--preset', '-p', default='Universal',
help="Handbrake preset to use, defaults to Apple Universal")
parser.add_argument('--list-presets', '-l', action='store_true',
help="List available presets and quit")
return parser.parse_args()
def print_presets(output=sys.stdout):
"""
Print the available presets and more info message to output
"""
handbrake = HandBrake()
presetTuple = handbrake.presets()
print("Available presets; {}.".format(", ".join(presetTuple)), output)
print("Please check HandBrake for more information.", output)
def check_valid_preset(preset):
"""
Return boolean to denote valid HandBrake preset
"""
handbrake = HandBrake()
return handbrake.is_valid_preset(preset)
def get_recursive_files(directory):
"""
Crawl the supplied path and return a list of the files
"""
fileArray = []
for (directory, subdirectories, files) in os.walk(directory):
for filename in files:
fileArray.append(os.path.join(directory, filename))
return fileArray
def get_output_file(inFile, outDirectory):
"""
Take the input the file and the desired output directory, using both to
generate the output file, with full path.
inFile can be just a filename or have the path to the file in it.
"""
inFileName = os.path.basename(inFile)
outFileName = os.path.splitext(inFileName)[0] + ".m4v"
return os.path.join(outDirectory, outFileName)
def gui_main(arguments):
"""
This is the gui version, it will prompt for what information it needs
and also uses pop ups to display errors
"""
import Tkinter
import tkMessageBox
import tkFileDialog
root = Tkinter.Tk()
root.withdraw()
inDirectory = tkFileDialog.askdirectory(title="Pick Video Directory",
mustexist=True)
outDirectory = tkFileDialog.askdirectory(title="Pick Output Directory",
mustexist=False)
if not inDirectory or not outDirectory:
tkMessageBox.showerror(
"Hand Break It",
"You have to select both in and out directories")
return 1
if not os.path.isdir(outDirectory):
os.makedirs(outDirectory)
if arguments.recursive:
videos = get_recursive_files(inDirectory)
else:
videos = [os.path.join(inDirectory, videoFile) for
videoFile in os.listdir(inDirectory)]
try:
for episode in videos:
outFile = get_output_file(episode, outDirectory)
handbrake = HandBrake()
handbrake.encode(episode, outFile, arguments.preset)
except OSError, errorMessage:
tkMessageBox.showerror(
"Hand Break It",
"I had a directory access error: {}".format(errorMessage))
return 1
except HandbrakeError, errorMessage:
tkMessageBox.showerror(
"Hand Break It",
"HandBrake had an error: {}".format(errorMessage))
return 1
except Exception:
tkMessageBox.showerror(
"Hand Break It error",
"I had an error:\n {}".format(traceback.format_exc()))
return 1
tkMessageBox.showinfo("Done",
"I am done!\nCheck the Log for details")
return 0
def cli_main(arguments):
"""
Bypassing all the heavy gui this is designed for quick batches and no
prompts, assuming everything needed was supplied to argsparse.
"""
inDirectory = os.path.expanduser(arguments.in_directory)
outDirectory = os.path.expanduser(arguments.out_directory)
if not os.path.isdir(outDirectory):
os.makedirs(outDirectory)
if arguments.recursive:
videos = get_recursive_files(inDirectory)
else:
videos = [os.path.join(inDirectory, videoFile) for
videoFile in os.listdir(inDirectory)]
try:
for episode in videos:
outFile = get_output_file(episode, outDirectory)
handbrake = HandBrake()
handbrake.encode(episode, outFile, arguments.preset)
except OSError, errorMessage:
print("I had a directory access error: {}".format(errorMessage))
return 1
except HandbrakeError, errorMessage:
print("HandBrake had an error: {}".format(errorMessage))
return 1
except Exception:
print("I had an error:\n {}".format(traceback.format_exc()))
return 1
print("I am done.", "Check the Log for details", sep="/n")
return 0
if __name__ == '__main__':
ARGS = parse_arguments()
if ARGS.list_presets:
print_presets()
sys.exit(0)
if not check_valid_preset(ARGS.preset):
print("\"{}\" is not in the valid preset list".format(
ARGS.preset))
print_presets()
sys.exit("9")
if ARGS.in_directory and ARGS.out_directory:
sys.exit(cli_main(ARGS))
else:
sys.exit(gui_main(ARGS))