トップ «前の日記(2012年07月20日) 最新 次の日記(2012年08月04日)» 編集

Masa's blog

検索キーワード:

2012年07月31日 Encoding script for Panasonic Viera with miniDLNA [長年日記]

_ This is a encoding script that make miniDLNA can provide movie files for Panasonic Viera as DLNA client.

_ convert2dlna_mpegts.sh

This script convert any movie into mpegts(H.264, AC-3).

#! /bin/sh
#
if [ "X$1" = "X-h" -o "X$1" = "X--help" ]
then
    echo "Usage : $0 -ix input_x_resolution -iy input_y_resolution [-2] [-s start_time] [ -e elaps_time] [-iax input_aspect_x] [-iay input_aspect_y] [-oax output_aspect_x] [-oay output_aspect_y] [-ab audio_bitrate] [-vb video_bitrate] [-fps frame_per_second] [-o output] inputs [-h|--help] "
    echo ""
    echo "  -ix input_x_resolution : input x resolution"
    echo "  -iy input_y_resolution : input y resolution"
    echo "  -2                     : 2-pass encoding(default none)"
    echo "  -s start_time          : start position time"
    echo "  -e elaps_time          : elaps(pass) time"
    echo "  -iax input_aspect_x    : input x aspect (default ix)"
    echo "  -iay input_aspect_y    : input y aspect (default iy)"
    echo "  -oax output_aspect_x   : output x aspect (default 16)"
    echo "  -oay output_aspect_y   : output y aspect (default 9)"
    echo "  -ab audio_bitrate      : audio bitrate(Kbps) (default 600)"
    echo "  -vb video_bitrate      : video bitrate(Kbps) (default 128)"
    echo "  -fps frame_per_second  : frame per second (default 30000/1001)"
    echo "  -o output              : output file (default convert2dlna.m2t)"
    echo "  inputs                 : input file names separated by space"
    exit 0
fi
#
ix=""; export ix
iy=""; export iy
pass="1"
start=""
elaps=""
iax=""; export iax
iay=""; export iay
oax=16; export oax
oay=9; export oay
ab=128
vb=600
fps="30000/1001"
output="convert2dlna.m2t"
#
while [ $# != 0 ]
do
    case $1 in
    -ix )
        shift
        ix="$1"
        ;;
    -iy )
        shift
        iy="$1"
        ;;
    -2 )
        pass="2"
        ;;
    -s )
        shift
        start="-ss $1"
        ;;
    -e )
        shift
        elaps="-endpos $1"
        ;;
    -iax )
        shift
        iax="$1"
        ;;
    -iay )
        shift
        iay="$1"
        ;;
    -oax )
        shift
        oax="$1"
        ;;
    -oay )
        shift
        oay="$1"
        ;;
    -ab )
        shift
        ab="$1"
        ;;
    -vb )
        shift
        vb="$1"
        ;;
    -fps )
        shift
        fps="$1"
        ;;
    -o )
        shift
        output="$1"
        ;;
    * )
        input="${input} $1"
        ;;
    esac
    shift
done
#
if [ "X${ix}" = "X" ]
then
	echo "-ix(input x resolution) is not specified."
	exit 1
fi
#
if [ "X${iy}" = "X" ]
then
	echo "-iy(input y resolution) is not specified."
	exit 1
fi
#
if [ "X${iax}" = "X" ]
then
	iax=${ix}
fi
#
if [ "X${iay}" = "X" ]
then
	iay=${iy}
