Author Topic: I Need Help Creating A M3u Playlist.  (Read 6491 times)

March 20, 2004, 01:02:52 PM
Read 6491 times

Mouse

  • Legacy Reserved
  • Skulk

  • Offline
  • *

  • 96
    • View Profile
    • Danger Mouse
So I have a Linux server with all of my mp3s. I want to beable to have a script that will create an updated winamp playlist every 5 hours. There is no real pracitlcal reason right now for me to create a new one every 5 hours since I rarely get new mp3s. The reason why I'm really doing this is because my Linux professor wants us to create a Linux script that does something every so often and have it run in the background and also have it start on startup.

So anway, in a winamp generated playlist files that are queued over a network look like this:

Quote
\\Tachikoma\Music\Anime\Ghost in the Shell\Innocence OST (320CBR)\GitS Innocence - 07 - Kugutsuuta aratayo ni kamutsudo hite.mp3
\\Tachikoma\Music\Anime\Ghost in the Shell\Innocence OST (320CBR)\GitS Innocence - 08 - The Doll House I.mp3
\\Tachikoma\Music\Anime\Ghost in the Shell\Innocence OST (320CBR)\GitS Innocence - 09 - The Doll House II.mp3
\\Tachikoma\Music\Anime\Ghost in the Shell\Innocence OST (320CBR)\GitS Innocence - 10 - Kugutsuuta kagirohi ha yomi ni mata muto.mp3
\\Tachikoma\Music\Anime\Ghost in the Shell\Innocence OST (320CBR)\GitS Innocence - 11 - Tohokami emi tame.mp3
\\Tachikoma\Music\Anime\Ghost in the Shell\Innocence OST (320CBR)\GitS Innocence - 12 - Follow Me.mp3
\\Tachikoma\Music\Rock\Godsmack - Awake.mp3
\\Tachikoma\Music\Rock\Godsmack - Get Up, Get Out.mp3
\\Tachikoma\Music\Rock\Godsmack - Voodoo.mp3
\\Tachikoma\Music\Rock\Godsmack - Whatever.mp3
\\Tachikoma\Music\Alternative\Goo Goo Dolls - Big Machine.mp3
\\Tachikoma\Music\Rock\Foo Fighters - Big Me.mp3
\\Tachikoma\Music\Rock\Foo Fighters - Everlong.MP3
\\Tachikoma\Music\Rock\Foo Fighters - Learn To Fly.mp3
\\Tachikoma\Music\Rock\Foo Fighters - Monkey Wrench.mp3
\\Tachikoma\Music\Rock\Foo Fighters - My Hero.mp3
\\Tachikoma\Music\Rock\Foo Fighters - Stacked Actors.mp3
\\Tachikoma\Music\Rock\Foo Fighters - Times Like These (Acoustic).mp3

Here's my script that will find all the mp3s on the server, put them into a file and then repeat every 5 hours:

Code: [Select]
#!/bin/bash
#
# Script will compile a list of all the mp3s in the sub directories and
# then compile a new winamp play list.

exec 1> mp3_list

while [ 1 eq 1]
do
set `date`
# Output list of mp3 files on server into a file.
find -iregex .*\.mp3 -fprintf mp3_list.$2.$3.$6 \\\\\\\\Tachikoma\\\\%P\\n
# Wait for 5 hours and repeat.
sleep 18000
done
exit 0

The script yeilds the following output:

Quote
\\Tachikoma\Techno/DJ Sammy/DJ Sammy - Sunlight.mp3
\\Tachikoma\Techno/DJ Sammy/DJ Sammy - The Boys of Summer.mp3
\\Tachikoma\Techno/DJ Sammy/DJ Sammy - Unbreakable.mp3
\\Tachikoma\Techno/DJ Sammy/DJ Sammy - You're My Angel.mp3
\\Tachikoma\Techno/DJ Mangoo/DJ Mangoo - Eurodancer.mp3
\\Tachikoma\Techno/DJ Mangoo/DJ Mangoo - Goodbye.mp3
\\Tachikoma\Techno/DJ Mangoo/DJ Mangoo - Inside You Part 1.mp3
\\Tachikoma\Techno/DJ Mangoo/DJ Mangoo - You are my fantasy.mp3
\\Tachikoma\Techno/DJ Encore/DJ Encore - I See Right Through To You  [Extendend Version].mp3
\\Tachikoma\Techno/DJ Encore/DJ Encore - I See Right Through To You.mp3
\\Tachikoma\Techno/DJ Boom Boom/Dj Boom Boom - Ecstasy.mp3
\\Tachikoma\Techno/DJ Boom Boom/DJ Boom Boom - Magic Orgasm (Club 69 Mix).mp3
\\Tachikoma\Techno/DJ Boom Boom/DJ Boom Boom - Up & Down(Remix).mp3
\\Tachikoma\Techno/Daniel Beddingfield/Daniel Beddingfield - Gotta Get Through This.mp3
\\Tachikoma\Techno/Daniel Beddingfield/Daniel Bedingfield - Gotta Get Thru This (Acoustic).mp3
\\Tachikoma\Techno/Aura/Aura - Endless.mp3
\\Tachikoma\Techno/Aura/Aura - The Source of Trance - Cry (Remix).mp3
\\Tachikoma\Techno/Aura/Aura - The Source of Trance - Hold my hand.mp3

