Puddle TV - A product of boredom and genius
So Gorilla.scw was finally running, and was happily playing hours and hours of music videos continuously. This wasn't enough. I had no one to talk to except my PC... and Gorilla. I decided that it was time that Gorilla started doing something more useful, and not just blindly play music videos for the rest of eternity. My knowledge of a useful scripting language called Perl was very scarce, which forced me to use the simpler, more painful style of programming... bash scripting. I planned to make a couple scripts which played a variety of media, based upon a schedule which I would put into place via the use of the crontab.
The crontab is a type of event scheduler for *NIX boxes. The scheduler is called at certain specified times, and the script or program that is specified is run in the background. This is super useful for what I was wanting to do. The schedule would be exactly that, a schedule for the channel, detailing times to start programs. At first, it sounds pretty straight forward, and honestly, at first it was, but as soon as you start demanding more and more of the scripting, it becomes amazingly complex and complicated. The first huge obstacle I came across was having new events start running without just closing whatever was playing, but rather, wait until they have finished and then play afterwards, simulating a queuing system. This queuing system gave rise to a number of different scripts, each in charge of playing a certain type of video ie. music videos or movies or TV series. The queuing system was achieved through lock files, which would be 'touched' when playing was active and removed when playing was inactive. At the sign of no playing lock, the script would be able to play, knowing that nothing else is playing already.
Originally, this solution was outstanding, it performed exactly as I predicted, and seemed to make the queuing system work out quite efficiently. Unfortunately, with this queuing simulation, came the need for a certail hierarchy between all the different scripts. For instance, what happens if a movie is queued to play, but music videos are already playing. Here, the movie would wait for the music videos to finish before playing, but how does the music video script know that there is an even more important script waiting for airtime? I achieved this through the use of a "priority request" flag, which was set whenever a script of higher stature requested airtime. This way, the music video script would keep checking the flag, waiting to see if another script requested airtime, and in the case of a request, the music video script would play out the video until the end and then kill itself, removing the playing lock file, and "giving way" to the "priority request" script.
This worked out awesomely. The hierarchy was: Movie > TV Series > Music Videos. Music videos were at the bottom of the pile, which was ideal, because whenever neither a TV series, nor a Movie was playing, music videos would fill the "white space". That way, PuddleTV would never be idling and something would always be showing.
Unfortunately, (like with all scripts) something kept on bugging out and there were times when dead locks would occur with a race condition ocurring between two active scripts. Both would be fighting for the playing lock whilst one held the priority request. Suddenly, situations kept occuring where PuddleTV would be in a dead lock state with nothing playing. I came up with an idea to create a "check_idle" script which, through the crontab, would be called every two minutes. The script would check for the existence of the playing lock and/or the priority request, and then check whether MPlayer was running. If no mplayer was running, then it would mean that two scripts are fighting, and there needs to be some kind of intervention. The intervention would be to remove the playing lock and/or the priority request flag. This would cause both scripts to try grab the locks again, and because of the hierarchy and because of alternate waiting times between trying to grab the locks, the dead lock would be corrected, and the script with the highest priority would be granted airtime.
Eventually, I found a comfortable setting where no more dead locks ocurred, and the system ran fine for days without ever freezing due to a race condition. The system was brilliant, and after deciding on a daily TV schedule, I built the corresponding crontab file and started the system rolling.
After a couple hours watching PuddleTV, I realised the one main thing missing from the channel, compared to real world public TV channels, was advertising. I found myself missing the adverts between music videos or movies, as well as the advertising for up coming shows or the movies that are to appear that day/week. The solution was right in front of me: I had to create advertising on PuddleTV and some kind of way to let the viewers know about upcoming shows.
The first advert that was installed into the system was an advert showing the PuddleTV daily schedule. This was achieved by making a standard JPEG with all the details of showtimes in it, and then using mplayer, create an MJPEG of the picture repeating for a couple seconds. The schedule was shown over two different pages, one for morning/lunch viewing and the other for afternoon/evening viewing. Another necessity in advertising a schedule is to have some backing music to it. I used Audacity to cut some samples of equivalent running times as the schedule and then using mplex, I multiplexed the rendered video with the sampled audio. The results were incredible.
Soon after this, I figured out a way to advertise the movies for that particular day. Every day at midnight, Gorilla would randomize the list of movies and select two from it. The names of these two movies would then be pasted onto a template, using the convert command. This enables you to place text onto an image without actually placing it yourself in a graphics studio. After pasting the name onto the template, mplayer would create the MJPEG and mplex would multiplex it with some sampled audio, and then spit out two different AVI's, one for the 10AM movie and the other for the 8PM movie.
It was all good and well having these awesome scripts which would create movies (during runtime) and prepare them for airing on PuddleTV but there was no system implemented yet to decide when to play them, and how often. I started thinking about ways to decide when it will be appropriate to play adverts... obviously you don't want them too often otherwise it will be just like normal TV, but at the same time, you want the schedule to be aired every once in a while to show people whats up next.
The main scheduling of adverts would take place during music videos, because these would be playing the most, and the broken up nature of a whole lot of music videos would make slotting adverts in quite easy. I decided that the schedule should be shown every +- 8 videos, and advertising for the day's movies would happen every 8+(random(10)) videos. This would institute a kind of entropy with the advertising and not make it too structured. Another really cool feature I built in during a very lonely night was a stats page. The stats page would show you the current temperature outside, as well as maximum, minimum and wind speeds. It would also display the date and time, and the current load averages of Gorilla. This was done by a simple 'wget' of the local weather page, and then some fancy footwork to extract the relevant values. The stats were then pasted onto a stats page template the same way that the upcoming movies were, using convert, mplayer and mplex. The stats page also was also shown at random, iirc, about every 8-15 videos. Whenever movies or series were playing, the schedule and upcoming movies would be screened before and after every movie/series. I used some amazing backing samples from African Dope Records and Cape Town 2AM, both are local South African albums.
The upcoming movie script also had to be able to check what the time was, so as to avoid playing the advert for the 10am movie at like 2pm that day (after the movie had actually played). This was done by just performing a 'date' command and then cutting the relevant data from it, to check what the hour of the day is.
The scripts are now keeping themselves up to date by running a regeneration script every midnight. The script rebuilds the listings of movies/musicvideos/series to ensure that newly added media is also put into the loop. The use of these listings of available media was critical in making sure that the system ran smoothly and quickly, and that wait times between media playing on PuddleTV was kept to a minimum. With this system, the viewer rarely experiences an idle period of no more than 3 seconds, and if there is a deadlock, the viewer will only wait for under 2 minutes before the check_idle script is called to fix the deadlock (although, if the system is running untouched, this will rarely, if ever, occur).
After running PuddleTV for a couple days and once everyone was back in the digs, I could start getting some feedback and recommended addons. The one major noticeable problem was that certain music videos kept being played more often than others. It is so difficult to create completely random values constantly, without finding some kind of repetition of or "favourite" value. It sounds silly, but it was happening. It was quite obvious that some videos would keep coming up, far more often than others. The solution for this was quite simple: I made the music video script add the name of the song that it chose to a running list. The list would only ever be 100 lines long. As soon as it is filled, and another music video is added, the video on the bottom of the file would drop off. Each time a music video is randomly selected, the script would check if that name exists in the list, if it did, it would randomize another song and then check again, until it found a song that hadn't been played in the last 100 music video slots. This system seemed to work efficiently, and because it only ever had to deal with text files with music video names within it, the randomization and reselection would happen extremely quickly and completely transparent to the viewer.
The system is still far from perfect, I still want to implement some more advertising (real adverts, like the ones around the net that have been banned from TV), as well as more TV series. I also plan to make it more generic and more able to be ported across different computers. By making path names and other specific variables unset inside the actual script, other people could start to use my scripts to make their own PuddleTV, by just supplying the necessary directories containing the relevant media.
** All media discussed above have been obtained legally.
3 Comments:
This will help you make it even cooler:
http://galleon.sourceforge.net/phpwiki/index.php/HomePage
8:29 pm
Well done!
[url=http://kbdgppqh.com/hxbp/ohiy.html]My homepage[/url] | [url=http://flwtmtcc.com/exzt/csng.html]Cool site[/url]
8:10 pm
Great work!
http://kbdgppqh.com/hxbp/ohiy.html | http://hftbtgog.com/dbvd/ujwf.html
8:10 pm
Post a Comment
<< Home