2011-04-24

Скрипт перекодирования всех видео файлов в текущей папке в формат Android 'а

Скрипт перекодирования всех файлов в текущей папке в формат Android'а и бытового DVD player'а.
имеет опцию(--screencast) для скринкастов(сохранение качества, по возможности), использую для railscasts.com, и поцию(--movie-low) для больших документальных фильмов.
(параметр dsize=480:320 можно заменить на ваше разрешение, mencoder оптимально отмасштабирует поб это разрешение с сохранением пропорций)


Если кто-то знает "способ лучше", комментарии приветствуются.

____________________________________________________________
#! /bin/bash
echo ;echo ;echo ;echo ;echo ;

#     echo "Parameters are '$*'"
#     until [ -z "$1" ]
#     do
#       echo "Processing parameter of: '$1'"
#       if [ ${1:0:1} = '--' ]
#       then
#           tmp=${1:1}               # Strip off leading '/' . . .
#           parameter=${tmp%%=*}     # Extract name.
#           value=${tmp##*=}         # Extract value.
#           echo "Parameter: '$parameter', value: '$value'"
#           #eval $parameter=$value
#       fi
#       shift
#     done


E_OPTERR=65

if [ "$#" -eq 0 ]
then   # Script needs at least one command-line argument.
  echo "Usage: $0 --movie-low|--screencast"  # [--force-ffmpeg-precoding] --movie-hi|--movie-mid|--cartoon
  echo "ffmpeg-precoding forced for .MOV files"
  echo ;echo ;echo ;echo ;echo ;
  exit $E_OPTERR
fi  

while [ ! -z "$1" ]
do
  case "$1" in





    --movie-low) 
      echo "Movie \"Low\" Quality profile"   #$2
fps=15
fps_ff="-r "$fps
fps_mc="-fps "$fps
#fps_ff=""
#fps_mc=""

mp3bitrate=24
mp3opts_mc="-srate 24000 -oac mp3lame -lameopts cbr:br=$mp3bitrate"
mp3opts_ff=$mp3bitrate"k"

vf="dsize=480:320:0,scale=-8:-8,harddup"
name_postfix="mc-q5.25-vbr$mp3bitrate-fps$fps"
xvidopts="fixed_quant=5.25:vhq=4:bvhq=1:chroma_opt=1:quant_type=h263:trellis=1:lumi_mask=1:hq_ac=1"
#:bitrate=-1400 #1400mb   #:cartoon
scale=7
# 7 - gauss
      echo;;





    --screencast)
      echo "Screencast profile"  #$2
fps=5
fps_ff="-r "$fps
fps_mc="-fps "$fps
#fps_ff=""
#fps_mc=""

mp3bitrate=96
mp3opts_mc="-oac mp3lame -lameopts cbr:br=$mp3bitrate"
mp3opts_ff=$mp3bitrate"k"

vf="eq=0:3,dsize=480:320:0,scale=-8:-8,eq=0:3,harddup"
name_postfix="mc-q1-vbr$mp3bitrate-fps$fps"
xvidopts="fixed_quant=1:vhq=4:bvhq=1:chroma_opt:quant_type=h263:trellis:lumi_mask:hq_ac"
#:bitrate=-1400 #1400mb   #:cartoon
scale=9
# 9 - lanczos
      echo;;





#     --force-ffmpeg-precoding)
#       echo "FFMpeg precoding forced"
#       echo;;
     *) 
      echo "Wrong argument, usage: $0 --movie-low|--screencast"  # [--force-ffmpeg-precoding]--movie-hi|--movie-mid|--cartoon
      echo "ffmpeg-precoding forced for .MOV files"
      echo;echo ;echo ;echo ;echo ;
      exit $E_OPTERR
      break;;
  esac

  shift
done





out_folder_name=Android_DVD_out

mkdir $out_folder_name
#ls --format=single-column

for in_file in *; do
    #echo ;echo --- "infile" --------------------- $in_file --------------------------------------

    for in_ext in '.mov' '.mp4' '.avi' '.flv'; do #for arg in "$file" "$iname"; do
      #echo ;echo ------ "in_ext " -------------- "${in_ext}" -------------------------

      in_file_lowcase=`echo $in_file | tr A-Z a-z`
      #echo -- "${in_file_lowcase:(-4)}" == "$in_ext"
 if [ "${in_file_lowcase:(-4)}" == "$in_ext" ]
 then
   echo --- IN -------------------- "${in_file}" ------------------------- 119

   in_file_wo_ext=${in_file/$in_ext/}
   #file_wo_ext=${file/abc/xyz}    # xyzABC123ABCabc # Replaces first match of 'abc' with 'xyz'
   #file_wo_ext=`echo "$file" | sed 's/\.\w*$//'`
   ##orig:  i_wo_ext=`echo "$i" | sed 's/\.\w*$/.avi/'`    
   #echo DEBUG: "${in_file_wo_ext}"

   out_file="./$out_folder_name/$in_file_wo_ext-$name_postfix.avi"
   echo --- OUT -------------------- "${out_file}" ------------------------- 128


   curr_file=$in_file
   if [ "${in_file_lowcase:(-4)}" == '.mov' ]
   then

     echo ">>> INFO: .MOV file, need FFMPEG preencoding";
     ff_out_file=./$out_folder_name/$in_file_wo_ext-FFMPEG.avi
     echo ">>> OUT-FF: $ff_out_file"
     rm "$ff_out_file"
     
            echo ;echo ;echo ;echo ;
     ffmpeg -i "./$in_file" $fps_ff -f avi -vcodec mpeg4 -sameq -b 2100000 -g 1 -bf 2 -acodec libmp3lame -ab $mp3bitrate "$ff_out_file"
     curr_file=$ff_out_file
   fi

   echo ; echo ; echo "===================== IN-MENCODER ${curr_file} ===================================== 145";
   mencoder "${curr_file}" -idx $fps_mc -sws $scale -vf $vf -o "${out_file}" -ovc xvid -xvidencopts $xvidopts $mp3opts_mc
