Monday, January 23, 2012

Player videos as a Gnome-screensavers

I have a collection of short interesting (to me anyway) videos that I've always wanted to play as a screen saver without audio. I use the gnome desktop in Fedora which uses gnome-screensaver. Search around on the internet I found this very useful post on Ubuntu's forum.

I have made some modifications. Firstly, to use a single mplayer line with the shuffle command to the full collection. Secondly, to kill mplayer and all of its child threads whenever gnome-screensaver exits (otherwise, you are left with many running mplayers without a window id to feed the video output to). The second part was a bit tricky to do properly since killing the parent mplayer did not kill all the child threads (probably a bug in mplayer). So here is the code:

 #! /bin/bash  
 # Movie screensaver code based on  
 # http://ubuntuforums.org/showthread.php?t=1368224  
 # modified January 2012  
 ## path to video  
 ### USER MODIFY #####  
 # Modify this to add the directories with the videos you want played.  
 VIDEO=( "/mnt/Storage1/Video_Screensavers/*" \  
     "/mnt/Storage1/Videos/RSA_Animate/*" )  
 #####################  
 if [ ! -z $XSCREENSAVER_WINDOW ]; then  
      # allow this script to run as a standalone without gnome-screensaver  
      WINDOW="-wid $XSCREENSAVER_WINDOW"  
 fi  
 ## 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 $WINDOW -nostop-xscreensaver -fs -really-quiet -shuffle"  
 ## we handle SIGTERM and SIGINT here to kill the child  
 ## if active then quit.  
 function ex {  
   pkill -TERM -P $CPID mplayer  
   kill -s 9 $CPID  
   exit 0  
 }  
 trap ex SIGINT SIGTERM  
 mplayer $MPLAYERARGS -loop 0 ${VIDEO[*]} < /dev/null &  
 CPID=$!  
 wait $CPID  

Copy this script (called movie.sh) to /usr/libexec/xscreensaver/ (in Fedora). Add xscreensaver-movie.desktop to /usr/share/applications/screensavers/.

 [Desktop Entry]  
 Encoding=UTF-8  
 Name=Movie  
 Comment=Plays Videos  
 TryExec=/usr/libexec/xscreensaver/movie.sh  
 Exec=/usr/libexec/xscreensaver/movie.sh  
 StartupNotify=false  
 Terminal=false  
 Type=Application  
 Categories=GNOME;Screensaver;  
 OnlyShowIn=GNOME;  

Now my process list is free of rogue mplayer processes.

No comments:

Post a Comment