Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # vid4rb
- #
- # REQUIREMENTS:
- # Mplayer & Mencoder
- # This script encodes video for playback on Rockboxed players. It
- # automatically detects and applies good crop values. It automatically
- # scales the video to a suitable size. It detects the audio type in the
- # source video and transcodes it only if necessary. There are options
- # to make sample encodes (tests), to specify output directory, to
- # disable autocropping and to choose higher quality encoding.
- # Run the script with no options or arguments for some short
- # instructions & read the comments at the end of the script.
- SCREENW=320
- function usage ()
- {
- printf "\n\tEdit the script and change the value of SCREENW to the width
- \tof your player's screen.
- \n\tScreen Sizes WxH of various Rockbox Supported Players:\n
- \tiPod Video, Gigabeat, Fuze+\t\t\t320x240
- \tiriver H300, iPod Color/Photo & Sansa e200\t220x176
- \tiPod Nano\t\t\t\t\t176x132
- \tiAudio X5 / M5 and iriver H10 20GB / H120 160x128
- \tiPod Mini 1G / 2G\t\t\t\t138x110
- \tiriver H10 5/6GB\t\t\t\t128x128
- \tSansa c200\t\t\t\t\t132x80
- \n\nUSAGE:\n\t$(basename "$0") -o <full path to output dire\
- ctory> [videofile1 videofile2 videofile3...]
- \tIf -o is not used then new files are written to input directories.\n
- \t-t encode 120 second test sample
- \t-f # set fps to # (defaults to 25)
- \t-c disable crop detection and cropping (useful for short clips)
- \t-q Higher quality - Video at 400 kbs & Audio at 160 kbs\n
- \tConverted movies have old extension removed and are saved as
- \t<movie>_rb.mpeg or <movie>_sample_rb.mpeg\n\n"
- }
- function checkaudio ()
- {
- AUD=$(mplayer -noconfig all -frames 0 -vo null -ao null -channels 8 \
- -identify "$MOVIE" 2>/dev/null |awk -F "ID_" '{print $2}'|sort -u|\
- sed '/^$/d' |grep -ci -e AUDIO_CODEC=mpg123 -e AUDIO_RATE=44100)
- }
- function cropdetect ()
- {
- CROP="1"
- TOTAL_LOOPS="10"
- A=0
- while [ "$A" -lt "$TOTAL_LOOPS" ] ; do
- A="$(( $A + 1 ))"
- SKIP_SECS="$(( 30 * $A ))"
- mplayer "$MOVIE" -ss $SKIP_SECS \
- -frames 20 -vo md5sum -nosound -nocache \
- -vf cropdetect=20:16 2>&1 > mplayer.log < /dev/null
- CROP[$A]=`awk -F 'crop=' '/crop/ {print $2}' < mplayer.log |\
- awk -F ')' '{print $1}' | tail -n 1`
- done
- rm md5sums mplayer.log
- B=0
- while [ "$B" -lt "$TOTAL_LOOPS" ] ; do
- B="$(( $B + 1 ))"
- C=0
- while [ "$C" -lt "$TOTAL_LOOPS" ] ; do
- C="$(( $C + 1 ))"
- if [ "${CROP[$B]}" == "${CROP[$C]}" ] ; then
- COUNT_CROP[$B]="$(( ${COUNT_CROP[$B]} + 1 ))"
- fi
- done
- done
- HIGHEST_COUNT=0
- D=0
- while [ "$D" -lt "$TOTAL_LOOPS" ] ; do
- D="$(( $D + 1 ))"
- if [ "${COUNT_CROP[$D]}" -gt "$HIGHEST_COUNT" ] ; then
- HIGHEST_COUNT="${COUNT_CROP[$D]}"
- GREATEST="$D"
- fi
- done
- CROP="crop=${CROP[$GREATEST]}"
- echo -e "$CROP"
- }
- while getopts 'co:qtF' OPTION
- do
- case $OPTION in
- c) cflag=1
- ;;
- f) fflag=1
- fval=$OPTARG
- ;;
- o) oflag=1
- oval=$OPTARG
- ;;
- q) qflag=1
- ;;
- t) tflag=1
- ;;
- esac
- done
- shift $(($OPTIND-1))
- if [ $# -lt 1 ]; then
- usage
- exit 1
- fi
- printf "\n\tMovies to be converted are:\n"
- printf "\n\t\t%s" "$@"
- let CORES=$(cat /proc/cpuinfo | grep processor | wc -l)
- for MOVIE in "$@" ; do
- if [ $oflag ]; then
- VIDOUT=$(echo ${MOVIE%.*}| sed 's,.*/,,')
- VIDOUT="$oval"/"$VIDOUT"
- else
- VIDOUT="${MOVIE%.*}"
- fi
- if [ $tflag ]; then
- SAMPLE=" -ss 120 -endpos 120"
- EXTN=_sample_rb.mpeg
- else
- EXTN=_rb.mpeg
- fi
- if [ $qflag ]; then
- TRANSCODE="-af resample=44100:0:0:volnorm=2,volume=2 -oac mp3lame \
- -lameopts cbr:preset=160"
- VBITRATE="vbitrate=400"
- else
- TRANSCODE="-af resample=44100:0:0:volnorm=2,volume=2 -oac mp3lame \
- -lameopts cbr:preset=96"
- VBITRATE="vbitrate=224"
- fi
- # if audio is already mp1/2/3 at 44.1 then keep it, otherwise transcode.
- checkaudio
- if [ $AUD -eq 2 ]; then
- AUDIO="-oac copy"
- else
- AUDIO=$TRANSCODE
- fi
- printf "\n\n\tConverting $(basename "$MOVIE")\n\t
- Calculating Cropping, Scaling, Audio...wait\n\n"
- if [ -z $cflag ]; then
- # Run cropdetect, discard all the crap, get the crop value
- CROP="-vf crop=$(cropdetect| awk -F 'crop=' '/crop/ {print $2}')"
- echo $CROP
- else
- CROP=""
- fi
- # Do the encoding
- mencoder "$MOVIE"$SAMPLE \
- -of mpeg -ofps ${fval:-25} $CROP -vf scale=$SCREENW:-2 \
- -ovc lavc -lavcopts vcodec=mpeg2video:vmax_b_frames=4:$VBITRATE:threads=$CORES -mc 0 -noskip \
- -alang en $AUDIO \
- -o "$VIDOUT"$EXTN
- done
- # Cropping
- #
- # The autocrop will often cause a failure if the video is much shorter
- # than 5 minutes. Use -c to disable the autocropping.
- # Audio
- #
- # Rockbox's MPEGplayer can play video with mp1, mp2 or mp3 audio, but it
- # can only play audio with a sample rate of 44.1 KHz. It's quite common
- # to find mp3 audio at 48 KHz in video clips. This script detects if
- # audio is mp1/mp2/mp3 at 44.1. If so then the audio is retained. If not
- # then it is transcoded.
- # Quality
- #
- # Default settings are video bitrate at 224 and audio at 96.
- # Use -q to switch this to video at 400 and audio at 160.
- # If the existing audio is already mp1/mp2/mp3 at 44.1 then it will not
- # be transcoded even when the -q option is used, only the video bitrate
- # will be increased. This prevents loss of audio quality where possible.
- #
- # Both picture quality and file size increase a lot when raising video
- # bitrate to 400. Above 400 you don't really see any quality
- # improvement on tiny screens, only bigger files.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement