LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   build a playlist with a script :) (https://www.linuxquestions.org/questions/linux-newbie-8/build-a-playlist-with-a-script-738103/)

wademac 07-06-2009 12:03 PM

build a playlist with a script :)
 
Hello All,

I would like to build a playlist of over 500 songs but I dont see anything online that will construct this with a program the playlist format is xspf for jw flash player.

format of the song are:

Band - Album - 01 - Song1.mp3
Band - Album - 02 - Song2.mp3
Other Band - Other Album - 01 - Other Song.mp3
Other Band - Other Album - 02 - Other Song two.mp3
etc..

File layout for the playlist is

<playlist version="1" xmlns="http://xspf.org/ns/0/">
<title>playlist</title>
<tracklist>

<track>
<title>SONG</title>
<annotation>BAND</annotation>
<location>FILE</location>
</track>

</tracklist>
</playlist>


so if I had a script that could basically rip apart the list of songs and build this playlist that would be grand!

rn_ 07-06-2009 12:50 PM

here's a small shell script that might help. this can probably be done more elegantly in perl; too bad i don't like perl that much :); but perhaps someone else on the list might be able to help.

place this script in a file (mkplaylist.sh for example; and remember to 'chmod +x' it). you'll have to edit it to provide the path to your mp3 files or you can make that a command-line parameter that you pass to the script.

HTH.
-RN.

<code>

echo "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">"
echo "<title>playlist</title>"
echo "<tracklist>"
find /path/to/mp3files -type f | while read line
do
FN=`basename "$line"`
SONG=`echo $FN | cut -d- -f4 | sed 's/ //g;s/.mp3//'`
BAND=`echo $FN | cut -d- -f1 | sed 's/ //g'`
echo "<track>"
echo "<title>$SONG</title>"
echo "<annotation>$BAND</annotation>"
echo "<location>$line</location>"
echo "</track>"
done
echo "</tracklist>"
echo "</playlist>"

</code>

sarin 07-06-2009 01:10 PM

Well, it will be difficult to do it without knowing the exact delimiter and which field corresponds to which. Anyway, my already weak perl scripting skills were getting rusty. So I decided to try it.

<Warning> This is a 10 minutes script and might have errors</warning>

Assumed that your file names are present in list.txt

Code:

#!/usr/bin/perl

open FILE, "<", "list.txt" or die;

print "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">
<title>playlist</title>
<tracklist>
";

while(<FILE>) {
        chomp;
        @info=split(/-/);
        $num=@info;
        if ( $num == 3 ) {
                print "<track>\n<title>@info[2]</title>\n<annotation>$info[0]</annotation>\n<location>$_</location>\n</track>";
        }
        if ( $num == 4 ) {
                print "<track>\n<title>@info[3]</title>\n<annotation>$info[0]</annotation>\n<location>$_</location>\n</track>";
        }
}

print "
</tracklist>
</playlist>";


wademac 07-06-2009 01:59 PM

tried the perl script and got:

/mkplaylist2.sh: line 3: open: command not found
./mkplaylist2.sh: line 8: print: command not found
./mkplaylist2.sh: line 10: syntax error near unexpected token `)'
./mkplaylist2.sh: line 10: `while(<FILE>) {'

chrism01 07-06-2009 06:27 PM

Sounds like you tried to run a perl script as a shell script, hence the syntax errors.

Given his script above, just check where your perl is installed and adjust the

#!/usr/bin/perl

line as needed.

Save it as mkplaylist.pl and run as ./mkplaylist.pl (assuming rx perms)

jschiwal 07-06-2009 06:50 PM

Look in the bash info manual section on HERE documents.

You could have in a loop something like:
Code:

cat >>playlist <<EOP
<track>
  <title>$SONG</title>
  <annotation>$BAND</annotation>
  <location>$FILE</location>
</track>
EOP


wademac 07-07-2009 06:50 AM

Quote:

Originally Posted by chrism01 (Post 3599022)
Sounds like you tried to run a perl script as a shell script, hence the syntax errors.

Given his script above, just check where your perl is installed and adjust the

#!/usr/bin/perl

line as needed.

Save it as mkplaylist.pl and run as ./mkplaylist.pl (assuming rx perms)

I had a space at the first of my perl line of code :( DUH!

Worked fine after :)

wademac 07-14-2009 01:48 PM

Thanks GUYS (gals) for the help you made this happen for me! ROCK AND ROLLA :)


All times are GMT -5. The time now is 05:28 AM.