Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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 ## ## 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 " <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;

Replies are listed 'Best First'.
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


    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


      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://979320]
[Discipulus]: 'give man a fish erix it sonds like a push towards omosexuality.. ;=)
[Discipulus]: see Re: from txt file to array erix
[erix]: yeah, I ask for the text (I don't see signatures)
[Discipulus]: dear brother karl has "Furthermore I consider that Donald Trump must be impeached as soon as possible" in his sig
[erix]: ah, thanks Discipulus
[1nickt]: erix perhaps you could kindly point out your technique to roho and everyone will be happy.
[Discipulus]: but i'm also animalist, so i' hirted by 1nickt's sig: The way forward always starts with a mammal test... grin..
[erix]: I might if I did remember. It's probably somewhere in the site docs
[uhClem]: I don't favor that sort of thing around here but the place for our politics is wherever we live.
[uhClem]: And besides, the objection to it was off-topic.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-06-22 12:31 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.