Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added --main-feature and --dry-run/-n flags.

--main-feature flag rips/encodes only the main feature (longest title).

--dry-run/-n disables writing -- useful for verifying that it'll do what
you want.

Now using argparse for parsing arguments, instead of old manual
approach. This means that --help works, too...
  • Loading branch information...
commit ed3d8e5498864e66a4c25abfb29d027b73ae39fd 1 parent f44b15d
Laurence Gonsalves authored

Showing 1 changed file with 49 additions and 9 deletions. Show diff stats Hide diff stats

  1. +49 9 media/dvdrip/dvdrip.py
58 media/dvdrip/dvdrip.py
... ... @@ -1,5 +1,6 @@
1 1 #!/opt/local/bin/python2.7
2 2
  3 +import argparse
3 4 import errno
4 5 import os
5 6 import re
@@ -116,7 +117,7 @@ def ParseNode(scan, pos, indent):
116 117
117 118 TITLE_KEY_RE = re.compile(r'title (\d+)')
118 119
119   -def RipTitle(title_number, title, input, output, title_count):
  120 +def RipTitle(title_number, title, input, output, title_count, dry_run):
120 121 print '=' * 78
121 122 print 'Title %s / %s' % (title_number, title_count)
122 123 print '-' * 78
@@ -147,7 +148,8 @@ def RipTitle(title_number, title, input, output, title_count):
147 148 ]
148 149 print ' '.join(('\n ' + a) if a.startswith('-') else a for a in args)
149 150 print '-' * 78
150   - subprocess.call(args)
  151 + if not dry_run:
  152 + subprocess.call(args)
151 153
152 154 def ParseTitleKey(key):
153 155 return TITLE_KEY_RE.match(key).group(1)
@@ -202,8 +204,31 @@ def Eject(device):
202 204 def Reveal(fnam):
203 205 subprocess.call(['open', '--reveal', fnam])
204 206
205   -if __name__ == '__main__':
206   - _, input, output = sys.argv
  207 +def ParseDuration(s):
  208 + result = 0
  209 + for field in s.strip().split(':'):
  210 + result *= 60
  211 + result += int(field)
  212 + return result
  213 +
  214 +def main():
  215 + global input, output
  216 + parser = argparse.ArgumentParser(description='Rip a DVD.')
  217 + parser.add_argument('-n', '--dry-run',
  218 + action='store_true',
  219 + help="Don't actually write anything.")
  220 + parser.add_argument('--main-feature',
  221 + action='store_true',
  222 + help="Rip only the main feature title.")
  223 + parser.add_argument('input',
  224 + help="Volume to rip (must be a directory).")
  225 + parser.add_argument('output',
  226 + help="""Output location. Extension is added if only one title
  227 + being ripped, otherwise, a directory will be created to contain
  228 + ripped titles.""")
  229 + args = parser.parse_args()
  230 + input = args.input
  231 + output = args.output
207 232
208 233 assert os.path.exists(input), '%r not found' % input
209 234 assert os.path.isdir(input), '%r is not a directory' % input
@@ -212,6 +237,13 @@ def Reveal(fnam):
212 237 print
213 238
214 239 title_count, titles = ScanTitles()
  240 + if args.main_feature and title_count > 1:
  241 + print 'Attempting to determine main feature of %d titles...' % title_count
  242 + main_feature = max(titles,
  243 + key=lambda key_title: ParseDuration(key_title[1]['duration']))
  244 + title_count, titles = 1, [main_feature]
  245 + print 'Selected %r as main feature.' % titles[0][0]
  246 + print
215 247
216 248 if title_count < 1:
217 249 print "No titles to rip!"
@@ -219,11 +251,19 @@ def Reveal(fnam):
219 251 if title_count == 1:
220 252 (key, title), = titles
221 253 output = '%s.mp4' % output
222   - RipTitle(ParseTitleKey(key), title, input, output, title_count)
  254 + RipTitle(ParseTitleKey(key), title, input, output, title_count,
  255 + args.dry_run)
223 256 else:
224   - mkdir_p(output)
  257 + if not args.dry_run:
  258 + mkdir_p(output)
225 259 for key, title in titles:
226   - RipTitle(ParseTitleKey(key), title, input, os.path.join(output, '%s.mp4' % key.capitalize()), title_count)
  260 + RipTitle(ParseTitleKey(key), title, input,
  261 + os.path.join(output, '%s.mp4' % key.capitalize()),
  262 + title_count, args.dry_run)
227 263 print '=' * 78
228   - Reveal(output)
229   - Eject(input)
  264 + if not args.dry_run:
  265 + Reveal(output)
  266 + Eject(input)
  267 +
  268 +if __name__ == '__main__':
  269 + main()

0 comments on commit ed3d8e5

Please sign in to comment.
Something went wrong with that request. Please try again.