forked from mdmonk/python_snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripdvd.py
134 lines (114 loc) · 3.23 KB
/
ripdvd.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
#!/usr/bin/python
import os
import sys
import re
from subprocess import Popen
import time
HANDBRAKE = '/usr/local/bin/HandBrakeCLI'
def usage():
print 'rip.py [dvd_name output_dir]'
def get_dvd_file():
vols = os.listdir('/Volumes')
candidates = list()
for vol in vols:
if vol.startswith('.'):
continue
candidates.append(vol)
if len(candidates) > 0:
for vol in candidates:
dirs = os.listdir('/Volumes/' + vol)
if 'VIDEO_TS' in dirs:
return vol
else:
sys.exit()
def prettify_filename(filename):
"""
LITTLE_MISS_SUNSHINE => Little Miss Sunshine
"""
pretty = ''
l = None
for i in xrange(len(filename)):
c = filename[i]
if not l or l == '_':
pretty += c.upper()
else:
pretty += c.lower()
l = c
return pretty
start = time.time()
dvd_file = None
out_dir = None
#really ugly comand line parsing, should you opt
if len(sys.argv) > 3:
print sys.argv
usage()
sys.exit()
if len(sys.argv) == 3:
dvd_file, out_dir = sys.argv[1:]
elif len(sys.argv) == 2:
dvd_file = get_dvd_file()
out_dir = sys.argv[1]
else:
dvd_file = get_dvd_file()
out_dir = '.'
if out_dir.endswith('/'):
out_dir = out_dir[:-1]
#input and output options for handbrake
infile = '/Volumes/%s' % (dvd_file)
dvd_file = prettify_filename(dvd_file)
outfile = '%s/%s.m4v' % (out_dir, dvd_file)
#lets verify the paths
if not os.path.exists(infile) or not os.path.exists(out_dir):
print 'bad paths: input file (%s) output directory (%s)' % (infile, out_dir)
sys.exit()
#scan the dvd for the titles
tmp = os.tmpfile()
po = Popen((HANDBRAKE, '-i%s' % (infile), '-t0'), stderr=tmp)
while po.poll() == None:
time.sleep(1)
sys.stdout.write('.')
sys.stdout.flush()
tmp.seek(0)
sys.stdout.write('n')
#read the output from the scan to get all the titles and their length
title = re.compile('title (d+):')
duration = re.compile('duration: (d+):(d+):(d+)')
chapters = list()
t = d = None
for line in tmp.readlines():
tm = title.search(line)
dm = duration.search(line)
if tm != None:
t = tm.groups()[0]
elif dm != None:
d = dm.groups()
d = [ int(x) for x in d ]
h, m, s = d
secs = h * 3600 + m * 60 + s
chapters.append((t, secs))
t = d = None
#determine the longest title
max_secs = title = 0
for chapter in chapters:
if chapter[1] > max_secs:
title = chapter[0]
max_secs = chapter[1]
print 'ripping: chapter %s (%ds)' % (title, max_secs)
#start the ripping process, redirect stderr to tmp
tmp = os.tmpfile()
po = Popen((HANDBRAKE, '-i%s' % (infile), '-t%s' % (title), '-o%s' % (outfile),
'-effmpeg', '-m', '-b2048', '-p', #video options
'-B256', '-R48', '-66ch') , stderr=tmp) #audio options
while po.poll() == None:
time.sleep(60)
sys.stdout.write('.')
sys.stdout.flush()
sys.stdout.write('n')
#get total time
secs = time.time() - start
hrs = int(secs) / 3600
min = int(secs) % 3600 / 60
sec = int(secs) % 60
print 'you totally ripped! (%d:%d:%d total time)' % (hrs, min, sec)
# notify via growl
os.popen('growlnotify -m "Completed encoding %s" -p 2' % (dvd_file))