-
-
Save bobbigmac/041c99dbf1e918a02cd5ce936a3f2b3c to your computer and use it in GitHub Desktop.
Script to automate ripping DVDs using Handbrake-CLI and mkvmerge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Author: postmodern | |
| # This fork: bobbigmac | |
| # Description: | |
| # Rips a DVD to one H.264 MKV file for each title. | |
| # Ignores any bad blocks or sectors on the DVD. | |
| # Unlike original, does not write tags | |
| # Dependencies: | |
| # * gddrescue | |
| # * handbrake-cli | |
| # * mkvtoolnix | |
| # Original: | |
| # https://gist.github.com/postmodern/482fa8a9d810c40353e2 | |
| set -e | |
| shopt -s extglob | |
| rip_dvd_version=1.0.1 | |
| dvd_device="/dev/dvd" | |
| dvd_blocksize=2048 | |
| dvd_fps=30 | |
| video_codec="x264" | |
| video_quality="20.0" | |
| #audio_codec="vorbis" | |
| audio_codec="aac" | |
| x264_tune="film" | |
| x264_preset="fast" | |
| x264_profile="high" | |
| all_titles="" | |
| all_titles_arr="" | |
| ddrescue_passes=3 | |
| cleanup="true" | |
| handbrake_opts=() | |
| function print_help() | |
| { | |
| cat <<USAGE | |
| usage: rip_dvd [OPTIONS] "TITLE" [-- HANDBRAKE_OPTS] | |
| Options: | |
| -d,--device DEV The dvd device. | |
| Default: $dvd_device | |
| -t,--dvd-title NUM Rips the specific title from the DVD. | |
| --x264-tune TUNE What type of video to tune for. | |
| Default: $x264_tune | |
| --x264-profile PROFILE Encoding profile | |
| Default: $x264_profile | |
| --x264-preset PRESET Encoding preset | |
| Default: $x264_preset | |
| --video-codec Video codec to encode with | |
| Default: $video_codec | |
| --video-quality Video quality | |
| Default: $video_quality | |
| --audio-codec Audio codec to encode with | |
| Default: $audio_codec | |
| -T,--content-type TYPE MKV Content-Type | |
| -K,--keywords WORD,[...] MKV Keywords | |
| --ddrescue-passes 0,1,2,3 The number of ddrescue passes | |
| --[no-]cleanup Delete files after ripping | |
| -V,--version Print the version | |
| -h,--help Print this message | |
| Examples: | |
| rip_dvd "Stargate I" | |
| USAGE | |
| } | |
| function parse_options() | |
| { | |
| while (( $# > 0 )); do | |
| case "$1" in | |
| -d|--device) | |
| dvd_device="$2" | |
| shift 2 | |
| ;; | |
| --video-codec) | |
| video_codec="$2" | |
| shift 2 | |
| ;; | |
| --video-quality) | |
| video_quality="$2" | |
| shift 2 | |
| ;; | |
| --audio-codec) | |
| audio_codec="$2" | |
| shift 2 | |
| ;; | |
| --x264-profile) | |
| x264_profile="$2" | |
| shift 2 | |
| ;; | |
| --x264-tune) | |
| x264_tune="$2" | |
| shift 2 | |
| ;; | |
| --x264-preset) | |
| x264_preset="$2" | |
| shift 2 | |
| ;; | |
| -T|--content-type) | |
| mkv_content_type="$2" | |
| shift 2 | |
| ;; | |
| -K|--keywords) | |
| mkv_keywords="$2" | |
| shift 2 | |
| ;; | |
| --cleanup) | |
| cleanup="true" | |
| shift | |
| ;; | |
| --ddrescue-passes) | |
| ddrescue_passes="$1" | |
| shift 2 | |
| ;; | |
| --no-cleanup) | |
| cleanup="false" | |
| shift | |
| ;; | |
| -V|--version) | |
| echo "rip_dvd $rip_dvd_version" | |
| exit | |
| ;; | |
| -h|--help) | |
| print_help | |
| exit | |
| ;; | |
| --) | |
| shift | |
| handbrake_opts+=($@) | |
| break | |
| ;; | |
| -*) | |
| error "unknown option $1" | |
| exit -1 | |
| ;; | |
| *) | |
| if [[ -n "$title" ]]; then | |
| error "additional argument $1" | |
| exit -1 | |
| fi | |
| title="$1" | |
| shift | |
| ;; | |
| esac | |
| done | |
| if [[ -z "$title" ]]; then | |
| error "no title argument given" | |
| exit -1 | |
| fi | |
| output="${title}/${title}" | |
| working_dir="$title/working" | |
| } | |
| function log() | |
| { | |
| if [[ -t 1 ]]; then | |
| echo -e "\x1b[1m\x1b[32m>>>\x1b[0m \x1b[1m$1\x1b[0m" | |
| else | |
| echo ">>> $1" | |
| fi | |
| } | |
| function warn() | |
| { | |
| if [[ -t 1 ]]; then | |
| echo -e "\x1b[1m\x1b[33m***\x1b[0m \x1b[1m$1\x1b[0m" >&2 | |
| else | |
| echo "*** $1" >&2 | |
| fi | |
| } | |
| function error() | |
| { | |
| if [[ -t 1 ]]; then | |
| echo -e "\x1b[1m\x1b[31m!!!\x1b[0m \x1b[1m$1\x1b[0m" >&2 | |
| else | |
| echo "!!! $1" >&2 | |
| fi | |
| } | |
| function fail() | |
| { | |
| error "$@" | |
| exit -1 | |
| } | |
| function backup_file() | |
| { | |
| local file="$1" | |
| if [[ -f "$file" ]]; then | |
| cp "$file" "${file}.bak" | |
| fi | |
| } | |
| function enter_working_directory() | |
| { | |
| mkdir -p "$working_dir" | |
| pushd "$working_dir" > /dev/null | |
| output="../$output" | |
| } | |
| function copy_dvd() | |
| { | |
| if [[ -f dvd.iso ]]; then | |
| return | |
| fi | |
| ddrescue -b "$dvd_blocksize" -r 0 -n "$dvd_device" dvd.iso.part dvd.log | |
| if (( ddrescue_passes > 1 )); then | |
| ddrescue -b "$dvd_blocksize" -d "$dvd_device" -r 0 dvd.iso.part dvd.log | |
| fi | |
| if (( ddrescue_passes > 2 )); then | |
| ddrescue -b "$dvd_blocksize" -d -R "$dvd_device" -r 0 dvd.iso.part dvd.log | |
| fi | |
| mv dvd.iso.part dvd.iso | |
| } | |
| function load_dvd_info() | |
| { | |
| all_titles="$(lsdvd -q dvd.iso | grep "Audio streams: [0123456789][123456789]" | grep -o "Title: [01234567890][01234567890]" -m 20)" | |
| readarray -t all_titles_arr <<< "$all_titles" | |
| } | |
| function rip_dvd_title() | |
| { | |
| curr_title="$1" | |
| outfile="../$title-$curr_title.mkv" | |
| log "Ripping title $curr_title ... $outfile" | |
| if [[ -f $outfile ]]; then | |
| return | |
| fi | |
| local options=( | |
| --input dvd.iso | |
| --format mkv | |
| --output work.mkv.part | |
| --markers | |
| ) | |
| #--use-opencl | |
| if [[ -n "$curr_title" ]]; then | |
| options+=(--title "$curr_title") | |
| fi | |
| # video options | |
| options+=( | |
| --encoder "$video_codec" | |
| --quality "$video_quality" | |
| --loose-anamorphic | |
| ) | |
| case "$video_codec" in | |
| x264) | |
| options+=( | |
| --x264-preset "$x264_preset" | |
| --x264-profile "$x264_profile" | |
| --x264-tune "$x264_tune" | |
| ) | |
| ;; | |
| esac | |
| # audio options | |
| options+=(--aencoder "$audio_codec") | |
| HandBrakeCLI "${options[@]}" "${handbrake_opts[@]}" | |
| mv work.mkv.part "$outfile" | |
| } | |
| function leave_working_directory() | |
| { | |
| popd >/dev/null | |
| output="${output#../}" | |
| #if [[ "$cleanup" == "true" ]]; then | |
| # rm -r "$working_dir" | |
| #fi | |
| } | |
| parse_options "$@" | |
| enter_working_directory | |
| log "Copying DVD $dvd_device ..." | |
| copy_dvd | |
| load_dvd_info | |
| log "Ripping DVD \"$title\"" | |
| for in_title in "${all_titles_arr[@]}"; do | |
| title_num="${in_title#Title: }" | |
| rip_dvd_title $title_num | |
| warn "Ripped title \"$title_num\"" | |
| done | |
| log "DVD ripped!" | |
| leave_working_directory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment