playing with playlists.no pun intended.

suppose you have a playlist that you always use to stream audio content, but at times it gets boring to always listen to the same sequence of songs. what if you wanted your stream to play a commercial after every 2 or so songs? what if you want to shuffle your songs at random every time the last song on the playlist is heard? what is you want the same for your commercials? the wait is over, “playlist manipulator v432.65″ is finally here, for free, for everyone, yadayada

I’ve been playing around with playlists lately. Sometimes I would want to play albums/songs in a non-sequential manner, doing that would be easy I’d just randomize the .m3u file everytime it’s done being played. But I want something more, I thought of fictitious commercials like the ones being played in GTA games (ingame, when you enter a vehicle you can listen to the radio which plays songs and commercials in between not to mention hilarious commentaries from DJs). So yeah, why not play them commercials in between songs like after every 2 or 3 songs. Do them in random too I say.

There’s a lot of ways to accomplish such task, here’s how I did it:

-=BEGIN SCRIPT=-

#!/bin/bash

staging=/usr/local/music/staging
ezstream=/usr/local/ezstream/bin/ezstream
cd /usr/local/music/playlists

while :
do

#remove old playlists
rm -f /usr/local/music/staging/*

#create playlist for individual album, just so you have one for future reference/use
#do this manually, copy created playlist(with .m3u extension) in /usr/local/music/playlists folder
#to create a fresh ordered playlist for each album:
#for i in `ls /usr/local/music/albums`;do find /usr/local/music/albums/$i -name *.mp3 \
#-print >/usr/local/music/playlists/$i.m3u;done;

#exclude commercials.m3u
cat *.m3u |grep -v commercials >$staging/songs.m3u

#randomize songs
/usr/local/bin/sort -R $staging/songs.m3u > $staging/random-songs.m3u

#randomize commercials
/usr/local/bin/sort -R commercials.m3u >$staging/random-commercials.m3u

#in some cases, depending on which distro you’re using, your “sort” command won’t have the
#-R (randomize) option. Just like in my case(centos 5.4). You have 2 options here:
#1) use ezstream -s option to randomize the playlsit
# ezstream -s songs.m3u >random-songs.m3u
#2) download the source for coreutils, compile then install. be sure not to overwrite your rpm
# coreutils’ package binaries unless you know what you’re doing. To do so, just use an install prefix when
# configuring ./configure –prefix=/usr/local

#merge random-commercials.m3u and random-songs.m3u
#such that 1 commercial is played after every 2 songs
awk ‘FNR==NR{
song[FNR]=$0;
next
}
{
print song[FNR+line];line++;
print song[FNR+line]
print $0
}’ $staging/random-songs.m3u $staging/random-commercials.m3u \
>/usr/local/music/playlist.m3u

#feed the playlist to ezstream, its config file is now set to stream the playlist only once.
$ezstream -c /usr/local/music/ezstream_mp3_conf

done

-=END SCRIPT=-

The script will loop infinitely, everytime it loops it creates a new randomized list of songs and commercials.
Sample data would be something like below:

/usr/local/music/playlists folder contains individual playlists of various albums, playlist for commercials is also in said folder:
albumA.m3u – is a playlist of albumA
sample lines would be:
/usr/local/music/albums/albumA/albumA-song1.mp3
/usr/local/music/albums/albumA/albumA-song2.mp3
albumB.m3u – is a playlist of albumB
sample lines:
/usr/local/music/albums/albumB/song1.mp3
/usr/local/music/albums/albumB/song2.mp3
commercials.m3u – playlist of commercials
/usr/local/music/albums/commercials/commercial1.mp3
/usr/local/music/albums/commercials/commercial2.mp3

Once randomized and merged, the playlist to be streamed would look similar to this:
/usr/local/music/albums/albumB/song2.mp3
/usr/local/music/albums/albumA/albumA-song1.mp3
/usr/local/music/albums/commercials/commercial2.mp3
/usr/local/music/albums/albumA/albumA-song1.mp3
/usr/local/music/albums/albumB/song1.mp3
/usr/local/music/albums/commercials/commercial1.mp3

The sequence would change everytime the script loops, it loops when ezstream is done playing the current list.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

One Response to playing with playlists.no pun intended.

  1. Mario Solis says:

    sorry for necroposting, would you mind giving me a hand on the script? im getting an error on the awk part, i got everything else ready, and by the way, you are the only one i ever saw that did it this way, and i just think its just the right way to do so. i hope you can reply :D

Leave a Reply

Your email address will not be published. Required fields are marked *

Security Code:

This site uses Akismet to reduce spam. Learn how your comment data is processed.