トップ 最新 追記

Masa's blog

検索キーワード:

2016年04月10日 dvd2file.sh [長年日記]

_ dvd2file.sh

#! /bin/sh
if [ "X$1" = "X-h" -o "X$1" = "X--help" ]
then
    echo "usage : $0 [-2] [-t title] [-c chapter] [-s start_time] [-e end_time] [-al audio_lang|-ai audio_id] [-sl subtitle_lang|-si subtitle_id] [-f fps] [-sz size] [-a aspect] [-vc vcodec] [-vb vbitrate] [-ac acodec] [-ab abitrate] [-ar srate] [-o output]"
    echo "  set source(dvd-video) to DVD(DOCK) DRIVE"
    echo ""
    echo "  -2                  : 2-pass encoding(default no)"
    echo "  -t title            : title number(default 1)"
    echo "  -c chapter          : chapter"
    echo "  -s start_time       : start position time (hh:mm:ss)"
    echo "  -e end_time         : pass time (hh:mm:ss)"
    echo "  -al audio_lang      : audio language (default ja,en)"
    echo "  -ai audio_id        : audio id"
    echo "  -sl subtitle_lang   : subtitle language (default none)"
    echo "  -si subtitle_id     : subtitle id"
    echo "  -f fps              : frame per second"
    echo "  -sz size            : WIDTHxHEIGHT[:EX_WIDTHxEX_HEIGHT]"
    echo "  -a aspect           : WIDTH:HEIGHT"
    echo "  -vc vcodec          : video codec(default h264)"
    echo "  -vb vbitrate        : video bitrate(default 800k)"
    echo "  -ac acodec          : audio codec(default aac)"
    echo "  -ab abitrate        : audio bitrate(default 128k)"
    echo "  -ar srate           : audio sampling rate"
    echo "  -o output           : output file(default dvd2file.mp4)"
    exit 0
fi
#
dvddock="/dev/dvd0"
#
two_pass="no"
title="1"
chapter=""
start=""
end=""
alang="-alang ja,en"
aid=""
slang="-slang none"
sid=""
fps=""
size=""
aspect=""
vcodec="h264"
vbitrate="800k"
acodec="aac"
abitrate="128k"
srate=""
output="dvd2file.mp4"
#
while [ $# != 0 ]
do
    case $1 in
    -2 )
        two_pass="yes"
        ;;
    -t )
        shift
        title="$1"
        ;;
    -c )
        shift
        chapter="-chapter $1"
        ;;
    -s )
        shift
        start="-ss $1"
        ;;
    -e )
        shift
        end="-endpos $1"
        ;;
    -al )
        shift
        alang="-alang $1"
	aid=""
        ;;
    -ai )
        shift
        aid="-aid $1"
        alang=""
        ;;
    -sl )
        shift
        slang="-slang $1"
        sid=""
        ;;
    -si )
        shift
        sid="-sid $1"
        slang=""
        ;;
    -f )
        shift
        fps="-r $1"
        ;;
    -sz )
        shift
        size="$1"
        ;;
    -a )
        shift
        aspect="-aspect $1"
        ;;
    -vc )
        shift
        vcodec="$1"
        ;;
    -vb )
        shift
        vbitrate="$1"
        ;;
    -ac )
        shift
        acodec="$1"
        ;;
    -ab )
        shift
        abitrate="$1"
        ;;
    -ar )
        shift
        srate="-ar $1"
        ;;
    -o )
        shift
        output="$1"
        ;;
    * )
        ;;
    esac
    shift
done
#
function func_encode() {
	if [ "X$1" = "X0" ]
	then
		pass_opt=""
		pass_opt2=""
	elif [ "X$1" = "X1" ]
	then
		pass_opt="-pass 1"
		pass_opt2="-an -f rawvideo"
	else
		pass_opt="-pass 2"
		pass_opt2=""
	fi

	if [ "X${size}" = "X" ]
	then
		filter_complex = ""
	else
		x=`echo ${size}|cut -d: -f1|cut -dx -f1`
		y=`echo ${size}|cut -d: -f1|cut -dx -f2`
		ex=`echo ${size}|cut -d: -f2|cut -dx -f1`
		ey=`echo ${size}|cut -d: -f2|cut -dx -f2`
		if [ "X${ex}" = "X" -o "X${ey}" = "X" ]
		then
			filter_complex="-filter_complex scale=${x}:${y}"
		else
			filter_complex="-filter_complex scale=${x}:${y},pad=${ex}:${ey}:(${ex}-${x})/2:(${ey}-${y})/2"
		fi
	fi

	mencoder -really-quiet \
		-dvd-device ${dvddock} \
		dvd://${title} ${chapter} \
		${start} ${end} \
		-oac pcm \
		-ovc copy \
		${alang} ${aid} ${slang} ${sid} \
		-o - |\
	ffmpeg -i - \
		${pass_opt} \
		${fps} \
		${aspect} \
		${filter_complex} \
		-vcodec ${vcodec} \
		-b:v ${vbitrate} \
		-acodec ${acodec} -strict -2 \
		-b:a ${abitrate} \
		${srate} \
		${pass_opt2} -vsync -1 -async 100 -y "$2"
}
#
if [ "X${two_pass}" = "Xyes" ]
then
	func_encode 1 /dev/null
	func_encode 2 ${output}
else
	func_encode 0 ${output}
fi
#eject ${dvddock}
#
exit 0