forked from JasonMillward/Autorippr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautorippr.py
366 lines (269 loc) · 11.2 KB
/
autorippr.py
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# -*- coding: utf-8 -*-
"""
Autorippr
Ripping
Uses MakeMKV to watch for videos inserted into DVD/BD Drives
Automaticly checks for existing directory/video and will NOT overwrite existing
files or folders
Checks minimum length of video to ensure video is ripped not previews or other
junk that happens to be on the DVD
DVD goes in > MakeMKV gets a proper DVD name > MakeMKV Rips
Compressing
An optional additional used to rename and compress videos to an acceptable standard
which still delivers quality audio and video but reduces the file size
dramatically.
Using a nice value of 15 by default, it runs HandBrake (or FFmpeg) as a background task
that allows other critical tasks to complete first.
Extras
Released under the MIT license
Copyright (c) 2014, Jason Millward
@category misc
@version $Id: 1.7.0, 2016-08-22 14:53:29 ACST $;
@author Jason Millward
@license http://opensource.org/licenses/MIT
Usage:
autorippr.py ( --rip | --compress | --extra ) [options]
autorippr.py ( --rip [ --compress ] ) [options]
autorippr.py --all [options]
autorippr.py --test
Options:
-h --help Show this screen.
--version Show version.
--debug Output debug.
--rip Rip disc using makeMKV.
--compress Compress using HandBrake or FFmpeg.
--all Do everything.
--test Tests config and requirements.
--silent Silent mode.
--skip-compress Skip the compression step.
--force_db=(tv|movie) Force use of the TheTVDB or TheMovieDB
"""
import errno
import os
import subprocess
import sys
import yaml
from classes import *
__version__ = "1.7.0"
CONFIG_FILE = "{}/settings.cfg".format(
os.path.dirname(os.path.abspath(__file__)))
notify = None
def eject(config, drive):
"""
Ejects the DVD drive
Not really worth its own class
"""
log = logger.Logger("Eject", config['debug'], config['silent'])
log.debug("Ejecting drive: " + drive)
log.debug("Attempting OS detection")
try:
if sys.platform == 'win32':
log.debug("OS detected as Windows")
import ctypes
ctypes.windll.winmm.mciSendStringW(
"set cdaudio door open", None, drive, None)
elif sys.platform == 'darwin':
log.debug("OS detected as OSX")
p = os.popen("drutil eject " + drive)
while 1:
line = p.readline()
if not line:
break
log.debug(line.strip())
else:
log.debug("OS detected as Unix")
p = os.popen("eject -vm " + drive)
while 1:
line = p.readline()
if not line:
break
log.debug(line.strip())
except Exception as ex:
log.error("Could not detect OS or eject CD tray")
log.ex("An exception of type {} occured.".format(type(ex).__name__))
log.ex("Args: \r\n {}".format(ex.args))
finally:
del log
def rip(config):
"""
Main function for ripping
Does everything
Returns nothing
"""
log = logger.Logger("Rip", config['debug'], config['silent'])
mkv_save_path = config['makemkv']['savePath']
log.debug("Ripping initialised")
mkv_api = makemkv.MakeMKV(config)
log.debug("Checking for DVDs")
dvds = mkv_api.find_disc()
log.debug("{} DVD(s) found".format(len(dvds)))
if len(dvds) > 0:
# Best naming convention ever
for dvd in dvds:
mkv_api.set_title(dvd["discTitle"])
mkv_api.set_index(dvd["discIndex"])
disc_title = mkv_api.get_title()
if not config['force_db']:
disc_type = mkv_api.get_type()
else:
disc_type = config['force_db']
disc_path = os.path.join(mkv_save_path, disc_title)
if not os.path.exists(disc_path):
os.makedirs(disc_path)
mkv_api.get_disc_info()
saveFiles = mkv_api.get_savefiles()
if len(saveFiles) != 0:
filebot = config['filebot']['enable']
for dvdTitle in saveFiles:
dbvideo = database.insert_video(
disc_title,
disc_path,
disc_type,
dvdTitle['index'],
filebot
)
database.insert_history(
dbvideo,
"Video added to database"
)
database.update_video(
dbvideo,
3,
dvdTitle['title']
)
log.debug("Attempting to rip {} from {}".format(
dvdTitle['title'],
disc_title
))
with stopwatch.StopWatch() as t:
database.insert_history(
dbvideo,
"Video submitted to MakeMKV"
)
status = mkv_api.rip_disc(
mkv_save_path, dvdTitle['index'])
# Master_and_Commander_De_l'autre_côté_du_monde_t00.mkv become
# Master_and_Commander_De_l_autre_cote_du_monde_t00.mkv
log.debug('Rename {} to {}'.format(
os.path.join(dbvideo.path, dvdTitle['title']),
os.path.join(dbvideo.path, dvdTitle['rename_title'])
))
os.rename(
os.path.join(dbvideo.path, dvdTitle['title']),
os.path.join(dbvideo.path, dvdTitle['rename_title'])
)
if status:
log.info("It took {} minute(s) to complete the ripping of {} from {}".format(
t.minutes,
dvdTitle['title'],
disc_title
))
database.update_video(dbvideo, 4)
if 'rip' in config['notification']['notify_on_state']:
notify.rip_complete(dbvideo)
else:
database.update_video(dbvideo, 2)
database.insert_history(
dbvideo,
"MakeMKV failed to rip video"
)
notify.rip_fail(dbvideo)
log.info(
"MakeMKV did not did not complete successfully")
log.info("See log for more details")
if config['makemkv']['eject']:
eject(config, dvd['location'])
else:
log.info("No video titles found")
log.info(
"Try decreasing 'minLength' in the config and try again")
else:
log.info("Video folder {} already exists".format(disc_title))
else:
log.info("Could not find any DVDs in drive list")
def skip_compress(config):
"""
Main function for skipping compression
Does everything
Returns nothing
"""
log = logger.Logger("Skip compress", config['debug'], config['silent'])
log.debug("Looking for videos to skip compression")
dbvideos = database.next_video_to_compress()
comp = compression.Compression(config)
for dbvideo in dbvideos:
if comp.check_exists(dbvideo) is not False:
database.update_video(dbvideo, 6)
log.info("Skipping compression for {} from {}" .format(
dbvideo.filename, dbvideo.vidname))
def compress(config):
"""
Main function for compressing
Does everything
Returns nothing
"""
log = logger.Logger("Compress", config['debug'], config['silent'])
comp = compression.Compression(config)
log.debug("Compressing initialised")
log.debug("Looking for videos to compress")
dbvideos = database.next_video_to_compress()
for dbvideo in dbvideos:
dbvideo.filename = utils.strip_accents(dbvideo.filename)
dbvideo.filename = utils.clean_special_chars(dbvideo.filename)
if comp.check_exists(dbvideo) is not False:
database.update_video(dbvideo, 5)
log.info("Compressing {} from {}" .format(
dbvideo.filename, dbvideo.vidname))
with stopwatch.StopWatch() as t:
status = comp.compress(
args=config['compress']['com'],
nice=int(config['compress']['nice']),
dbvideo=dbvideo
)
if status:
log.info("Video was compressed and encoded successfully")
log.info("It took {} minutes to compress {}".format(
t.minutes, dbvideo.filename
)
)
database.insert_history(
dbvideo,
"Compression Completed successfully"
)
database.update_video(dbvideo, 6)
if 'compress' in config['notification']['notify_on_state']:
notify.compress_complete(dbvideo)
comp.cleanup()
else:
database.update_video(dbvideo, 5)
database.insert_history(dbvideo, "Compression failed", 4)
notify.compress_fail(dbvideo)
log.info("Compression did not complete successfully")
else:
database.update_video(dbvideo, 2)
database.insert_history(
dbvideo, "Input file no longer exists", 4
)
else:
log.info("Queue does not exist or is empty")
if __name__ == '__main__':
arguments = docopt.docopt(__doc__, version=__version__)
config = yaml.safe_load(open(CONFIG_FILE))
config['debug'] = arguments['--debug']
config['silent'] = arguments['--silent']
if arguments['--force_db'] not in ['tv','movie', None]:
raise ValueError('{} is not a valid DB.'.format(arguments['--force_db']))
else:
config['force_db'] = arguments['--force_db']
notify = notification.Notification(
config, config['debug'], config['silent'])
if bool(config['analytics']['enable']):
analytics.ping(__version__)
if arguments['--test']:
testing.perform_testing(config)
if arguments['--rip'] or arguments['--all']:
rip(config)
if (arguments['--compress'] or arguments['--all']) and not arguments['--skip-compress']:
compress(config)
if arguments['--skip-compress']:
skip_compress(config)