You are not logged in.

#1 2009-07-25 09:06:05

ezzetabi
Member
Registered: 2006-08-27
Posts: 947

Yet another DVD ripper

Anything welcome.

#!/bin/bash

say() { echo -en "$@" >&2; }
die() { say "$@\n"; exit 1; }
say_help() {
    say "-dvd-device Set the dvd where read from, default $DVD_DEVICE\n"
    say "-title      Set the title to rip. Default $TITLE\n"
    say "-chapter    Set first[,last] chapter to read from. Default $CHAPTER\n"
    say "-alang      Set audio lang to extract, might be repeated\n"
    say "-slang      Set subtitles to extract, might be repeated\n"
    say "-width      Set width of the output movie. Default -1 (original width)\n"
    say "-video_crop Set cropping as -vf crop of mplayer\n"
    say "-temp-dir   Set working dir, must not exist. Default $TMP_DIR\n"
    say "-o          Set output file name: it will be a mkv file. Default $OUTPUT_FILE\n"
    say "-noclean    Do not delete the temp directory after the work\n"
    exit 0
}

DVD_DEVICE="/dev/dvd"
TITLE=1
CHAPTER=1
declare -a ALANG
declare -a SLANG
ORIZONTAL='scale=-1:-10,'
CROP=""
TMP_DIR="$HOME"/tmp_ripping
OUTPUT_FILE="ripped_movie.mkv"
NOCLEAN='false'
START_DIR="$PWD"

if [[ $# -gt 1 ]]; then
    while test -n "$1" ;do
        case "$1" in
            (-dvd-device)
                DVD_DEVICE="$2"
                shift;;
            (-title)
                TITLE="$2"
                shift;;
            (-chapter)
                CHAPTER="$2"
                shift;;
            (-alang)
                ALANG=( "${ALANG[@]}" "$2")
                shift;;
            (-slang)
                SLANG=( "${SLANG[@]}" "$2")
                shift;;
            (-width)
                ORIZONTAL='scale='"$2"':-10,'
                shift;;
            (-video_crop)
                CROP='crop='"$2"','
                shift;;
            (-temp-dir)
                TMP_DIR="$2"
                shift;;
            (-o)
                OUTPUT_FILE="$2"
                shift;;
            (-noclean)
                NOCLEAN='true'
            (-help|-h)
                say_help;;
        esac
        shift
    done
else
    say_help
fi

DVD_DEVICE=`readlink -nf "$DVD_DEVICE"`
say "Input device set to \"$DVD_DEVICE\"\n"

OUTPUT_FILE=`readlink -nf "$OUTPUT_FILE"`
say "Output file set to \"$OUTPUT_FILE\"\n"

TMP_DIR=`readlink -nf "$TMP_DIR"`
say "Temp files directory set to \"$TMP_DIR\"\n"

say "\nExtracting audio: "
for ONELANG in "${ALANG[@]}" ;do
    say "$ONELANG "
done

say "\nExtracting subs: "
for ONESUB in "${SLANG[@]}" ;do
    say "$ONESUB "
done
say "\n\n\n"


say "Starting in 4 seconds...\nPress enter or ctrl-c to abort\n"
read -t 4 && die "ok, nevermind."


touch "$OUTPUT_FILE" || die "Cannot write the output file!"

if [[ ! -d "$TMP_DIR" ]] ;then
    mkdir -p "$TMP_DIR" || die "Cannot create the temp directory!"
else
  die "Sorry, the temporary working directory must not exist."
fi

cd "$TMP_DIR" || die "Cannot go in the temp directory!"

touch "test" || die "Cannot write in the temp directory!"
rm "test" || die "Cannot write in the temp directory!"


nice -19 mencoder -dvd-device "$DVD_DEVICE" dvd://"$TITLE" -chapter "$CHAPTER" -o /dev/null -nosound -ovc x264 \
-x264encopts direct=auto:pass=1:turbo:bitrate=900:bframes=1:\
me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300 \
-vf "$CROP""$ORIZONTAL"harddup  || die "Failed the first passage."

nice -19 mencoder -dvd-device "$DVD_DEVICE" dvd://"$TITLE" -chapter "$CHAPTER" -nosound -ovc x264 \
-x264encopts direct=auto:pass=2:bitrate=900:frameref=5:bframes=1:\
me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300 \
-vf "$CROP""$ORIZONTAL"harddup -o video.avi || die "Failed second passage."

declare -a AUDIO
for ONELANG in "${ALANG[@]}" ;do
    CHANNELS=`mplayer -dvd-device "$DVD_DEVICE" dvd://"$TITLE" -alang "$ONELANG" -chapter "$CHAPTER" \
    -identify -frames 0 2>&1 | sed -n -e '/ID_AUDIO_NCH=[^0]/ s_^.*=__ p'`

    if [[ $CHANNELS -ne 6 ]] ;then
        nice -19 mencoder -dvd-device "$DVD_DEVICE" dvd://"$TITLE" -alang "$ONELANG" -chapter "$CHAPTER" -ovc frameno \
        -oac lavc -lavcopts acodec=vorbis:abitrate=224 -channels 2 -srate 48000 -o "$ONELANG".avi \
        || die "Failed audio extraction of $ONELANG"
    else
        nice -19 mencoder -dvd-device "$DVD_DEVICE" dvd://"$TITLE" -alang "$ONELANG" -chapter "$CHAPTER" -ovc frameno \
        -oac copy -o "$ONELANG".avi \
        || die "Failed audio extraction of $ONELANG"
    fi

    AUDIO=( "${AUDIO[@]}" --language 1:"$ONELANG" -D "$ONELANG".avi )
done

INDEX=0
for ONESUB in "${SLANG[@]}" ;do
    nice -19 mencoder -dvd-device "$DVD_DEVICE" dvd://"$TITLE" -alang "$ONELANG" -chapter "$CHAPTER" \
    -ovc copy -nosound -o /dev/null \
    -vobsubout subtitles -vobsuboutindex "$INDEX" -slang "$ONESUB" \
    || die "Failed subtitle extraction extraction of $ONESUB"

    INDEX=$(($INDEX + 1))
done

if [[ "$INDEX" -ne 0 ]] ;then
    SUBTITLES='subtitles.idx'
else
    SUBTITLES=''
fi

mkvmerge -o "$OUTPUT_FILE" -A video.avi "${AUDIO[@]}" $SUBTITLES

cd "$START_DIR"
if [[ "$NOCLEAN" == "false" ]] ;then
    rm -rf "$TMP_DIR"
fi

Offline

Board footer

Powered by FluxBB