Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: M3U Playlist Generator

by Soul Singin' (Initiate)
on Jul 01, 2012 at 20:31 UTC ( #979320=note: print w/ replies, xml ) Need Help??


in reply to M3U Playlist Generator

This thread is over 10 years old, so I'm reviving it.

Below is my rewritten version which does not depend on File::Find. Comments welcome.

#!/usr/bin/env perl ## Soul Singin' ## 01 July 2012 ## ## completely rewritten M3U playlist generator ## http://www.perlmonks.org/?node_id=132478 ## preliminaries use strict ; use warnings ; no warnings qw( uninitialized numeric void ) ; use MP3::Info ; ## arguments ( my $inpath = $ARGV[0] ) =~ s/\/$// ; my $otfile = $ARGV[1] ; ## die if the arguments are empty if ( $inpath eq "" || $otfile eq "" ) { die "mk-m3u.pl <source dir> <output file>\n" ; } ## die if cannot find source path if ( ! -e $inpath ) { die $inpath . " does not exist.\n" ; } ## should we overwrite existing files? if ( -e $otfile ) { print $otfile . " already exists.\n" ; print 'Should I overwrite it? (Type "yes" to overwrite). ' ; my $response = <STDIN> ; if ( $response !~ /yes/i ) { die 'You did not type "yes", so I will not overwrite ' . $otfile . + ".\n" ; } } ## get the MP3 files that we want to add to our playlist my @infiles = sort( grep { -f $_ && ($_ =~ /\.mp3$/i) } glob "$inpath/ +*" ) ; ## prepare our new M3U playlist open( OTFILE, ">$otfile" ) || die "could not overwrite $otfile : $!"; print OTFILE "#EXTM3U\n"; foreach my $infile (@infiles) { ## get info from the files my $mtag = get_mp3tag( $infile) ; my $info = get_mp3info($infile) ; my $artist = $mtag->{ARTIST} ; my $stitle = $mtag->{TITLE} ; ## create title-author pair my $pair ; if ( $stitle ne "" ) { $pair .= $stitle ; } if ( $artist ne "" && $stitle ne "" ) { $pair .= " (by " . $artist . ")" ; } elsif ( $artist ne "" ) { $pair .= $artist ; } if ( $pair !~ /[A-Za-z0-9]/ ) { $pair = $infile ; } ## prepare output line my $otline = "#EXTINF:" . int ($info->{SECS}) . "," . $pair ; ## send output to our new M3U playlist print OTFILE $otline . "\n" ; print OTFILE $infile . "\n" ; } ## close the M3U playlist close OTFILE;


Comment on Re: M3U Playlist Generator
Download Code
Re^2: M3U Playlist Generator
by Anonymous Monk on Jul 01, 2012 at 21:28 UTC

    Below is my rewritten version which does not depend on File::Find

    Why?

    Comments welcome.

    $inpath can be file ( use -f or -d inestead of -e, since both -f and -d imply -exist )

    $inpath could contain glob metacharacters

    I'm personally not a fan of interactive commands, exit program if file already exists and overwrite not forced ( Getopt::Long ),

    $inpath could contain glob metacharacters

    if you don't want to recurse, you could write

    use File::Find::Rule; my @files = find( 'file', 'name', qr/\.mp3$/i, 'maxdepth', 1, 'in', $i +npath );

    FWIW, if you need to do path manipulations, Path::Class::Rule is better than File::Find::Rule;

    3 argument open is better than two, "or" is better than "||" since it doesn't require extra parens

    open my($fh), '>', $file or die "can't open '>', $file : $! ";

      Below is my rewritten version which does not depend on File::Find

      Why?

      Because I cannot understand the documentation. I'll take a look at your example and keep playing. Thanks!

      $inpath can be file ( use -f or -d inestead of -e, since both -f and -d imply -exist )

      Good point! -d would be best because the $inpath should be a directory.

      $inpath could contain glob metacharacters

      Doh! I did not even think of that ... because on my system it will not!

      3 argument open is better than two

      Old habits are hard to break.

      Thanks! Your comments have been very helpful.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2015-07-02 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (29 votes), past polls