[Bash] [Tutorial] Parsing Command-Line Options - Programming On Unix

Users browsing this thread: 1 Guest(s)
desyncr
Members
Thanks for the tutorial! Here is a little script I made as I followed your instructions:

PHP Code:
#!/bin/bash
preview=0           # Make a preview only
start=0:00:00       # Start position
endpos=5            # End position
fps=13              # Frame per second. The more the better.
output=movie.gif    # Gif file name
scale=240:180       # Gif scale
video=              # Video file name
test=0              # Wether output the command or run it

# mplayer filename [Name of video file]-ao null
#       -ss 0:04:15 [Startposition h:mm:ss]
#       -endpos 15 [duration of gif]
#       -vo gif89a:fps=13 [frames per second]:output=animated.gif [Name of ouput file]
#       -vf scale=240:180 [Dimensions of gif file]
#
# mplayer -ao null -ss 0:03:33 [Startposition h:mm:ss]
#       -endpos 15 [duration of gif] filename.mpg [Name of video file]
#
while getopts v:s:e:f:o:c:pt opt; do
    case ${
optin
        v
)
            
video=${OPTARG}
            if [[ ! -
e $video ]]; then
                
echo "Can\'t access video: ${video}"
                
exit 1
            fi
            
;;
        
s)
            
start=${OPTARG}
            ;;
        
e)
            
endpos=${OPTARG}
            ;;
        
f)
            
fps=${OPTARG}
            ;;
        
o)
            
out=${OPTARG}
            ;;
        
c)
            
scale=${OPTARG}
            ;;
        
p)
            
preview=1
            
;;
        
t)
            
test=1
            
;;
        \?)
            echo 
'Missing argument'
            
exit 1
            
;;
    
esac
done
if [[ $video == '' ]]; then
    
echo "Usage ${0} -v movie.avi"
    
exit 1
fi

cmd
="mplayer ${video} -ao null -ss ${start} -endpos ${endpos}"
if (($preview == 0)); then
    cmd
="$cmd -vo gif89a:fps=${fps}:output=${output} -vf scale=${scale}"
fi

if (($test)); then
    
echo $cmd
else
    
exec $cmd
fi 


Messages In This Thread
RE: [Bash] [Tutorial] Parsing Command-Line Options - by desyncr - 21-11-2013, 02:09 AM