#!/usr/bin/perl -w
#
# GenMP3 0.1.9 by avizion 2003
#
# Syntax: genmp3
#
# Description:
# This script will rename all dirs and files in and make a new .m3u
# to make it "RIAA" compliant. In glftpd and 3rd party scripting for this daemon
# it simply means it's gonna convert all illegal chars into good ones or strip
# them off while also cutting down dir/file-name lengths to max 64 chars.
#
# Dependancies:
# You must have mp3info installed in order for better m3u generation.
#
# Please notice:
# It will delete any existing .m3u, .pls in the dirs.
# This script WILL cut filenames longer than 64 chars! BE WARNED! Use at own risk!
#
# Todo:
# Make debug output an option.
# Make more stuff an option.
# Maybe preserve text in parantesis over the actual song name.
# Options to enable/disable things like "Please notice" :)
#
# History:
# 0.0.0
# I made this script because I got sick and tired of Nero Burning Rom
# always complaining about this and that was not an OK Juliet name.
#
# 0.1.9
# First public version.
use strict;
if($#ARGV lt 0) {
print "Param count is ".$#ARGV."\n";
exit 1;
}
print "Generating Valid MP3 dir and file names with elaborate playlist.\n";
my($do_dir) = $ARGV[0];
opendir(DIR, $do_dir) || die "Cannot opendir $do_dir ($!)\n";
print "Entered $do_dir ...\n";
while (my $cur = readdir(DIR)) {
if( $cur !~ /^\./ ) {
my($newdir) = $cur;
print $cur." (".length($cur).")";
if( length($cur) > 64 ) {
print " >>>";
# fetch grptag and shrink dirname length = 64 (but with full grptag)
my(@tokens) = split /-/, $cur;
my($grptag) = $tokens[$#tokens];
my($grplen) = length($grptag);
print " Grp = ".$grptag." (".$grplen.")\n";
$newdir = (substr $cur, 0, 64-$grplen-1)."-".$grptag;
print $newdir;
}
print "\n";
my($m3u) = (substr $newdir, 0, length($newdir)-4).".m3u";
print $m3u;
print "\n";
open(M3U, "> $do_dir/$cur/$m3u.tmp");
print M3U "#EXTM3U\n";
#opendir(SUBDIR, "$do_dir/$cur") || die "Cannot opendir $do_dir/$cur ($!)\n";
#while (my $file = readdir(SUBDIR)) {
my(@subdirs) = `ls -1 '$do_dir/$cur/'`;
#print @subdirs;
foreach my $file (@subdirs) {
chomp $file;
#print "FILE ### $file\n\n";
if( $file !~ /^\./ ) {
my($newfile) = $file;
print $file." (".length($file).")";
print " >>>";
# cut filename length + maintain extension
my(@f_tokens) = split /\./, $file;
my($f_ext) = $f_tokens[$#f_tokens];
my($f_extlen) = length($f_ext);
print " Ext = ".$f_ext." (".$f_extlen.")\n";
if( length($file) > 64 && $f_ext ne "tmp") {
$newfile = (substr $file, 0, 64-$f_extlen-1).".".$f_ext;
print $newfile."\n";
}
if($f_ext eq "mp3") {
my($extinfo) = `mp3info -p "%S;%a;%t" '$do_dir/$cur/$file'`;
my($x_sec, $x_art, $x_trk) = split /;/, $extinfo;
if($x_trk eq "") {
print M3U "#EXTINF:$file\n";
}
else {
print M3U "#EXTINF:$x_sec,$x_art - $x_trk\n";
}
print M3U "$newfile\n";
}
elsif($f_ext eq "m3u") {
`rm '$do_dir/$cur/$file'`;
}
if("$do_dir/$cur/$file" ne "$do_dir/$cur/$newfile" && $f_ext ne "m3u") {
`mv '$do_dir/$cur/$file' '$do_dir/$cur/$newfile'`;
}
}
}
close(M3U);
#closedir SUBDIR;
# rename "cur" to "newdir" (rename above files first since *.mp3 and playlist are most important)
if("$do_dir/$cur" ne "$do_dir/$newdir") {
`mv '$do_dir/$cur' '$do_dir/$newdir'`;
}
`mv '$do_dir/$newdir/$m3u.tmp' '$do_dir/$newdir/$m3u'`;
print "---\n";
}
#die "... lets test just ONE dir first!\n";
}
closedir DIR;
exit 0;
# Get mp3 info: S = length in seconds, a = artist, t = track title
# mp3info -p "%S, %a - %t"