#!/bin/sh # Re-encode movie files, using mencoder # $Id$ # (c)2003 by HIROSE Yuuji [yuuji@gentei.org] myname=`basename $0` SCALE="" vcodec=mpeg4 oac=mp3lame outdir=. channels=2 gamma=1.2 ext=avi verbose='' seconds='' ofps='' vb='' force='' newX='' newY='' # if echo a | tr -u a a >/dev/null 2>&1; then # TR='tr -u' # else # TR='tr' # fi finalize () { # rm -f $anntmp > /dev/null 2>&1 echo Terminated. 1>&2 exit 0 } usage() { cat<<EOF $myname read multiple MOVIE files (perhaps in another directory) and re-encode them into current directory(by default). For example, % $myname /cf/dcim/104sands/vclp/*.mov re-encode all *.mov files. Usage of $myname; $myname [Options] MovieFiles... Possible options are as follows(Value in parens is default value) -s SCALE SCALE ratio of generated movie files '-s 0.5' generates 0.5 of original geometry -x WIDTH Width of generated movie -y HEIGHT Height of generated movie -g GAMMA GAMMA correction value (1.2) -ac CODEC One of mp3lame, pcm, raw -vc CODEC One of mpeg4, msmpeg4, mjpeg, rv10 mpg1video -ch CHANNEL Number of channels stereo=2 monoral=1 (none) -mz for Sanyo MZ-3, same as '-oa pcm -ch 1' -sl for Sanyo MZ-3, same as '-vb 2000' -o DIR Output directory(current directory) -ss SEC-POS Seek to SEC-POS seconds position -end SECS Time to encode -fps FPS Output fps -vb BITRATE Bitrate of Video in kHz) -f Force overwriting $myname requires mencoder, netpbm, and jpeglib(djpeg) Type '$myname' for Japanese help. EOF exit 0 } while [ $# -ne 0 ]; do case "$1" in -s) shift; SCALE=`echo $1|tr x :` ;; -g) shift; gamma="$1" ;; -ac*) shift; oac=$1 ;; -vc*) shift; vcodec=$1 ;; -ext) shift; ext=$1 ;; -ch*) shift; channels="$1" ;; -mz) oac=pcm channels=1 ;; -sl) vb=2000 ;; -o) shift; outdir="$1" ;; -ss) shift; start="$1" ;; -end*) shift; end="$1" ;; -fps) shift; ofps="$1" ;; -vb) shift; vb="$1" ;; -x) shift; newX="$1" newY='' ;; -y) shift; newY="$1" newX='' ;; -f) force=1 ;; -h) usage ;; *) if [ -s "$1" ]; then files="$files $1" fi ;; esac shift done if [ "$files" = "" ]; then cat<<_EOU_ なんかファイルを指定してね。 $myname は(おそらく別のディレクトリにある)たくさんのMOVIEファイルを 読み込んで、Web向けのちいさいサイズに一括してエンコードしなおしてくれま。 これからWebページを書こうってディレクトリに移動してから % $myname /cf/dcim/104sands/vclp/*.mov なんて風にやるよろし。オプションには以下のようなものがあるでよ。 $myname の使い方: $myname [オプション] 動画ファイルたち オプションは以下の通り(括弧内はデフォルト値) -s 倍率 新しい動画ファイルの大きさの倍率指定 -s 0.5 とすると元の0.5倍、 -x 幅 新しい動画ファイルの幅 [高さ指定省略可能] -y 高さ 新しい動画ファイルの高さ [幅指定省略可能] -g γ補正値 γ補正での明るさを指定(1.2) -ac CODEC mp3lame, pcm, raw のどれか -vc CODEC mpeg4, msmpeg4, mjpeg, rv10 mpg1video のどれか -ch 数 チャンネル数指定 ステレオ=2 モノラル=1 (無指定) -mz Sanyo MZ-3画像の場合は指定する -oa pcm -ch 1 と同じ -sl Kyocera SL400R の動画だと -vb 2000 かなーと思って -o DIR ファイルを吐きだすディレクトリ(カレント) -ss 秒位置 エンコードし始める秒数 -end 秒数 エンコードする秒数 -fps FPS 生成動画の フレーム/秒 -vb Vレート 生成動画の画像サンプリングレートkHz -f 生成ファイルをどんどん上書き (上書きしない) $myname には mencoder と netpbm と jpeglib(djpeg)が必要です。 Unix上で画像処理をするには欠かせないツールなので絶対にインストールしましょう。 Type "$myname -h" for English help. _EOU_ exit 0; fi newname() { # $1=base $2=ext base="$1" if [ ! -f "$base.$ext" -o "$force" ]; then echo "$base.$ext" return fi i=0 while [ -f "$base-$i.$ext" ]; do i=`expr $i + 1` done echo "$base-$i.$ext" } checksize() { # $1=movie-file # Referring global variable, $SCALE, $newX and $newY if [ -z "$newX" -a -z "$newY" ]; then case $SCALE in *:*) echo "-vop scale=$SCALE" return ;; [.0-9]*) ;; ''|*) echo "Specify size as XxY or 0.75 (eg.)." 1>&2 echo "サイズ指定は XxY とか 0.75 のように書くべし" 1>&2 exit 2 ;; esac fi tmp1=00000001.jpg tmp2=00000002.jpg jpginfo=`(cd "$outdir" mplayer -frames 1 -vo jpeg "$1" rdjpgcom -v $tmp1 rm -f $tmp1 $tmp2)` width=`expr "$jpginfo" : '.* \([0-9]*\)w.*'` height=`expr "$jpginfo" : '.* \([0-9]*\)h.*'` if [ "$newX" ]; then newwidth="$newX" newheight=`expr $height \* $newX / $width` elif [ "$newY" ]; then newwidth=`expr $width \* $newY / $height` newheight="$newY" else newwidth=`echo $width \* $SCALE | bc | sed 's/\..*//'` newheight=`echo $height \* $SCALE | bc | sed 's/\..*//'` fi echo "-vop scale=${newwidth}:${newheight}" } reconv() { #dir=`dirname "$1"` base=`basename "$1"` root="$outdir"/`expr "$base" : '\(.*\)\..*'` new=`newname $root $ext` [ $verbose ] && echo "Set outfile to $new" if [ -z "$newX" -a -z "$newY" -a -z "$SCALE" ]; then vop='-vop ' else vop=`checksize "$1"` fi vop="$vop,eq2=$gamma" [ $quiet ] || echo "$1 -> $new $vop" mencoder -ovc lavc -lavcopts vcodec=$vcodec -oac $oac $vop $opts \ -channels $channels -o "$new" "$1" 2> /dev/null touch -r "$1" $new # | nkf -u | perl -e '$|=1; select((select(0), $|=1)[0]); # while (<>) {s/\n/\r/; print}' } opts='' [ "$start" ] && opts="$opts -ss $start" [ "$end" ] && opts="$opts -endpos $end" [ "$ofps" ] && opts="$opts -ofps $ofps" [ "$vb" ] && vcodec="${vcodec}:vbitrate=$vb" for f in $files; do reconv "$f" done