fi
#
sx_sy_ex_ey=`cat /dev/null |\
awk 'BEGIN{
	ix = ENVIRON["ix"];
	iy = ENVIRON["iy"];
	iax = ENVIRON["iax"];
	iay = ENVIRON["iay"];
	oax = ENVIRON["oax"];
	oay = ENVIRON["oay"];

	if ((ix / iy) == (iax / iay)){
		ilx = ix;
		ily = iy;
	}else if ((ix / iy) < (iax / iay)){
		ily = iy;
		ilx = iy * iax / iay;
	}else{
		ilx = ix;
		ily = ix * iay / iax;
	}

	scalex = ilx;
	scaley = ily;
	if ((ilx / ily) == (oax / oay)){
		expandx = ilx;
		expandy = ily;
	}else if ((ilx / ily) < (oax / oay)){
		expandy = ily;
		expandx = ily * oax / oay;
	}else{
		expandx = ilx;
		expandy = ilx * oay / oax;
	}

	printf("%d %d %d %d\n", scalex, scaley, expandx, expandy);
}'`
#
sx=`echo ${sx_sy_ex_ey}|cut -d ' ' -f1`
sy=`echo ${sx_sy_ex_ey}|cut -d ' ' -f2`
ex=`echo ${sx_sy_ex_ey}|cut -d ' ' -f3`
ey=`echo ${sx_sy_ex_ey}|cut -d ' ' -f4`
#
function menc() {
        pass=$1
        filename=$2

        if [ "X${pass}" = "X0" ]
        then
                vp=""
        else
                vp="pass=${pass}:"
        fi

	mencoder \
		${start} ${elaps} \
		-of lavf -lavfopts format=mpegts \
		-ofps ${fps} \
		-oac lavc -lavcopts acodec=ac3:abitrate=${ab}:aspect=${oax}/${oay} \
		-ovc x264 -x264encopts ${vp}bitrate=${vb} \
		-vf scale=${sx}:${sy},expand=${ex}:${ey},harddup \
		-o ${filename} \
		${input}
}
#
if [ "X${pass}" = "X1" ]
then
        menc 0 ${output}
else
        menc 1 /dev/null
        menc 2 ${output}
fi

_ ffmpeg-1.1.1

I built ffmpeg-1.1.1 with following configuration.

./configure --enable-shared \
	--enable-libopencore-amrnb --enable-version3 \
	--enable-libopencore-amrwb \
	--enable-libmp3lame \
	--enable-libfaac --enable-nonfree \
	--enable-libx264 --enable-gpl

_ convert2dlna_mp4.sh

This script convert any movie into mp4(H.264, AAC).

#! /bin/sh
#
if [ "X$1" = "X-h" -o "X$1" = "X--help" ]
then
    echo "Usage : $0 [-2] [-s start_time] [ -e elaps_time] [-sz size] [-a AX:AY] [-ab audio_bitrate] [-vb video_bitrate] [-fps frame_per_second] [-o output] inputs [-h|--help] "
    echo ""
    echo "  -2                     : 2-pass encoding(default none)"
    echo "  -s start_time          : start position time = hh:mm:ss(default none)"
    echo "  -e elaps_time          : elaps(pass) time second(default none)"
    echo "  -sz size               : size for output = XSIZExYSIZE(default same as input)"
    echo "  -a aspect              : aspect ratio for output = X:Y(default same as input)"
    echo "  -ab audio_bitrate      : audio bitrate(Kbps) (default 128)"
    echo "  -vb video_bitrate      : video bitrate(Kbps) (default 600)"
    echo "  -fps frame_per_second  : frame per second (default 30000/1001)"
    echo "  -o output              : output file (default convert2dlna.mp4)"
    echo "  input                  : input file name"
    exit 0
fi
#
pass="1"
start=""
elaps=""
size=""
aspect=""
ab=128
vb=600
fps="30000/1001"
output="convert2dlna.mp4"
#
while [ $# != 0 ]
do
    case $1 in
    -2 )
        pass="2"
        ;;
    -s )
        shift
        start="-ss $1"
        ;;
    -e )
        shift
        elaps="-t $1"
        ;;
    -sz )
        shift
        size="-s $1"
        ;;
    -a )
        shift
        aspect="-aspect $1"
        ;;
    -ab )
        shift
        ab="$1"
        ;;
    -vb )
        shift
        vb="$1"
        ;;
    -fps )
        shift
        fps="$1"
        ;;
    -o )
        shift
        output="$1"
        ;;
    * )
        input="$1"
        ;;
    esac
    shift
done
#
function menc() {
        pass=$1
        filename=$2

        if [ "X${pass}" = "X0" ]
        then
                vp=""
        else
                vp="-pass ${pass}"
        fi

	ffmpeg \
		-y \
		${start} ${elaps} \
	       	-i ${input} \
		-f mp4 \
		-vcodec h264 -b ${vb}k ${aspect} ${size} ${vp} \
		-acodec aac -ab ${ab}k -strict -2 -absf aac_adtstoasc \
		-r ${fps} \
		${filename}
}
#
if [ "X${pass}" = "X1" ]
then
        menc 0 ${output}
else
        menc 1 /dev/null
        menc 2 ${output}
fi