Help - Search - Members - Calendar
Full Version: Encoding Videos For Zaurus
OESF Forums > Distros, Development, and Model Specific Forums > Model Specific Forums > C1000/3x00 Hardware
vrejakti
It's been a while since I posted my first encoding video for the Zaurus script, so here's an updated Version 2 of it. Enjoy. happy.gif

CODE
#!/bin/sh
# Declare some variables
INFILE=$1
OUTFILE=$2
WIDTH=640
HEIGHT=480

# Check for input video.
if [ -z "$1" ]
then echo "#########################"
echo "# ERROR! NO INPUT FILE. #"
echo "#########################"
echo
echo "Usage"
echo --------------------
echo "$0 INPUT OUTPUT width_option height_option"
echo
echo "Example"
echo --------------------
echo "$0 Funny_Clip.avi Funny_Clip_Resized.avi 320 240"
exit
fi

# Check for output file name.
if [ -z "$2" ]
then echo "##########################"
echo "# ERROR! NO OUTPUT FILE. #"
echo "##########################"
echo
echo "Usage"
echo --------------------
echo "$0 INPUT OUTPUT width height"
echo
echo "Example"
echo --------------------
echo "$0 Funny_Clip.avi Funny_Clip_Resized.avi 320 240"
exit
fi

# Change to custom height and width
if [ -n "$3" ]
then WIDTH=$3
fi

if [ -n "$4" ]
then HEIGHT=$4
fi

# Print some information
echo Video Encoding for Sharp Zaurus Playback
echo By Vrejakti
echo http://www.lan358.com
echo
echo
echo "Default 640x480"
echo "Full screen [4:3]  use 640x480 [default]."
echo "Wide screen [16:9] use 640x360."
echo --------------------
echo "   Input: $1"
echo "  Output: $2"
echo "   Width: $WIDTH"
echo "  Height: $HEIGHT"
echo --------------------

# Check responce
echo "Is this correct? [Yes/No] "
read answer
# VERY USEFUL CODE: This accepts "Yes", "yes", "Y", and "y".
if [ $answer == "Yes" ] || [ $answer == "yes" ] || [ $answer == "Y" ] || [ $answer == "y" ]
then
# Start mencoder magic
mencoder "$INFILE" -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=300:vpass=1 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac mp3lame -o $OUTFILE
mencoder "$INFILE" -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=300:vpass=2 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac mp3lame -o $OUTFILE
echo "Converted file is $OUTFILE"

else echo "Encoding canceled!!!"
fi


Please post any suggestions for improvements. I'm still learning how to write bash scripts, so there's bound to be room for improvement. happy.gif Version 3 will allow for encoding multiple files.

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

Edit notes: *shudder* The above is version 2, that is more of a proof of concept. I'm posting version 4.0 below for a side by side comparison. happy.gif Many new features have been added in version 4.0 as well as a great number of simplifications.

i) Script is easier to run. Install it as vrecoder to /usr/local/bin then run `vrecoder -i AVI_File'
ii) All options have been added as switches. See `vrecoder -h' for the details.
iii) The `-a' switch attempts to resize your video keeping the correct aspect ratio.
iv) `-s' uses an experimental smartcoder that will encode every AVI file in the current directory.



CODE
#!/bin/bash

# Encoding Video for Zaurus Playback
# Script version 4.5
# Attributions:
# Vrejakti   LAN358 Inc, http://lan358.com: Head Developer
# XorA       Open Zaurus Project: Produced original mpencoder code
# Da_Blitz   OESF Forums:  additional mencoder code, and shell code
# Xamindar   OESF Forums:  feature suggestions

MAX_WIDTH="640"    # Default max width
WIDTH="640"        # Default width
HEIGHT="480"       # Default height
encoder="vrecoder" # Default encoder
BITRATE="600"      # Default quality / bit rate
CODEC="mpeg4"      # Default codec

help () {
cat << EOF
vrecoder: Video Encoding for Sharp Zaurus Playback

Usage: vrecoder [-h] [-a] [-s] [-i infile] [-o outfile]
 [-x size] [-y size] [-b bitrate]

-h,  Display this help menu
-a,  Auto calculate resized video resolution
-s,  Use smartcoder
-i,  Input File
-o,  Output File
-x,  Change width
-b,  Change bitrate. 300 default, 6000 would be smooth
-y,  Change height
--quick  No confirms, jump to encoding.

:: Encoders ::
vrecoder:   Default encoder. Works on a signle input file.
smartcoder: Experimental encoder. Atempts to encode every AVI file in the
           current directory.
:: Notes ::
[-a] depends on awk >= 3.1.5, file >= 4.3.0, grep >= 2.5.1.
    Use at your own risk.
[-s] depends on file >= 4.18, find >= 4.3.0.
    Use at your own risk.

EOF
exit
}

if [ -z "$1" -o "$1" = "-h" ]
then
help
fi

for ARG in $@; do
    case $ARG in
 -x) WIDTH="$2";    shift;;
 -y) HEIGHT="$2";      shift;;  
 -i) IN="$2";    shift;;
 -o) OUT="$2";    shift;;
 -a) autores="yes";      shift;;
 -b) BITRATE="$2";      shift;;
 -s) encoder="smartcoder";  shift;;
 --quick) rush="x";      shift;;
 *) shift;;
    esac
done

if [ "$encoder" == "vrecoder" ]
then
if [ -z "$IN" ]
then
printf "Whoops! No input found.
Did you forget to input a file with -i file.avi?\n"
exit
fi
fi

if [ -z "$OUT" ]
then
OUT="Zaurus-"$IN""
# /*
# * Why is this switch true when -i is a complex file name?
#  */
fi

if [[ "$encoder" == "smartcoder" ]]
then
rush="x"
fi

if [[ "$rush" == "x" ]]
then
answer="yes"
else

#####calculate res#############################
case "$autores" in
    "Yes" | "yes" | "Y" | "y" )
# /*
#  * Anyone have a suggestion
#  * how to better impliment this function?
#  */
    printf "Enter screen width (ie. 640): "
    read MAX_WIDTH
    height=`file $IN | awk -F, '{print $3}' | awk -F" " '{print $1}'`
    width=`file $IN | awk -F, '{print $3}' | awk -F" " '{print $3}'`
    first="$[$width*100/$height]"     # fake float
    second="$[$MAX_WIDTH*$first/100]" # restore int
    WIDTH="$MAX_WIDTH"
    HEIGHT="$second"
;;
esac
###############################################

echo
echo "Please confirm:"
echo "------------------------------"
echo "       Input: $IN"
echo "      Output: $OUT"
echo "       Width: $WIDTH"
echo "      Height: $HEIGHT"
echo "     Encoder: $encoder"
echo "     Bitrate: $BITRATE"
echo "------------------------------"

printf "Start encoding?
[Yes/No] "
read answer
fi

####functions##################################
function vrecoder ()
{
    mencoder "$IN" -ovc lavc -lavcopts vcodec=$CODEC:\
    vbitrate=$BITRATE:vpass=1 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"
#--------------------
    mencoder "$IN" -ovc lavc -lavcopts vcodec=$CODEC:\
    vbitrate=$BITRATE:vpass=2 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"
}

function smartcoder ()
{
    i=1
    while read "F"
    do
    T=$(file -zibkp "$F")
    if [[ $T == 'video/x-msvideo' ]]
    then
    OUT="Zaurus-"$F""
#    mencoder "$F" -ovc lavc -lavcopts vcodec=$CODEC:vbitrate=\
#    $BITRATE:vpass=1 -vf scale=$WIDTH:$HEIGHT -oac copy -o "Output_"$F""
#    mencoder "$F" -ovc lavc -lavcopts vcodec=$CODEC:vbitrate=\
#    $BITRATE:vpass=2 -vf scale=$WIDTH:$HEIGHT -oac copy -o "Output_"$F""
    mencoder "$F" -ovc lavc -lavcopts vcodec=$CODEC:\
    vbitrate=$BITRATE:vpass=1 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"
    #--------------------
    mencoder "$F" -ovc lavc -lavcopts vcodec=$CODEC:\
    vbitrate=$BITRATE:vpass=2 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"
# /*
# * Can someone please tell me the difference between the commented lines
# * and the uncommented lines?
#  */

    i=$[$i+1]
    fi
    done < <(find -maxdepth 1 -printf "%f\n")
}
####/functions#################################


####main#######################################
case "$answer" in
    "Yes" | "yes" | "Y" | "y" )
 $encoder
 printf "\nEncoding completed at `date`\n"
    exit;;

    "op" )
 printf "Minutes: [hh:mm:ss] "
 read minutes
 mencoder "$IN" -ovc lavc -lavcopts vcodec=$CODEC:vbitrate=\
 $BITRATE -vf scale=$WIDTH:$HEIGHT -oac copy -endpos \
 $minutes -o "$OUT"
    exit;;

esac
###############################################

printf "\nEncoding has been cancled.\n"



Todo:
Fix script so everything will work on complex file names as well as normal file names.
Have script work on each file on the command line, not just the one file included with the `-i' switch.
Replace experimental code with real portable code.
Rewrite the entire script in x86 ASM using mencoder shared libraries. (Yeah right. ;-) )
Da_Blitz
have you tried making moives with org vorbis, i am thinking that it might be worth trying out the integer decoding on the Z and see how it compares to libmad

i havent been into the video encoding scene in awhile but i belive that some of the other formats may be more Z friendly. i hear one of the design goals of mkv was fast seeking

just some sugestions. i dont have the time to fiddle with video encoding at the moment and i dont watch videos on my Z but i wonder if we can get some more performance out of these little things by changing somthing so slightly
vrejakti
QUOTE(Da_Blitz @ Jul 11 2006, 02:31 AM)
have you tried making moives with org vorbis, i am thinking that it might be worth trying out the integer decoding on the Z and see how it compares to libmad

i havent been into the video encoding scene in awhile but i belive that some of the other formats may be more Z friendly. i hear one of the design goals of mkv was fast seeking

just some sugestions. i dont have the time to fiddle with video encoding at the moment and i dont watch videos on my Z but i wonder if we can get some more performance out of these little things by changing somthing so slightly
*


Hmm, hmm... to be honest, I have very little knowledge on the actual mencoder settings. You are absolutely right, the mencoder settings I have can be tweaked for much better performance. I think MKV support under Linux is still in developing stages [It wasn't until four months ago mplayer with mkv support enabled would emerge under Gentoo], but if it can be done, I'll happily make it the default in my next update.

I'm also thinking I'll add the option to customize most encoding settings as part of running the script.
Da_Blitz
i would say that mkv is bleeding edge in linux, i use it for most of the files i "aquire"

i might take a look in an hour or two as i have to do some video encoding soon and might be able to teweak it a bit but i think that some benchmarks at diffrent bitrates for payback of aac, mp3 and org are in oder, lets say 96, 128 and 192kbps, 2 channel 16 bit audio, with both abr (average bit rate, normally confused with varible bit rate) and cbr (constant bit rate)

perhaps set up a script that encodes one file to all 3 formats at all 3 bit rates, then take it to the z and have another script run
CODE
#!/bin/bash
TEMP=$(date)
for I in *.[aac|mp3|ogg]; do
  time mplayer $I >| benchmark-$TIME
;done
echo done
then post the results as that will tell us what is happening cpu wise
chrget
QUOTE(Da_Blitz @ Jul 11 2006, 11:46 AM)
[...] i think that some benchmarks at diffrent bitrates for payback of aac, mp3 and org are in oder, lets say 96, 128 and 192kbps, 2 channel 16 bit audio, with both abr (average bit rate, normally confused with varible bit rate) and cbr (constant bit rate)
*

While it's over a year old and was originally in a different context, a posting I wrote lays down the basic observations I have made w/regard to A/V-encoding and playback in the many days I have tinkered with it. It should still hold true today, and while I haven't done any specific benchmarking for quite some time now, there is nothing that indicates otherwise.

Maybe this can give you some idea of what to expect.

Best regards,
Chris.
xamindar
BTW, what's this "org" you guys are talking about? Is it some new variation of "ogg"?

I used to have ogg encoded videos run on my 5600 but when I tried those same videos on my 3100 with kino2 they would not play. Has ogg support been removed?
Da_Blitz
yeah we are talking about ogg

had a quick read and it probelly does still hold true today. i would still like to see a retest just to confirm everything (just in case they improved the code)

you do have some good points there. i use to cut the framerate in half for animation as it would improve things alot. i did notice that setting up your processing filters and changing the order has a huge impact of performance

the order i would do it in was
deinterlace
denoise
crop
shrink
rotate

that gives the best visual results but takes longer to do than other orders of post processing

is there a guide around here to what the maximum video capabilities of each model are. could be handy and allow us to chose several diffirnt profile (ie c700 low, med, high and c3000 low medium high)

i heard MKV has support for varible frame rates. for animation this could be handy due to the high amount of static images in some animaiton. in others the difrence between the frames is small enough that it should encode to be tiny anyway
SadaraX
QUOTE(vrejakti @ Jul 11 2006, 01:20 AM)
I think MKV support under Linux is still in developing stages [It wasn't until four months ago mplayer with mkv support enabled would emerge under Gentoo], but if it can be done, I'll happily make it the default in my next update.


Its funny that mkv is still perceived as the bleeding edge. Someone else mentioned AAC audio, which is considerably newer than the mkv project. That format just been pushed everywhere and implemented in many things, so it seems less bleeding edge. smile.gif

About the video encoding this, I would be interested if anyone has comments about the ogg media container (OGM) and vorbis audio in their videos for the Zaurus series.

That script looks rather handy. I will probably write a similar script that can use Avidemux to encode videos....
Da_Blitz
i have been getting alot of stuff with subtitles and multitrack audio in the mkv format. the reason i say its bleeding edge is because they are still extening (or mabey not. havent looked at it scince i went from winCE to lin and left betaplayer behind)

i have used OGM on windows and found the ffmpeg stuff rather poorley intergrated (sits next to the clock) however its good under linux.

i think we will probelly end up with ogg audio, must do some testing tomorrow smile.gif (libav gives me headaches under mplayer, its to damn comprehensive smile.gif)
xamindar
Great script vrejakti! Thanks. It makes videos that play the best I have seen on my zaurus.

I have an idea. Is it possible to just specify the width, for example 640, and then have the height automaticly set according to the aspect ratio of the input video? It seems a few of my videos have strange aspect so I have to open them up in xine, check the aspect, then calculate it myself. And sense I am never interested in streching the video this would be very usefull.

You know what I mean?
W/640=H/h
where:
W= original width
640= new width
H= Original height
h= new height
vrejakti
Hey everyone, I have some free time, so I'll be re-writing this script to add the option of custimizing the video and audio formate easily. I was going to post an updated script... a couple months ago wink.gif but I gave up figureing out how mencoder works. I can write bash scripts with ease, but what mencoder does is all magic to me.

If anyone can explain how mencoder chooses the video codex and/or audio codex, I'd be happy to write a script that includes all the options you can provide me.

Well, I'd like to keep my development code secret, but here's what we're looking at for the next script:
CODE
# Declare some variables
INFILE=$1
OUTFILE=$2
WIDTH=640
HEIGHT=480
AUDIO=mp2
VIDEO=lavc
VIDEO_CODEC=mpeg4


echo Choose an audio codec
echo "[mp2] // MPEG Layer 2"
echo "[ac3] // AC3, AKA Dolby Digital"
echo "[adpcm_ima_wav] // IMA adaptive PCM (4 bits per sample, 4:1 compression)"
echo "[sonic] // experimental lossy/lossless codec"


# Start mencoder magic
#mencoder "$INFILE" -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=300:vpass=1 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac copy -o $OUTFILE
#mencoder "$INFILE" -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=300:vpass=2 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac mp3lame -o $OUTFILE
mencoder "$INFILE" -ovc $VIDEO -lavcopts vcodec=$VIDEO_CODEC:vbitrate=300:vpass=1 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac lavc -lavcopts acodec=$AUDIO -o $OUTFILE
mencoder "$INFILE" -ovc $VIDEO -lavcopts vcodec=$VIDEO_CODEC:vbitrate=300:vpass=2 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac lavc -lavcopts acodec=$AUDIO -o $OUTFILE


All I need is some options to code in for $VIDEO_CODEC and $AUDIO.

I wish it was as simple as VIDEO_CODEC=mkv AUDIO=ogg - it seems a fair bit more complicated.
vrejakti
QUOTE(xamindar @ Jul 17 2006, 03:01 PM)
Great script vrejakti!  Thanks.  It makes videos that play the best I have seen on my zaurus.

I have an idea.  Is it possible to just specify the width, for example 640, and then have the height automaticly set according to the aspect ratio of the input video?  It seems a few of my videos have strange aspect so I have to open them up in xine, check the aspect, then calculate it myself.  And sense I am never interested in streching the video this would be very usefull.

You know what I mean? 
W/640=H/h
where:
W= original width
640= new width
H= Original height
h= new height
*


Thanks xamindar. smile.gif I'm sorry for taking so long to reply to you - that's an excellent idea to have the script calculate the the height given the aspect ratio.

I have the formual A/B = C, B*C=A written on my wall so I always remember how to calculate the right ratio. *laughs* Might as well code that math into the next script. I'll see what I can do. ^^
Da_Blitz
i belive that mencoder has settings to adjust it automajically, but i have had problems when applying it to DVDs

if you want some help, email me and i will see what i can do
vrejakti
If you guys liked my first scripts, you're going to LOVE this one! I simplified the heck out of it, and the auto resolution calculation works so nicely.

All you have to do is type
./script video.avi
Yes
Yes

And it does the rest. :-D

CODE
#!/bin/sh
# Encoding Video for Zaurus Playback
# Script version 3
# Attributions:
#
# Vrejakti of LAN358 Inc (http://lan358.com): Main contributor of Bash code
# XorA of the Open Zaurus Project: Produced original mencoder code
# Da_Blitz from OESF Forums: Provided additional mencoder code
# Xamindar from OESF Forums: Provided feature suggestions


# Declare some variables
INFILE=$1
OUTFILE=Zaurus-$1
WIDTH=640
HEIGHT=480
AUDIO=mp2
VIDEO=lavc
VIDEO_CODEC=mpeg4
#--------------------
ERROR1="#########################
# ERROR! NO INPUT FILE. #
#########################

(Usage) $0 INPUT OUTPUT width_option height_option
--------------------
(Ex) $0 Funny_Clip.avi 320 240\n"

#--------------------
# Check for input video.
if [ -z "$1" ]
then printf "$ERROR1"
exit
fi

# Change to custom height and width
if [ -n "$2" ]
then WIDTH=$2
fi

if [ -n "$3" ]
then HEIGHT=$3
fi

# Print some information
printf "Video Encoding for Sharp Zaurus Playback\n\n"

printf "Would you like to use experimental auto resolution calculation? [Yes/No] "
read custom
case "$custom" in
"Yes" | "yes" | "Y" | "y" )
height=`file $INFILE | gawk -F, '{print $3}' | gawk -F" " '{print $1}'`
width=`file $INFILE | gawk -F, '{print $3}' | gawk -F" " '{print $3}'`
first="$[$width*100/$height]"
second="$[640*$first/100]"
WIDTH=640
HEIGHT=$second
;;
esac

#--------------------
#--------------------
MAGIC1="mencoder "$INFILE" -ovc $VIDEO -lavcopts vcodec=$VIDEO_CODEC:vbitrate=300:vpass=1 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac lavc -lavcopts acodec=$AUDIO -o $OUTFILE"
#--------------------
MAGIC2="mencoder "$INFILE" -ovc $VIDEO -lavcopts vcodec=$VIDEO_CODEC:vbitrate=300:vpass=2 -lameopts cbr:br=64:mode=3 -vf scale=$WIDTH:$HEIGHT -oac lavc -lavcopts acodec=$AUDIO -o $OUTFILE"
#--------------------
#--------------------

echo "Full screen [4:3]  use 640x480 [default]."
echo "Wide screen [16:9] use 640x360."
echo --------------------
echo "       Input: $INFILE"
echo "      Output: $OUTFILE"
echo "       Width: $WIDTH"
echo "      Height: $HEIGHT"
echo " Audio Codec: $AUDIO"
echo " Video Codec: $VIDEO_CODEC"
echo --------------------
echo " First mendoder command: $MAGIC1"
echo --------------------
echo " Second mencoder command: $MAGIC2"


# One last chance to bail out
printf "\nStart encoding? [Yes/No] "
read answer

# Start mencoder magic
case "$answer" in
 "Yes" | "yes" | "Y" | "y" )
$MAGIC1
$MAGIC2
printf "\n\nConverted file is $OUTFILE"
exit;;
esac

printf "\nEncoding has been cancled.\n"
Da_Blitz
got your email and am looking into the mkv stuff

however i dont mean to cast your work in a bad light but here is some code i whipped up quickly (still needs to be debugged and finnished). at the moment its just the setup code however it might be handy to you as it is

Featuers:
Easy to maintian command line parsing
built in help
profile support to quickly encode diffrent files in diffrent ways (for example decimate the framrate by 2 and apply the xvid cartoon settings for anime)
ability to override built in varibles from the command line to assist in testing and activate advanced features (-o <somthing>=<val>)
supports config files (user and system)
rotation support (any angle)
Multiple file support (even when only one file is specified, this is so there are no special hacks for multipass files and so that they can be treated in the same manner as single pass)

planned features:
accept files as command line args or as part of a pipe (INFILE="-")

CODE
# Copyright: Jay Coles 2006
# Licsence: GPL v2
# Version 0.9

# ######## Cut and paste from here into config file ##########
# ***** File Vars *******
ENCODER=mencoder
# File to be converted
INFILE=""
# Save file as
OUTFILE=""


# ****** Encoding Vars ******
# nice easy policys for encoding diffrent media
# added because it was easy to hack together :)
# note that you might want to add a policy_ to the
# front of your funtions to avoid clobbering
# the internal code
POLICY=policy_anime

policy_anime() {
    V_CODEC="xvid"
    A_CODEC="mp3"

    V_BITRATE="300"
    A_BITRATE="96"

    #Diffrent encoding policies (ie cbc abr vbr multipass)
    V_POLICY="multipass"
    A_POLICY="cbr"

    # allow recording at diffrent rates/fps
    # this really helps with anime by decimating the fps
    # by 2, allows for huge reductions in size with a small
    # loss in percived quality (ie motion is slightly more
    # jagged)
    V_RATE="0.5"
    A_RATE="22050"

    # Allow overiding of ratio blank for keep original
    RATIO=""
    KEEP_RATIO="y"
    # Note that these are max vals that will never be exceeded
    # while still maintiaining asspect ratio
    X_RES="640"
    Y_RES="480"

    #Amount of degrees to rotate video (helps increse performance)
    ROTATION=90
}

################ End cut and paste ################

if [ -f /etc/zencoder.conf ]; then
    . /etc/zencoder.conf
fi

if [ -f ~/zencoder.conf ]; then
    . ~/zencoder.conf
fi

# Auto sources Policy
$POLICY

help () {
cat << EOF
zencoder: encodes moives for small media players

Usage: zencoder [-h | --help] [-o <VAR NAVE>=<VAL>] [-a <audio codec>]
               [-v <video codec>] [-b <video bitrate] [-d <audio bitrate>]
 [-x <res>] [-y <res>] [-r <rotation>] INFILE [-f OUTFILE]

    -h --help    This menu
    -o  Allows you to overide built in varibles
     Mainly for testing
    -a <audio codec>
     The audio codec used to encode the audio
     use help for an abreaveated listing
    -v <video codec>
     The video cadec used to encode the video
     use help for an abreaveated listing
    -b <video bitrate>
     The bitrate used to encode the video
    -d <audio bitrate>
     the bitrate used to encode the audio
    -x <res>    The max X resolution
    -y <res>    The max Y resolution
    -r <rotation>    The angle to rotate the video output
    INFILE  The file to be processed
    -f <OUTFILE>    The name to call the compleated file,
     Note that if not specified then it is
     Automatically detirmined from the video
     codec
EOF

exit
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then
    help
fi

for ARG in $@; do
    case $ARG in
 -o) $ARG; shift;;
 -a) A_CODEC=$ARG; shift;;
 -v) V_CODEC=$ARG; shift;;
 -b) V_BITRATE=$ARG; shift;;
 -d) A_BITRATE=$ARG; shift;;
 -x) X_RES=$ARG; shift;;
 -y) Y_RES=$ARG; shift;;
 -r) ROTATION=$ARG; shift;;
 -f) OUTFILE=$ARG; shift;;
 *) FILES="$FILES $ARG"; shift;;
    esac
done

for I in $FILES; do
    test -f $I || echo "File: $I dosent exist, bailing out" && exit 1
done

# note that we pass FILES in a loop to the var INFILE and process in
# the loop locally as INFILE


enjoy, hopefull i should be about 80% compleate tommorrow on it (and actually have files encoding with it), also wont be that hard to get it to just do audio encoding (ie audio CD to mp3 aka ripping, specify INFILE as cdda://1)

*** edit added file as the CODE statement on the fourms clobbers the formatting sad.gif *******
***** edit this fourm dosent like files with no extension or the .sh extension, sorry about the tar.gz'ing, i relise the tar is unnecsarry (blame gnome wink.gif *****
vrejakti
nice code Da_Blitz, very nice! Admittedly, I am still learning how to write bash code, and the difference in professionalism between our scripts is quite crushing to me, but I don't mind. ^^ I'll keep improving my script as I learn more.

Keep up the good work. smile.gif
Da_Blitz
smile.gif thanks for that

when i switched to linux i did it the hard way and tried to install debian as a noob sad.gif the reason was that i had to do a course on scripting for my egineering course. scince then i havent looked back and prefer to work with the command line

probelly the best intro to scripting is the read the advanced bash scripting guide (where i got the command line arg passing from) and the alien guide as well as the man pages for bash

links:
http://subsignal.org/doc/AliensBashTutorial.html
http://www.tldp.org/LDP/abs/html/

read i bit at a time but most of it comes from having a problem to solve, such as video encoding. the advanced bash scripting guide is great as it shows you best practicies and how to make your code portable (between shells and diffrent OS's)

for a bit of color (providing the $TERM enviroment setting is correct) try the tput command. here is a bit from my zsh startup scripts (i manage it in a simmilar way to init)

CODE
       GREY_TXT=`tput setf 7`
       RED_TXT=`tput setf 1`
       GREEN_TXT=`tput setf 2`
       YELLOW_TXT=`tput setf 3`
       BLUE_TXT=`tput setf 4`
       PINK_TXT=`tput setf 5`
       CYAN_TXT=`tput setf 6`
       WHITE_TXT=`tput setf 7`
       NORMAL_TXT=`tput setf 8`

       BOLD=`tput bold 1`
       BOLD_OFF=`tput bold 0`
       DIM=`tput dim 1`
       DIM_OFF=`tput dim 0`
       UNDERLINE=`tput smul`
       UNDERLINE_OFF=`tput rmul`
       REVERSE=`tput rev 1`
       REVERSE_OFF=`tput rev 0`
       ALTERNATE=`tput smacs`
       ALTERNATE_OFF=`tput rmacs`


and my prompt:
CODE
PS1="$BOLD$WHITE_TXT,-$BLUE_TXT [$RED_TXT%* %D$BLUE_TXT]-[$GREEN_TXT%n@$GREEN_TXT%M$BLUE_TXT]-[$PINK_TXT%y$BLUE_TXT]-[$RED_TXT%d$BLUE_TXT]$WHITE_TXT-,
'-$BLUE_TXT [$RED_TXT%?$BLUE_TXT] $RED_TXT%# $WHITE_TXT>- $WHITE_TXT"
RPS1=""

CODE
,- [19:24:23 06-09-14]-[dablitz@dhcppc7]-[pts/2]-[/home/dablitz/.zsh/startup]-,
'- [0] % >- cat 90prompt


your PS1 string is a good way to learn scripting and the builtin text manipulation abilities of the shell you use, keep in mind however that a 2 line prompt has a problem or two and i haveyet to see a shell that can handel without problems, i just live with it sad.gif
vrejakti
Phew, looks like I don't have to implement ogg or mkv support into the script after all.

From the legendary Gentoo Wiki: http://gentoo-wiki.com/HOWTO_Mencoder_Introduction_Guide
QUOTE
"Ogg or Ogg bitstream format is also an open-source video container, part of the Xiph project. OGM is an extension of Ogg bitstream format to support some proprietary video codecs. Like matroska, mplayer can play, but not create Ogg and OGM videos."

"Matroska is an open-source video container, similar to AVI, except that it has many more advanced options and settings that can be included in the metadata. mplayer and mencoder can play and read matroska files, but not create them. Matroska audio and video files use filename extensions .mka and .mkv, respectively."


So, as of yet, mencoder and mplayer can read and play mkv or ogg file formates, but they can not create them.

Da_Blitz thank you for pointing out Aliens Bash Tutorial - that's the perfect resource to bring me up to speed on bash scripting. happy.gif I've played around with the ABS guide for about a year now, and it's great, but Aliens guide looks to be the perfect compliment to it.

As for your shell prompt, that has got to be the most customized shell prompt I have ever seen. smile.gif
Da_Blitz
thanks as i said it causes problems but as its hard to tell which machine i am on it cames in handy wink.gif

i am glad i found ABS, it really helped to cement the stuff i learnt from the bash guide, if you get a chance readup on sed from an oriley book and perhaps awk, however if you think awk is a good idea then perl or python are probelly a better idea

with rivision 2 of the code i will be able to support multiple encoding egines (mencoder or the mkv or ogg stuff) so we will see if we need support for it <manical laugh>

i must say that the gentoo site is great, i dont think many people give gentoo enough credit as it has some of the best infomation of any distro i have encountered
Da_Blitz
Here is version 2.0, fixed a problem with the command line parsing and added code to actually encode the file

whats left is post processing or additinol options and cropping/scaling support as well as automatic sellection of the correct encoder for the specified format, however i suspect it will be implmented with function calls to the encoder profile and require a bit of a rewrite of the stuff that has been added

apart from that have fun and please poke holes in it, i expect that some areas can be slightly improved and i suspect there is a bug or two in there. in its current state it should reencode a moive if you set up the policy correctly.

CODE
# Copyright: Jay Coles 2006
# Licsence: GPL v2
# Version 0.9

# ######## Cut and paste from here into config file ##########
# ***** File Vars *******
ENCODER=mencoder
# File to be converted
INFILE=""
# Save file as
OUTFILE=""


# ****** Encoding Vars ******
# nice easy policys for encoding diffrent media
# added because it was easy to hack together :)
# note that you might want to add a policy_ to the
# front of your funtions to avoid clobbering
# the internal code
POLICY=policy_anime
POLICY_LIST="$POLICY policy_anime "

policy_anime() {
       NAME="Anime Medium Quality"
       DESCRIPTION="This is designed for encoding media for a zaurus with an anime file with hard subs (hence the high res)"
       AUTHOR="Jay Coles"
       LICSENSE="GPL"

       CONTAINER="avi"
       V_CODEC="xvid"
       A_CODEC="mp3"

       V_BITRATE="300"
       A_BITRATE="96"

       #Diffrent encoding policies (ie cbc abr vbr multipass)
       V_POLICY="multipass"
       A_POLICY="cbr"

       # allow recording at diffrent rates/fps
       # this really helps with anime by decimating the fps
       # by 2, allows for huge reductions in size with a small
       # loss in percived quality (ie motion is slightly more
       # jagged)
       V_RATE="0.5"
       A_RATE="22050"

       # Allow overiding of ratio blank for keep original
       RATIO=""
       KEEP_RATIO="y"
       # Note that these are max vals that will never be exceeded
       # while still maintiaining asspect ratio
       X_RES="640"
       Y_RES="480"

       #Amount of degrees to rotate video (helps increse performance)
       ROTATION=90
}

ENCODER_POLICY=encoder_mencoder

encoder_mencoder() {
       ENCODER_PROG=mencoder

       PROVIDES="xvid divx mp4"

       V_CODEC_STRING="-voc"
       A_CODEC_STRING="-aoc"
}
################ End cut and paste ################


################ functions ################
locate_codec() {
       for I in $ENCODER_POLICY; do
               . $I
               if (echo $PROVIDES | grep $1) = 0; then
                       echo $I
                       exit 0
               fi
       done

       exit 1
}


if [ -f /etc/zencoder.conf ]; then
       . /etc/zencoder/zencoder.conf
fi

if [ -f ~/zencoder.conf ]; then
       . ~/zencoder/zencoder.conf
fi

help () {
cat << EOF
zencoder: encodes moives for small media players

Usage: zencoder [-h | --help] [-o <VAR NAVE>=<VAL>] [-a <audio codec>]
               [-v <video codec>] [-b <video bitrate] [-d <audio bitrate>]
               [-x <res>] [-y <res>] [-p <policy>] [-r <rotation>]
               INFILE [-f OUTFILE]

       -h --help       This menu
       -o              Allows you to overide built in varibles
                       Mainly for testing
       -a <audio codec>
                       The audio codec used to encode the audio
                       use help for an abreaveated listing
       -v <video codec>
                       The video cadec used to encode the video
                       use help for an abreaveated listing
       -b <video bitrate>
                       The bitrate used to encode the video
       -d <audio bitrate>
                       the bitrate used to encode the audio
       -x <res>        The max X resolution
       -y <res>        The max Y resolution
       -r <rotation>   The angle to rotate the video output
       -p <policy>     Which settings to use to encode the file
       INFILE          The file to be processed
       -f <OUTFILE>    The name to call the compleated file,
                       Note that if not specified then it is
                       Automatically detirmined from the video
                       codec
EOF

exit
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then
       help
fi

for ARG in $@; do
       case $ARG in
               -o) $2; shift;;
               -a) A_CODEC=$2; shift;;
               -v) V_CODEC=$2; shift;;
               -b) V_BITRATE=$2; shift;;
               -d) A_BITRATE=$2; shift;;
               -x) X_RES=$2; shift;;
               -y) Y_RES=$2; shift;;
               -r) ROTATION=$2; shift;;
               -f) OUTFILE=$2; shift;;
               -p) POLICY=$2; shift;;
               *) FILES="$FILES $2"; shift;;
       esac
done

for I in $FILES; do
       test -f $I || ( echo "File: $I dosent exist, bailing out"; exit 1 )
done

# Auto sources Policy
$POLICY
$ENCODER_POLICY

# note that we pass FILES in a loop to the var INFILE and process in
# the loop locally as INFILE

for INFILE in $FILES; do
       $ENCODER $V_CODEC_STRING $V_CODEC $A_CODEC_STRING $A_CODEC $INFILE $OUTFILE
done
johnny_a
The script above gives me this error:

CODE
./zencoder.sh: line 77: syntax error near unexpected token `='
./zencoder.sh: line 77: `               if (echo $PROVIDES | grep $1) = 0; then'


Any ideas? I use bash.
vrejakti
QUOTE(johnny_a @ Oct 4 2006, 01:49 PM)
The script above  gives me this error:

CODE
./zencoder.sh: line 77: syntax error near unexpected token `='
./zencoder.sh: line 77: `               if (echo $PROVIDES | grep $1) = 0; then'


Any ideas? I use bash.
*


Huh, at first glance, I'm thinking you may have saved the config file into the shell script. I haven't give this much thought, so I'm probably completly wrong here: Half the code may be a template for a config file that you save your perferences in. The other half is the shell script that does the encoding, reading defaults from the configuration file.
johnny_a
Thanks!

I didn't bother to read through the script.. which I should have done, of course. wink.gif thanks again.
Da_Blitz
i thoght that the ####### cut here ##### was obvious enogh

i am aware it dosent work 100% right now but i am very happy with the structure of the code, all that comes now is the meat of it
vrejakti
I've tweaked my script once again. Code is cleaner. As for features, I added
1) Option to quickly jump to encoding. Type x when asked to, and it'll skip the next 3 questions.
2) A bit of an easter egg that lets you rip the OP of an anime episode in very high quality. At the last menu when it asks "Start encoding?" type "op" then enter hh:mm:ss of the episode to rip to HQ. 00:02:00 would grab the first 2 minutes. I wrote this code when I wanted to show a friend the coolest OP's from the newest anime season.
3) Tried to make it clearer for errors and note the importance of simple file names.

Complex file names are a bit of an issue in bash. Make sure your infile is "simplefilename.avi" and not "[I AM] 4 c0m|>;e'x' fiL_e name~~~~!". Renaming it is eacy in a GUI, select the file, hit F2, and type in something sane. In the console, make sure you're using Bash or ZSH, then type a single quote, a couple unique first letter, then press tab to auto comple the name. Use mv to rename that file to something simple.

Posting all these scripts as text is getting pretty crazy. *laughs* Here's a direct link to a download.

Download:http://www.lan358.com/scripts/vrecoder.sh
View:http://www.lan358.com/scripts/vrecoder.txt
Da_Blitz
not bad at all, never did like interactive stuff in bash but it seems to work out alright (im a big fan of the use the flags and get out of my face philoshpy, espesially when doing a batch job like this)

i have been thinking about mine and might do a bit of an archtecture change so that it only encodes 1 moive at a time as its easy enaugh to write a do while or for i in on the command line in one line (or even an xargs) and it simplifies some of my handelling

perhaps in the futre we can set it up so that you use my script as the encoding back end and yours as the interactive?
vrejakti
Combining scripts is an interesting idea. For now, I'll take a good hard look at your code and see what ideas I can come up with. Your script has a lot of features I like, such as the custimization options and the flags. At the moment, I'm looking for what code would be needed so a user simply types "./zencoder movie.avi" for the first time and the script takes care of the rest.

It should be possible to have the script make the dir $HOME/zencoder and use the same cat << EOF trick to drop the config text into $HOME/zencoder/zencoder.conf

All this remeinds me, I wanted to ask you what you would think of a ncurses interface for this whole script? Could be overkill, but it could also be useful in the sence of having a menu to check off which codec to use and such?
vrejakti
Several tweaks later... I've got your script encoding a file after typing ./zencoder -i video.avi -o z-video.avi The tweaks are barely proof of concept, but you may find something worth building on. smile.gif

http://lan358.com/scripts/zencoder.txt
vrejakti
In a couple days I'll be releasing a new version of vrecoder that has two new features:

First off, it can handle any file name. So if you happen to have a lot of files named similar to [S^M] Galaxy Ange-lune 04 RAW.avi, the new script will have no issues at all with them.

Second, the script will be able to determine what files in the current directory are video files, and it will encode everyone of them. Obviously, this feature will be very useful if you want to convert your entire collection of videos over night.

Anyone who'd like a head start is welcome to hack the following code into a script of their own. Also, the shell zsh will be required to run the new script, so please grab it from
http://www.zsh.org/
or by using the package manager of your distro.

CODE
#!/bin/zsh
SMART_CODE=smart_code
smart_code () {
i=1
while read F
do
T=$(file -zibkp "$F")
minutes="00:01:00"
[[ $T == 'video/x-msvideo' ]] && mencoder $F -ovc lavc -lavcopts debug=0:vcodec=mpeg4:vbitrate=6000 -oac copy -endpos $minutes -o Zaurus-$i.avi
i=$[$i+1]
done < <(find -maxdepth 1 -printf "%f\n")
}
$SMART_CODE


The above code is a proof of concept for the two features. ;-)
Da_Blitz
any particular reason for zsh? i try hard to keep my dependencies down to bash

anyway back on topic, i did a rehash of my script and removed the ability to encode more than one file at a time.

other changes include a config file redesign so that the config file consists of a couple of varibles and one function (encode())

the reason for this is that several of the encoders handel things very diffrently so the codec choice has been moved to the encoder (ie no more chosing mencoder as the backend and allowing the "anime" policy to chose the codec

the policy now only choses bitrate, this was because of the incompatability of some mixtures and working out the match up was a PITA. in futre builds i might just merge this as well so that you only have to trade one file. actually that sounds like a better idea i might do that

funny thing is that mplayer already has support for decoding/encoding profiles built in so all my script is goot for is non mencoder support and automating multipass.

you think i should just release optimised mencoder profiles to go into the .mplayer folder i dont think mkv is a biggy at the moment (althogh i do prefer it) and ogg support would be nice however getting this stuff to work with streams is a nighmare (ie demux each steram from the container, rencoding the streams then reassemble into new container format), my current code will do it nicly but is it worth the effort (ie how many people want ogg and mkv support over avi and aac/mp3 with mpeg4)
nilch
QUOTE(vrejakti @ Jul 10 2006, 11:56 PM)
It's been a while since I posted my first encoding video for the Zaurus script, so here's an updated Version 2 of it. Enjoy. happy.gif

Please post any suggestions for improvements. I'm still learning how to write bash scripts, so there's bound to be room for improvement. happy.gif Version 3 will allow for encoding multiple files.
*


I lost this thread here ... tell me, is this all doable on the Zaurus, or is it just video encoding for the Zaurus on the desktop ?

I would love to have some script which can encode single to 2-3 files on my zaurus which I copy from the internet without knowing if its codecs will be supported by Kino or not.

Having a mass batch encoder like what the thread discusses later on, on the desktop for overnight runs is also a good idea but naturally limited to the desktop.

Sorry guys, not much idea about video encoding hence the incomprehension about basic facts ohmy.gif
vrejakti
You're right about this thread being all over the place. happy.gif To answer your question, I always assumed the encoding would be done on a desktop, mainly because computer resources greatly affect the time to encode a video. It is still possible to encode on the Zaurus, if you have the mencoder package installed.

I have seen a mencoder package for the Zaurus, though I'm sorry I don't have a link.
vrejakti
QUOTE(Da_Blitz @ Nov 1 2006, 06:13 AM)
any particular reason for zsh? i try hard to keep my dependencies down to bash
*

Here's the issue I had when using /bin/bash or /bin/ksh

CODE
debian% ls
[S^M] Galaxy Ange-lune 04 RAW.avi
debian% ./script.sh
File not found: '[S^M]'
Failed to open [S^M].

zsh seems to be the only shell that can properly pass complex file names in variables to mencoder. Personally, I use whatever works. At some point, I may even try doing this same script in perl or C - just for heck of it.

Edit: After thinking about it, you raise a very good point. I assume a lot more people have access to bash than zsh, so I changed my newest release to use #!/bin/bash by default, leaving the end user the option to change it to use #!/bin/zsh if needed for complex file name issues.

I've tried a lot of solutions to file names. From using a long script of sed replacement commands, to saving files to a text file and greping each line, as well as quoting, double quoting, and single quoting. zsh works for now.

Keep up the good work on your script Da_Blitz. I'm looking forward to seeing your next release. I still think the scripts we're working on are very useful. Not many people have the time to read the mplayer man pages to learn how to write a profile for their needs, that's why scripts that simplify or automate the proccess are still useful, I think. happy.gif




Here's my latest script
View script online as text.
Download script.

Notes on features.
Using -e smartcoder tells the script to automagically seek out any video files in the current directory and encode them for Zaurus Playback.

Experimental auto-resolution calculation tries to guess a correct 4:3 resolution for you when scaling the video.

Selecting "op" when asked "Start encoding? [Yes/No/op]" will allow you to encode the first hh:mm:ss of a video. It's good for catching opening themes from anime.
grog
QUOTE(vrejakti @ Nov 1 2006, 06:02 PM)
QUOTE(Da_Blitz @ Nov 1 2006, 06:13 AM)
any particular reason for zsh? i try hard to keep my dependencies down to bash
*

Here's the issue I had when using /bin/bash or /bin/ksh

CODE
debian% ls
[S^M] Galaxy Ange-lune 04 RAW.avi
debian% ./script.sh
File not found: '[S^M]'
Failed to open [S^M].

zsh seems to be the only shell that can properly pass complex file names in variables to mencoder. Personally, I use whatever works. At some point, I may even try doing this same script in perl or C - just for heck of it.

I've tried a lot of solutions to file names. From using a long script of sed replacement commands, to saving files to a text file and greping each line, as well as quoting, double quoting, and single quoting. zsh works for now.
*


You should be able to just quote the variable, for example:

CODE
mencoder "$F"

Admittedly I've never used zsh, but I've yet to find anything that couldn't be accomplished with bash or korn smile.gif. HTH
Da_Blitz
well the problem that you are getting is related to spaces in the file name, it is wierd that double quoting dosent work (single qoutes dont allow for varible expansion wink.gif)

i would think that
CODE
mencoder "$IN" -ovc lavc -lavcopts debug=0:vcodec=mpeg4:vbitrate=300:vpass=1 -vf scale=$WIDTH:$HEIGHT -oac copy -o $OUT
#--------------------
mencoder "$IN" -ovc lavc -lavcopts debug=0:vcodec=mpeg4:vbitrate=300:vpass=2 -vf scale=$WIDTH:$HEIGHT -oac copy -o $OUT


should become
CODE
mencoder "$IN" -ovc lavc -lavcopts debug=0:vcodec=mpeg4:vbitrate=300:vpass=1 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"
#--------------------
mencoder "$IN" -ovc lavc -lavcopts debug=0:vcodec=mpeg4:vbitrate=300:vpass=2 -vf scale=$WIDTH:$HEIGHT -oac copy -o "$OUT"


and just magiacally work ($OUT to "$OUT")

anyway back to my ones, perosnally i wanted to have my script done by now so i could focus on coming up with the encoding reipies that work best for the Z, its a shame that i havent gotten to that stage yet

i use Zsh all the time for its contextual auto compleation however there are some features (or bugs) that i dont like so i have been considering making an ncurses shell that is interactive only (ie not for scripting) as i feel bash is more than adequte for that. the advantage of using ncurses is that its easy to extend to dual monitor support (which i have) and helps take care of resizing. instead of the zsh autocemplete with filnames that shifts everything up and leaves it that way when it disappears (anoying when you needed the history) i am thinking of a popup window that slides form the bottom upwards moving the content up as well. when it disappears the content slides down to its originol postion as well

its somthing i am working on as well as a small 100 liner copy and paste deamon (how do you like bieng able to go cat $FILE | copy in one shell and paste | xargs "ls -l" in another wink.gif) or an enviroment deamon that makes sure that any varible you export (ie export PATH) gets automatically synced across all open shell instances instantlly (i used to have problems when i had 10+ xterms open with diffrent states of exported varibles, sucks when trying to cross compile) designed at getting the user firendlyness of the cmd line up to desktop standards smile.gif
vrejakti
It would be nice if quoting was the solution---(rant cut short)

Da_Blitz, grog, your suggestions worked! Thanks! It really was as simple as quoting "$IN". Looks like zsh won't be needed after all. happy.gif

I must have offset the quoting of my variables somewhere in previous scripts causing that type of quoting not to work, but it works as it should now.
Da_Blitz
it happens, the most common problem is the null argument (smile.gif) where one uses test or [ with a varible and forgets to qoute it, when a vaible gets passed to it it spits out a "wrong number of args" error and quits causing many headaces or when used with strins compares the first and second word because people forget that spaces seperate args

it happens and you learn to qoute all your varibles because it dosent hurt and gets rid of a problem or two

leasons learned the hard way wink.gif funny thing is its probelly a bug in zsh that made it work
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.