### OLD   mencoder "$ff_file"                   -idx $fps_mc -sws $scale -vf $vf -o $i_out        -ovc xvid -xvidencopts $xvidopts -nosub -oac copy
   rm "$ff_out_file"





 fi
    done
done





echo ;echo ;echo ;echo ;echo ;

  #mencoder -ovc help
  #mencoder -oac help
  #mencoder -of help

  #mplayer $i -identify -frames 0
  # -msglevel identify=4
  # -identify=6
  #mplayer -ac help 
  #mplayer -vc help



















#mencoder [in_file] -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1000:vhq:keyint=250:threads= 2 -oac mp3lame -lameopts cbr:br=128 -ffourcc XVID -vf scale=320:-2,crop=320:240,expand=320:240 -af resample=44100:0:0 -o [out_file]

#mencoder $i -of lavf -ovc lavc -lavfopts format=mov -lavcopts aglobal=1:vglobal=1:vcodec=h264:acodec=mp3:vbitrate=512 -oac mp3lame -lameopts br=96:cbr -mc 0 -o "./OUT/$i noFF.mov"

#mencoder "./$i" -vf dsize=480:320:2,scale=-8:-8,harddup -idx -o "./$i.avi" -ovc xvid -xvidencopts turbo=1:vhq=4:bvhq=1:chroma_opt:quant_type=h263:bitrate=512:trellis -sws 2 -nosub -alang ru -oac mp3lame -lameopts br=96:cbr

#DV_Player# ffmpeg -i ./$i -f avi -s 720x544 -vcodec mpeg4 -b 1500k -g 300 -bf 2 -acodec libmp3lame -ab 128k ./$i.avi
#OGG#       ffmpeg -i ./$i -vcodec libtheora -sameq -acodec libvorbis -ab 128k ./$i.avi

# SAMPLE 1
# 
# cat /mnt/datos/kpax/vob/video*.vob | nice -+19 mencoder -sws 9 -vop scale=544:256:5,crop=650:435:35:75 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=850:vhq:v4mv:vpass=1:vqmin=2:vqmax=31:vmax_b_frames=2 -o /dev/null -
# 
# cat /mnt/datos/kpax/vob/video*.vob | nice -+19 mencoder -sws 9 -vop scale=544:256:5,crop=650:435:35:75 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=850:vhq:v4mv:vpass=2:vqmin=2:vqmax=31:vmax_b_frames=2 -o K-Pax.avi -


# SAMPLE 2
# 
# cat vts_*.vob | mencoder -nosound -sws 9 -vop scale=480:384:5,crop=698:568:10:4,lavcdeint -ovc lavc -lavcopts vcodec=mpeg4:vqscale=2:vhq:v4mv:vmax_b_frames=1:precmp=2:cmp=2:subcmp=2:vb_qoffset=0.6:qpel:trell:vpass=1 -o /dev/null -
# 
# cat vts_*.vob | mencoder -nosound -sws 9 -vop scale=480:384:5,crop=698:568:10:4,lavcdeint -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1600:vhq:v4mv:vmax_b_frames=1:precmp=2:cmp=2:subcmp=2:vb_qoffset=0.6:qpel:trell:vpass=2 -o beethoven.avi -



# -aid 128k
#mencoder -idx ./$i -o ./$i.avi -oac mp3lame -lameopts cbr:br=128 -ovc xvid -xvidencopts fixed_quant=2 quant_type=mpeg
#mencoder -idx ./$i -o ./$i.avi -oac mp3lame -lameopts cbr:br=128 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=200:vhq
#ffmpeg -i ./$i -vcodec libtheora -sameq -acodec libvorbis -ab 128k ./$i.avi

##-b bitrate
#ffmpeg -i ./$i -f avi -s 720x544 -vcodec mpeg4 -b 1500k -g 300 -bf 2 -acodec libmp3lame -ab 128k ./$i.avi