Now if you compair the output from the script and the winamp generated you'll notice that the slashes in the script output differ from the winamp generated. From what I can tell from the find documentation is that there is no string subsitution. :( So I was wondering if any one knows of a way to do string subsitution within a script or if there is a command that I can use in order to complete what I need to do.

There also might be a slight complication with the newline character that is placed at the end of each line. I dont think that'll be sucha huge problem, and if it is I'll cross that bridge when I come to it.

Inanycase any useful information, feedback or insightful comments are apprecaited.


March 20, 2004, 07:04:44 PM
Reply #1

devicenull

  • Legacy Admin
  • Marine

  • Offline
  • ****

  • 904
    • View Profile
Why are you creating a *winamp* playlist for linux? :p

I know theres a command that lets you replace things like that..
anyway, try this instead find \Tachikoma\Music\ | grep *.mp3 > m3u.m3u
I cant quite remember the output of find.. It *may* give paths..

if not,
find \ | grep *.mp3 > m3u.m3u
will give you paths.. I'm sure of that

March 20, 2004, 11:12:39 PM
Reply #2

Uranium - 235

  • Legacy Admin
  • Commander

  • Offline
  • ******

  • 1797
    • View Profile
Why are the slashes backwards AND forwards?

March 20, 2004, 11:37:41 PM
Reply #3

Mouse

  • Legacy Reserved
  • Skulk

  • Offline
  • *

  • 96
    • View Profile
    • Danger Mouse
Quote
Why are you creating a *winamp* playlist for linux? :p

I know theres a command that lets you replace things like that..
anyway, try this instead find \Tachikoma\Music\ | grep *.mp3 > m3u.m3u
I cant quite remember the output of find.. It *may* give paths..

if not,
find \ | grep *.mp3 > m3u.m3u
will give you paths.. I'm sure of that
I'm not creating a winamp playlist for linux. I'm creating a winamp playlist using linux for use on a Windows system.  <_< it's ok null, we still love you <3

No matter how you toss the weighted die you're still gonna come up with 6's. Both commands will give me some kinda error or just return no results. Reasons being that linux file structure use "/" to seprate the file directories as apposed to the "\" that windows uses. And it seems like you're not using the grep command correctly. In anycase I'd still have to use some command or script to change "/" to "\". From what I can tell your find command will do the same thing mine does but with out the "\\Tachikoma" which is the NetBEUI identifier in the prefix.

I did a google search on string subsitution and it came up with a command called awk or gawk, it's gnu counterpart. I can't for the life of me figure how how the heck to use it. :(

[edit] I tried both commands fixing them a little to try and get them to work. Didn't happen.  :ph34r:  Still at square one. Please advise.  :(  :help: [/edit]
« Last Edit: March 20, 2004, 11:41:27 PM by Mouse »


March 21, 2004, 03:15:28 AM
Reply #4

Leaderz0rz

  • Legacy Reserved
  • Fade

  • Offline
  • **

  • 421
    • View Profile
    • http://

March 21, 2004, 11:31:33 AM
Reply #5

devicenull

  • Legacy Admin
  • Marine

  • Offline
  • ****

  • 904
    • View Profile
Try the new m3u.. It will probably work - windows seems to treat \ as equal to /

March 21, 2004, 01:53:20 PM
Reply #6

Mouse

  • Legacy Reserved
  • Skulk

  • Offline
  • *

  • 96
    • View Profile
    • Danger Mouse
Quote
Try the new m3u.. It will probably work - windows seems to treat \ as equal to /
Well I'll be darned... It bloody worked!!!  :o  :blink: Now if only I had known that this worked before...  :angry:

Quote
Why are the slashes backwards AND forwards?
The reason is because when I used the fprintf option and that was formating the output to look like: "\\Tachikoma\" followed by the path and file name. With linux the way that the file structure is describes is the opposite of windows, "/home/user/documents". When the find command found a mp3 file it didn't edit the path it just slapped it into the output. So when the find command was finished I had a bunch a files in a wierd hybrid file structure, which oddly enough didn't seem to matter at all...

[edit] Anyway, does anyone know why winamp accepted the "/"?  :huh: [/edit]
« Last Edit: March 21, 2004, 01:58:38 PM by Mouse »


March 21, 2004, 02:13:24 PM
Reply #7

Mouse

  • Legacy Reserved
  • Skulk

  • Offline
  • *

  • 96
    • View Profile
    • Danger Mouse
Lets assume that the script above was named Mp3_Archive and that I have it setup so that the Mp3_Archive script starts in the background at startup. What if I wanted to beable to type: Mp3_Archive status and the script would give back the last time it wrote the m3u file and how many files were in it. Is there something speical I need to do in order to bring the program back out of the background, have it preform the task and then go back into the background to continue what it was doing?


March 21, 2004, 07:23:47 PM
Reply #8

devicenull

  • Legacy Admin
  • Marine

  • Offline
  • ****

  • 904
    • View Profile
It probably accepted it because windows is pretty much IE.. meaning it has to accept both slashes - its also easier for users, they dont have to specifiy which slash.

so.. you either need to set the number of files retrieved and time of last run as variables, or in a file
I *think* for variables you do export Varname "files"

so you could do..
export numfiles "some way to get # of files"
export lastrun "the date"
Then probably
echo "Mp3: " & numfiles & " files added to playlist at " & lastrun;
You just have to figure out setting variables and getting the number of files - the easy way is probably just get the number of lines in the file - then you dont even have to set a variable

March 21, 2004, 09:37:18 PM
Reply #9

Mouse

  • Legacy Reserved
  • Skulk

  • Offline
  • *

  • 96
    • View Profile
    • Danger Mouse
Quote
so.. you either need to set the number of files retrieved and time of last run as variables, or in a file
I *think* for variables you do export Varname "files"

so you could do..
export numfiles "some way to get # of files"
export lastrun "the date"
Then probably
echo "Mp3: " & numfiles & " files added to playlist at " & lastrun;
You just have to figure out setting variables and getting the number of files - the easy way is probably just get the number of lines in the file - then you dont even have to set a variable
Yea, but that isn't the hard part. To find the number of mp3s in the m3u file all i have to do is:

Code: [Select]
grep -c .*\.mp3$ m3u.m3u
As for when the last time it was run all I have to do is return %4. Because `date` was set just before the file was created.

As mentioned above the script is in a loop. How do I interupt the loop, make it so that it preforms the task and doesn't start another instance of the script?

[edit] I just remember that my professor wanted us to use the TRAP command in order to interrupt the script... but from the 2 text book and some google searches for some more information I can't seem to figure out how i'm supposed to get it to work like he wants us to :( [/edit]
« Last Edit: March 21, 2004, 11:59:57 PM by Mouse »


March 22, 2004, 12:47:18 PM
Reply #10

devicenull

  • Legacy Admin
  • Marine

  • Offline
  • ****

  • 904
    • View Profile
Why is that in a loop?
Why not just use cron?  It lets you add tasks to be preformed at certain intervals..
I don't have a clue as to how to interrupt that loop

March 23, 2004, 01:12:34 PM
Reply #11

Mouse

  • Legacy Reserved
  • Skulk

  • Offline
  • *

  • 96
    • View Profile
    • Danger Mouse
Quote
Why is that in a loop?
Why not just use cron?  It lets you add tasks to be preformed at certain intervals..
I don't have a clue as to how to interrupt that loop
I have no clue... I didnt know there was sucha command... The example that he showed us in class used a loop... and he ran it in the background... does that command leave the script running or can you use it via rc*.d?


March 23, 2004, 02:32:27 PM
Reply #12

devicenull

  • Legacy Admin
  • Marine

  • Offline
  • ****

  • 904
    • View Profile
if you used cron, you would have to remove the loop - cron would execute it whenever - no loops needed
It runs it - then lets it finish by itself

March 27, 2004, 03:54:38 PM
Reply #13

Mouse

  • Legacy Reserved
  • Skulk

  • Offline
  • *

  • 96
    • View Profile
    • Danger Mouse
This is the full extent of what i have right now...

Quote
#!/bin/bash
#
# Script will compile a list of all mp3s in it's workin directories and
# sub-directories into a winamp m3u playlist to be shared in a Samba
# directory share.
#


sArgument=$1

if [ "$sArgument" = "status" ]
then

# echos the last time the file was writen to and the
# the number of files that were found.

   echo "The last write was @ "$sLastWrite" with "`grep -c .*\.mp3$ mp3_list.Mar.20.2004.m3u`" files."
elif [ "$sArgument" = "start" ]
then

# If the fauxd.pid file already exists then the script
# must be running. So if the file is already there then
# there is no need to start another instance of the script.

   if [ -e fauxd.pid ]
   then
  echo "Can not start. Script already running."
  exit 0
   fi
   [span style=\'color:blue\']# Gets the pid of the script and stores it into the fauxd.pid
   # temp file.

   pidd=$$
   echo $pidd > fauxd.pid
elif [ "$sArgument" = "end" -o "$sArgument" = "stop" ]
then[/span]
# Use the pid stored in the fauxd.pid file to kill the script
# process.

   kill 'fauxd.pid'
# Variable clean up.
   unset argument
   unset sLastWrite
   unset pidd
#   rm /tmp/fauxd.pid
   exit 0
elif [ "$sArgument" != "end" -o "$sArgument" != "start" -o "$sArgument" != "status" -o "$sArgument" = "stop" ]
then

# Misc Error handling...
   echo "Correct usage: fauxd <start|stop|status>"
   exit 0
else
   echo "Blarg!!! Some sorta error that I wasn't able to forsee! Mybad."
   exit 1
fi

while [ true ]
do

# Get the date and time for the file name and the last time
# that the file was writen to.

   set `date`
   sLastWrite=$4
   export sLastWrite

# Search for the mp3 files and place the sorted results
# in a file unique to the day that it was created.

   find -iregex .*\.mp3 -fprintf mp3_list.$2.$3.$6.m3u \\\\\\\\Tachikoma\\\\Music\\\\::%P\\n | sort -fd
# Wait 5 hours and then repeat.
   sleep 18000
done


The only problem that I'm having now is killing the process by using the .pid file. I tried running kill fauxd.pid' or kill "fauxd.pid" or kill `fauxd.pid` at the command line and none of them worked!!  :angry: any help would be appreacated.  <3

[edit]  I get the error: bash: kill: fauxd.pid: no such pid :ph34r:  [/edit]

[edit2] Did some cleanup and tried to make the code a little easier to read and added some more comments to the script [/edit2]
« Last Edit: March 27, 2004, 04:53:37 PM by Mouse »


March 27, 2004, 04:01:41 PM
Reply #14

Saturn

  • Lerker

  • Offline

  • 1
    • View Profile
Linux and Unix lack the more robust error checking that Windows has.  In Windows, both "/" and "\" will work fine for locating files on the localhost, etc.    Just like you can type upper/lower case file names and folders and it does not matter.

In both Linux and Unix, the user is expected to be somewhat knowledgeable and mindful of the details (anal Retentive is more like it).  Personally, I'd love to see a kernal that supported Windows style error checking for us Window using Linux users, but what good would it do?  I'm afraid I can't command line compile anything in Linux having grown up in windows! rofl.  Point being, you have to be exact with the case and appropriate slashes in Linux and Unix.  I believe you can enable a tab-completion feature in some Linux builds.  Start typing a name of a file within a directory and hit tab and it will give you the first match in alphabetical order.  Hit tab again and you will get the second, and so on.  This is a wonderful feature for those files with eighty-gazillion characters such.

As for your winamp thread, you may want to add something like the below code so you don't have to Kill/Halt/Terminate ID your script to turn it off.  Courtesy of pages 420-421 of the Red Hat Linux Bible 9.0.

Code: [Select]
# ! /bin/bash
#
Count=1
while :
do
  echo -n "$Count  Continue? (y/n): "
  read ANS
  case "$ANS" in
  y|Y)  break
      ;;
  esac
  let Count+=1
done
exit 0

Quoted From Text:
The colon (:) on the fourth line does absolutely nothing.  It functions only as a placeholder, and allows the while loop to repeat until a y or Y is entered (causing the break to exit the loop).  The Count variable keeps track of the number of times the loop has run.  The -n option to echo suppresses the printing the new line and positions the cursor just beyond the Continue? (y/n): prompt to wait for input.
« Last Edit: March 27, 2004, 04:05:30 PM by Saturn »

April 12, 2004, 12:24:24 PM
Reply #15

Mouse

  • Legacy Reserved
  • Skulk

  • Offline
  • *

  • 96
    • View Profile
    • Danger Mouse
I think I've got it running like I want it to except for one thing, running it as a daemon. I know I need to have a link to the script in a /etc/rc.d/rc[0-6].d/ directory with a prefix of S and K. Is there anything else I need to do anything speical to the script in order for it to work other then having a start and stop functions?