Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Playlist parser for mpg123

by slugman (Novice)
on Aug 02, 2013 at 13:48 UTC ( #1047599=CUFP: print w/ replies, xml ) Need Help??

I know mpg123 has the ability to play files according to a playlist, however after reading the manpage I wasn't ever really able to figure it out. I became fed up with have to play my songs individually one after the other in the terminal, and when my mpg123 * became insufficient I decided to write something that would achieve playlist parsing.

So, I wrote Music_System. Yes, I know its misleading, since it doesn't actually play anything. Really, it just uses regex to parse a text file and plays the files accordingly. Then, it uses the find utility to find the appropriate files and plays them accordingly via mpg123.

To do list: Long Term: a. I just recently discovered the SDL library modules on cpan. I would like to use the audio player to make this an independant audio player capable of playing more than mpeg layers 1, 2, and 3 -- mpg123's primary purpose. Short Term: a. Now that I have mastered ARGV use, I will make use of arguments to be able to specify the music directory to search in, as opposed to hardcoding it as I have currently done. I can implement this easily, and should have that done in a day or two. b. Also, when I used windows I was hooked on foobar2000. I really liked the internal DB it created to sort your music through. Now, I know plenty of players already do this, but I think it would make a good experiment to be able to parse the directory and create an internal hash db based off of the tags. Not quite sure how I'll accomplish this though, since the tags will have to be multi-dimensional. May just stick to multi dimensional arrays. This was posted for my friends to review, and also for dams at Insomnia (to prove I am actually doing something :p ).

Please don't laugh!

#!/usr/bin/perl # # # Music System # Diego Z Pineda # July 2013 # dzpineda86@gmail.com use strict; use warnings; sub nl () { print "\n"; print "\n" } sub clrw { chomp $_[0]; $_[0] =~ s/ +$// } my $file = $ARGV[0]; open (my $in, "<", "$file") or die "Can't open input-file: $!"; my @array_in; my @array_out; my @dpath; while (<$in>) { push @array_in, $_ } my $aii = $#array_in; my $iai = $aii + 1; for (my $i=0; $i<$iai; $i++) { my $t1 = "$array_in[$i]"; if ($t1 =~ s/^\# [0-9]+. //) { chomp $t1; $t1 =~ s/ $//; push @array_out, $t1 }} # chopmping all elements being pushed to array_out # will allow shell command to be evaluated. my $aoi = $#array_out; my $iao = $aoi + 1; my $p_dir = '/home/slug/Music'; my $m_dir; my $m_dir_pre = "$array_in[1]"; if ($m_dir_pre =~ s/^\# //) { $m_dir = $m_dir_pre } my $dir = "$p_dir/$m_dir"; clrw $dir; for (my $i=0; $i<$iao; $i++) { my $t1 = qx[find $dir -iname "*$array_out[$i].mp3*"]; chomp $t1; if ($t1 =~ m/\n/) { chomp $t1; my ($s1, $s2) = split('\n', "$t1"); clrw $s1; clrw $s2; push @dpath, $s1; push @dpath, $s2 } else { clrw $t1; push @dpath, $t1 }} my $dpi = $#dpath; my $idp = $dpi + 1; for (my $i=0; $i<$idp; $i++) { my $prog = 'mpg123'; my @args = ("$prog", '--cpu', 'SSE', "$dpath[$i]"); system(@args) == 0 or die "System args failed: $?" }

So, you take a playlist made with your favorite editor (mine is vim), and follow the given format. Note, because the player uses find, it will search the files for you. (A true playlist, so you don't have to add the file-list paths.. that would be bullshit.) It will also add multiple hits into the playlist! (Some push action for ya.)

# This is a tracklist for my # Deftones # Mix Playlist #1 # July 2013 # # 1. Diamond Eyes # 2. My Own Summer # 3. CMND_CTRL # 4. 7 Words # 5. Hexagram # 6. Minerva # 7. Knife Party # 8. Rocket Skates # 9. Royal # 10. Passenger (feat. Maynard James Keenan) # 11. Change (In The House Of Flies) # 12. Sextape

Comment on Playlist parser for mpg123
Select or Download Code
Re: Playlist parser for mpg123
by toolic (Chancellor) on Aug 14, 2013 at 15:07 UTC
    Most importantly, thank God for the Deftones. Rocket Skates++, especially.

    Secondarily, I noticed you are doing a lot of \n manipulation after your qx. You might be able to simplify that code by capturing the find output into an array instead:

    my @files = qx[find $dir -iname "*$array_out[$i].mp3*"];

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1047599]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-07-28 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (197 votes), past polls