##mencoder "./$i" -vf scale=432:320 -force-avi-aspect 4/3  -idx -o "./$i andr432x320.avi" -ovc xvid -xvidencopts turbo=1:vhq=4:bvhq=1:chroma_opt:quant_type=h263:bitrate=512:trellis -sws 2 -nosub -alang ru -oac mp3lame -lameopts br=96:cbr
# mencoder "./$i" -vf scale=480:272 -force-avi-aspect 16/9 -idx -o "./$i andr480x272.avi" -ovc xvid -xvidencopts turbo=1:vhq=4:bvhq=1:chroma_opt:quant_type=h263:bitrate=512:trellis -sws 2 -nosub -alang ru -oac mp3lame -lameopts br=96:cbr

# mencoder ./$i  -vf pp=ci,scale=480:272 -ovc xvid -xvidencopts vhq=2:bvhq=1:chroma_opt:bitrate=3200 -sws 9 -alang ru -oac mp3lame -lameopts br=96:cbr -nosub -noaspect -o ./$i.avi
# mencoder ./$i  -vf pp=ci,scale=432:320 -ovc xvid -xvidencopts vhq=2:bvhq=1:chroma_opt:quant_type=mpeg:bitrate=1200 -sws 9 -alang ru -oac mp3lame -lameopts br=96:cbr -nosub -noaspect -o ./$i.avi
# prj  mencoder ./$i  -vf pp=ci,scale=432:320 -ovc dx -oac mp3lame -lameopts br=96:cbr -nosub -noaspect -o ./$i.avi




# MULTILINE SAMPLE
# 
# mencoder tv:// -tv driver=v4l2:width=768:height=576:input=0:normid=17:alsa:adevice=hw.1,0 \
#    -ovc xvid -xvidencopts bitrate=1200:chroma_opt:bvhq=1:quant_type=mpeg \
#    -oac mp3lame -lameopts vbr=0:br=64:mode=3 \
#    -vf pp=hb/vb/dr/al/fd,hqdn3d,crop=688:540:18:16 -sws 2 \
#    -o "$file" &



# x264 
# mencoder /
#   -idx /
#   "./$i" /
#   -o ./$i.m4v /
#   -oac faac -faacopts mpeg=4:object=2:raw=1:br=96 /
#   -ovc x264 -x264encopts crf=20:nocabac:global_header:threads=auto:frameref=5:subq=7 /
#   -of lavf -lavfopts format=mp4 /
#   -vf scale=-10:320,harddup -sws 7 -ass -noautosub







# -sws <software scaler type> (see -vop scale option too) This option sets the quality (and speed, respectively) of the software scaler, with the -zoom option. For example with # x11 or other outputs which lack hardware acceleration. Possible settings are:
# 0    fast bilinear (default) 
# 1    bilinear 
# 2    bicubic (good quality) 
# 3    experimental 
# 4    nearest neighbour (bad quality) 
# 5    area 
# 6    luma bicubic / chroma bilinear 
# 7    gauss 
# 8    sincR 
# 9    lanczos 
# 10   bicubic spline 
# NOTE: For -sws 2 and 7, the sharpness can be set with the scaling parameter (p) of -vop scale (0 (soft) - 100 (sharp)), for -sws 9, it specifies the filter length (1 - 10).

# -sameq    # same bitrate

#  lame -m s -h -b 256 $i -o mp3_out/$i

#  ffmpeg -i ./$i -ab 262144 ./mp3_out/$i
#  #sample: ffmpeg -i ./Lazarev\ 18\ 09\ 2010.mp3 -aq 5 ./Lazarev\ 18\ 09\ 2010 low.mp3 






# #/bin/sh
# #Файл который надо перекодировать (указываете свой)
# in_file=/home/user/MyDownloads/Swift.avi
# #Итоговый файл
# out_file=swift.avi
# 
# #Битрейт видео (больше обычно нет смысла, хотя можете попробовать)
# bit_rate=200
# #Ширина кадра (или 320, если ваш девайс имеет такой экран)
# width=220 
# #Битрейт аудио (для кина достаточно, если клип, то поставьте 128)
# abit_rate=48
# 
# #Собственно само кодирование (ничего менять не надо):
# mencoder $in_file -vf scale -zoom -xy $width -o $out_file -oac mp3lame -ovc xvid -channels 2 -srate 22050 -fps 20 -lameopts cbr:br=$abit_rate>/dev/null













#
# Converts all MP4 files in the current directory to MP3s.
#

#for f in *.mp3; do
 #newname=`echo $f | tr ' ' '_' `
#  echo $f
 #mv "$f" $newname
 #f=$newname
 #mplayer $f -ao pcm:file=tmp.wav
 #lame -b 128 -q 2 tmp.wav ${f/.MP4/.mp3}
 #rm -f tmp.wav
#done

1 комментарий:

  1. ОШИБОЧКА!

    неправильно:
    mencoder "${curr_file}" -ffourcc DX50 -idx $fps_mc -sws $scale -vf $vf -o "${out_file}" -ovc xvid -xvidencopts $xvidopts $mp3opts_mc

    правильно:
    mencoder "${curr_file}" -idx $fps_mc -sws $scale -vf $vf -o "${out_file}" -ovc xvid -xvidencopts $xvidopts $mp3opts_mc

    не использовать:
    -ffourcc DX50

    ОтветитьУдалить