Skip to content

Instantly share code, notes, and snippets.

@mhl
Created August 31, 2016 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhl/db117d403fc984bc5f02533e9c3fb316 to your computer and use it in GitHub Desktop.
Save mhl/db117d403fc984bc5f02533e9c3fb316 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from datetime import datetime
import os
import re
from subprocess import call, check_call, check_output
OUTPUT_DIRECTORY = "/home/mark/Videos"
SOURCE_DRIVE = "/dev/dvd"
# HANDBRAKE_PRESET = "Android Tablet MP3"
HANDBRAKE_PRESET = "Android 720p30 MP3"
EXTENSION = "mkv"
# Play a sound just to indicate that this script has started running:
call(['paplay', '/usr/share/sounds/ubuntu/stereo/dialog-information.ogg'])
# Find the DVD title:
dvd_title = check_output(
[
'blkid',
'-o',
'value',
'-s',
'LABEL',
SOURCE_DRIVE
]
)
dvd_title = re.sub(r' ', '-', dvd_title.strip())
print "Ripping:", dvd_title
timestamp = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
leaf_name = dvd_title + '_' + timestamp
output_directory = os.path.join(OUTPUT_DIRECTORY, leaf_name)
os.makedirs(output_directory)
lsdvd_re = re.compile(
r'^Title: 0*(\d+), Length: (\d+):(\d+):(\d+)\.'
)
# Now try to find all the titles on the DVD and rip each one:
for l in check_output(['lsdvd', SOURCE_DRIVE]).splitlines():
m = lsdvd_re.search(l)
if not m:
continue
title_number = int(m.group(1))
title_length_seconds = \
int(m.group(2), 10) * 60 * 60 + \
int(m.group(3), 10) * 60 + \
int(m.group(4), 10)
# Don't rip anything really short:
if title_length_seconds < 120:
continue
print "Ripping title {0} of length {1} seconds".format(
title_number, title_length_seconds
)
output_filename = os.path.join(
output_directory, 'title-{0:02d}.{1}'.format(
title_number, EXTENSION
)
)
check_call([
# 'HandBrakeCLI',
'/home/mark/hb/build/HandBrakeCLI',
'-v2',
'-N', 'eng',
# Only change for later DS9 videos, where German is track 1
'-a', '1',
'-i', SOURCE_DRIVE,
'-t', str(title_number),
'-o', output_filename,
'--preset-import-gui',
'--preset=' + HANDBRAKE_PRESET,
'-s', 'scan', '--subtitle-forced', '--subtitle-default'
])
check_call(['eject'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment