Edit as necessary, requires hsetroot, mplayer, and a working xvidix on your graphics card (for video).

# Set Wallpaper

# If $1 is not empty
if [ -n "$1" ] ; then
    case "$1" in
        # Grab last set wallpaper from file and store in $WALL
        WALL=`cat ~/desktop/lastwall`
        # Set $WALL from command line and write to file for future use
        echo $1 > ~/desktop/lastwall

    case "$WALL" in
            # $WALL is an image file, so use hsetroot to set it as the background
            echo Setting with hsetroot...
            hsetroot -full "$WALL"
            # $WALL is likely not an image, so attempt to play with mplayer
            echo Setting with mplayer...
            # Blank out background first
            hsetroot -solid "#000000"
            # "Play" background using colorkey
            sudo mplayer -fs -vo xvidix -rootwin -colorkey 000000 -quiet "$WALL" &

    # Return good
    exit 0
    echo Please specify an image or a video file to set, or use "last"

    # Return bad
    exit 1

To use, run setwallpaper.sh /path/to/image/or/video/file or setwallpaper.sh last

(Maybe) Related posts: