Results 1 to 10 of 30

Thread: HOWTO: Play Videos as Screensaver

Threaded View

  1. #1
    Join Date
    Oct 2006
    Beans
    9

    Smile HOWTO: Play Videos as Screensaver

    Recently I wanted to play a video as my screensaver. Thinking this would be a simple job, I set about searching. After a little time I could not find anything about using gnome-screensaver to play videos. But this has been done with xscreensaver, so I set about lifting the solution to gnome-screensaver.

    What follows was adapted from the electric sheep project to use MPlayer to render the screensaver. Two files need to be created, one .desktop that is used by the gnome-screensaver and the other is a script to select a video launch MPlayer.

    Step 1)
    Code:
    sudo apt-get install mplayer
    Its a good idea to test mplayer on a video file,
    Code:
    mplayer yourvideo.file
    just to make sure it can render well enough. If there are problems it could be the case that a different video driver is required, specified with the -vo command line argument. See MPlayer doc for more info.

    Step 2)
    Create a new file in /usr/share/applications/screensavers/ called movie.desktop,
    Code:
    sudo touch /usr/share/applications/screensavers/movie.desktop
    Then as root enter into movie.desktop
    Code:
    [Desktop Entry]
    Encoding=UTF-8
    Name=Movie
    Comment=Plays Videos
    TryExec=movie.sh
    Exec=movie.sh
    StartupNotify=false
    Terminal=false
    Type=Application
    Categories=Screensaver;
    OnlyShowIn=GNOME;
    This will be used by gnome-screensaver and specifies that the script movie.sh should be run when the screensaver is launched.

    Step 3) - movie.sh
    There are a few choice that can be made, the script I now present has a list of video files and randomly chooses one, plays it then randomly selects another and continues. It should be fairly simple to modify to suit your own purposes.

    Create the script,
    Code:
    sudo touch /usr/lib/gnome-screensaver/gnome-screensaver/movie.sh
    make it executable
    Code:
    sudo chmod 755 /usr/lib/gnome-screensaver/gnome-screensaver/movie.sh
    enter the following
    NOTE: See bootch's comment below for a better way of writing this script.
    Code:
    #! /bin/bash
    
    ## setup MPlayer aruments, remove -nosound if you want the video
    ## to play sound. If you have to specify the video driver to use
    ## then add that to the list
    MPLAYERARGS="-nosound -nolirc -wid $XSCREENSAVER_WINDOW -nostop-xscreensaver -fs -really-quiet"
    
    ## add videos to this list, one will be chosen and played, note
    ## that wild cards can be used here
    VIDEOS=( "/path/to/vid.avi" 
            /path/with/wildcard/*avi)
    
    ## child pid, no need to modify
    CPID=0
    
    ## we handle SIGTERM and SIGINT here to kill the child
    ## if active then quit.
    function ex {
        if [ $CPID -gt 0 ] 
        then
    	kill -s 9 $CPID
        fi
        exit 0
    }
    trap ex SIGINT SIGTERM
    
    ## loop while video list is not empty
    while [ ${#VIDEOS[*]} -gt 0 ] 
    do
        ## pick a random video
        VIDEO=${VIDEOS["$RANDOM % ${#VIDEOS[*]}"]}
    
        ## play video
        mplayer $MPLAYERARGS "$VIDEO" &
        CPID=$!
        ## wait for video to finish
        wait $CPID
        ## if things went wrong then exit
        if [ $? -ne 0 ]
        then
    	exit 1
        fi
        CPID=0
    done
    Please be careful when copying the code that "#! /bin/bash" is at the beginning of the file (no white space).

    This script might look complicated, but most of the code is taken up with killing mplayer when the script is killed. This could have been simplified by the use of exec but then to get a different video the screensaver has to be restarted. In a great simplification of the above script would have only one video, which would loop. See
    Code:
    #! /bin/bash
    
    ## setup MPlayer aruments, remove -nosound if you want the video
    ## to play sound. If you have to specify the video driver to use
    ## then add that to the list
    MPLAYERARGS="-nosound -nolirc -wid $XSCREENSAVER_WINDOW -nostop-xscreensaver -fs -really-quiet"
    
    ## path to video
    VIDEO=/path/to/vid
    
    exec mplayer $MPLAYERARGS -loop 0 "$VIDEO"
    Step 4)
    Configure the script by entering videos into the list VIDEOS, to understand the syntax of the list see bash arrays (http://tldp.org/LDP/abs/html/arrays.html). Make sure that quotes are not used around paths with wild cards as this stops shell expansion.

    Finally go System -> Preferences -> Screensaver and select Movie from the list. Your movie should start playing in the preview window, if not make sure the above steps were followed.

    All Done.


    To test the script remove "-wid $XSCREENSAVER_WINDOW" and "-really-quiet" from the arguments, and run the script from a terminal to see error messages.
    Last edited by kazza; March 12th, 2010 at 09:46 AM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •