You are not logged in.

#1 2008-02-15 18:07:58

rocktorrentz
Member
From: Southampton, England
Registered: 2007-08-05
Posts: 141

Simple Mencoder Bash Script

I have an old PC that I use to play videos using mplayer. However, for some reason the graphics card can't handle a width resolution of more than 768 and I have a lot of hi-def videos. I currently have a .bashrc alias that converts the file and then I play it with mplayer using a second command (all over ssh of course :-P) but I would really like to have it all automated so I can type "play file.avi" and it'll convert and then play the file. Here are the commands I currently run:

mencoder [file] -ovc lavc -oac pcm -vf scale=768:432 -o [outputfile]
DISPLAY=:0 mplayer -cache 16384 -fs

Does anyone have a similar script that they can share or help me write one that does this?

Thanks
rocktorrentz

Offline

#2 2008-02-15 19:00:40

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Simple Mencoder Bash Script

Something like this?

#!/bin/sh
extension=`echo "$1" | awk -F . '{print $NF}'`
scaled="${1%.$extension}_scaled.$extension"
if [[ -f "$scaled" ]]
then
    DISPLAY=:0 mplayer -cache 16384 -fs "$scaled"
else
    mencoder "$1" -ovc lavc -oac pcm -vf scale=768:432 -o "$scaled"
    DISPLAY=:0 mplayer -cache 16384 -fs "$scaled"
fi

The script must be called with an argument which is the file (ie. play.sh file.avi).  I haven't tried it over ssh but it seems to work locally.

EDIT: Version 2 of the script -- now if a converted file exists (from a previous run) the script will just play it without converting.

Last edited by fwojciec (2008-02-15 19:12:40)

Offline

#3 2008-02-15 19:22:05

rocktorrentz
Member
From: Southampton, England
Registered: 2007-08-05
Posts: 141

Re: Simple Mencoder Bash Script

Awesome big_smile That's exactly what I needed. I really need to learn bash or python.

Offline

#4 2008-02-15 19:23:28

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Simple Mencoder Bash Script

Here's another version -- this one doesn't need awk -- sorry, I'm learning new things here tongue

#!/bin/sh
extension="${1#*.}"
scaled="${1%.$extension}_scaled.$extension"
if [[ -f "$scaled" ]]
then
    DISPLAY=:0 mplayer -cache 16384 -fs "$scaled"
else
    mencoder "$1" -ovc lavc -oac pcm -vf scale=768:432 -o "$scaled"
    DISPLAY=:0 mplayer -cache 16384 -fs "$scaled"
fi

Offline

#5 2008-02-15 19:47:04

rocktorrentz
Member
From: Southampton, England
Registered: 2007-08-05
Posts: 141

Re: Simple Mencoder Bash Script

I've modified it a bit so it saves to my home folder instead of back to my file server and removed the cache options. Thanks big_smile I'll no doubt use it as a template for future conversion scripts.

Offline

#6 2008-02-15 22:53:13

fwojciec
Member
Registered: 2007-05-20
Posts: 1,411

Re: Simple Mencoder Bash Script

Since I'm only learning bash scripting at the moment I took this as an opportunity to experiment with some things.  Here is a maxi (probably an overkill) version of the script.  It allows you to specify the directory where you want the converted file saved and after playback it asks if you want to delete the converted file or not.  It also allows you to specify mplayer and mencoder options to be used for conversion and playback.  I'm not sure if you'll find it useful, but I'll post it anyways.  If anyone has any comments or criticisms please let me know.

#!/bin/sh

# ****Configuration****

# Enter the save directory here; if there is no value here the converted file
# will be saved in the source directory.  The correct format is "/aaa/bbb" --
# leave the final slash out.  Other options should be self-explanatory.

SAVEDIR="" 
MENCODER_OPTIONS="-ovc lavc -oac pcm -vf scale=384:216"
MPLAYER_OPTIONS="-fs -ao alsa"

# ****End of configuration****

# Other variables
EXTENSION="${1##*.}"
SCALED="${1%.$EXTENSION}_scaled.$EXTENSION"
SCALED_SAVEDIR="$SAVEDIR/`basename "$SCALED"`"

# Check if the SAVEDIR exists
if [ ! -z $SAVEDIR ] && [ ! -d "$SAVEDIR" ]
then
    echo "$SAVEDIR does not exist or is not a directory.  Exiting...";
    exit 1
fi

# Play if converted file already exists, otherwise play after converting
if [ -z "$SAVEDIR" ]
then
    if [ -f "$SCALED" ]
    then
        DISPLAY=:0 mplayer $MPLAYER_OPTIONS "$SCALED"
    else
        mencoder "$1" $MENCODER_OPTIONS -o "$SCALED"
        DISPLAY=:0 mplayer $MPLAYER_OPTIONS "$SCALED"
    fi
else
    if [ -f "$SCALED_SAVEDIR" ]
    then
        DISPLAY=:0 mplayer $MPLAYER_OPTIONS "$SCALED_SAVEDIR"
    else
        mencoder "$1" $MENCODER_OPTIONS -o "$SCALED_SAVEDIR"
        DISPLAY=:0 mplayer $MPLAYER_OPTIONS "$SCALED_SAVEDIR"
    fi
fi

# After playback is done ask if the file should be deleted
while true
do
    echo -n "Would you like to delete the converted file? (y or n) "
    read CONFIRM
    case $CONFIRM in
        y|Y|YES|yes|Yes)
        break
        ;;
        n|N|no|NO|No)
        echo "You entered \"$CONFIRM\" -- the converted file will not be deleted."
        exit
        ;;
        *)
        echo "Please enter only y or n"
        ;;
esac
done

# Option to delete file was selected
echo "You entered \"$CONFIRM\" -- deleting the converted file."
if [ -z "$SAVEDIR" ]
    then
        rm "$SCALED"
    else
        rm "$SCALED_SAVEDIR"
fi

Offline

Board footer

Powered by FluxBB