-
Notifications
You must be signed in to change notification settings - Fork 60
/
autorippr.py
467 lines (344 loc) · 14.5 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# -*- 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.
--extra Lookup, rename and/or download extras.
--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 *
from tendo import singleton
__version__ = "1.7.0"
me = singleton.SingleInstance()
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")
def extras(config):
"""
Main function for filebotting and flagging forced subs
Does everything
Returns nothing
"""
log = logger.Logger("Extras", config['debug'], config['silent'])
fb = filebot.FileBot(config['debug'], config['silent'])
dbvideos = database.next_video_to_filebot()
for dbvideo in dbvideos:
if config['ForcedSubs']['enable']:
forced = mediainfo.ForcedSubs(config)
log.info("Attempting to discover foreign subtitle for {}.".format(dbvideo.vidname))
track = forced.discover_forcedsubs(dbvideo)
if track is not None:
log.info("Found foreign subtitle for {}: track {}".format(dbvideo.vidname, track))
log.debug("Attempting to flag track for {}: track {}".format(dbvideo.vidname, track))
flagged = forced.flag_forced(dbvideo, track)
if flagged:
log.info("Flagging success.")
else:
log.debug("Flag failed")
else:
log.debug("Did not find foreign subtitle for {}.".format(dbvideo.vidname))
log.info("Attempting video rename")
database.update_video(dbvideo, 7)
movePath = dbvideo.path
if config['filebot']['move']:
if dbvideo.vidtype == "tv":
movePath = config['filebot']['tvPath']
else:
movePath = config['filebot']['moviePath']
status = fb.rename(dbvideo, movePath)
if status[0]:
log.info("Rename success")
database.update_video(dbvideo, 6)
if config['filebot']['subtitles']:
log.info("Grabbing subtitles")
status = fb.get_subtitles(
dbvideo, config['filebot']['language'])
if status:
log.info("Subtitles downloaded")
database.update_video(dbvideo, 8)
else:
log.info("Subtitles not downloaded, no match")
database.update_video(dbvideo, 8)
log.info("Completed work on {}".format(dbvideo.vidname))
if config['commands'] is not None and len(config['commands']) > 0:
for com in config['commands']:
subprocess.Popen(
[com],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True
)
else:
log.info("Not grabbing subtitles")
database.update_video(dbvideo, 8)
if 'extra' in config['notification']['notify_on_state']:
notify.extra_complete(dbvideo)
log.debug("Attempting to delete %s" % dbvideo.path)
try:
os.rmdir(dbvideo.path)
except OSError as ex:
if ex.errno == errno.ENOTEMPTY:
log.debug("Directory not empty")
else:
log.info("Rename failed")
else:
log.info("No videos ready for filebot")
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)
if arguments['--extra'] or arguments['--all']:
extras(config)