Category: Admin
Author/Contact Info
Description: First post in this section...
takes database from amarok and replaces (\s'"\._) with - in the title and artist names to mv the old mp3 to title.'_'.artist.mp3
not the best description i know but make a backup of your music dir and test it :)

let me know how it goes thanks.
#!/usr/bin/perl

use strict;
use warnings;
use DBI;

my $dbh = DBI->connect( "dbi:SQLite:/home/simon/.kde/share/apps/amarok
+/collection.db" ) or die "Cannot connect: ".$DBI::errstr."\n";
my $res = $dbh->selectall_arrayref("SELECT * FROM tags");

foreach (@$res) {
  my $artist = $dbh->selectrow_hashref("SELECT * FROM artist WHERE id=
+'".$_->[5]."'");
  $artist->{'name'} =~ s/[_ -\.]{1,}/-/g;
  $_->[8] =~ s/[_ -\.]{1,}/-/g;
  (my $full_title = $_->[8].'_'.$artist->{'name'}) =~ s/'"//g;
  change_name($_->[0],$full_title);
}

$dbh->disconnect();
exit(0);

sub change_name {
  my ($url, $title) = @_;
  $url =~ s/^\./\/home/;
  $title = $1.$title if ($url =~ m/^(\/.*\/)/);
  $title .= lc($1) if ($url =~ m/(.{4})$/);
  `mv "$url" "$title"` if (!(-e $title) and -e $url);
  return(1);
}
Replies are listed 'Best First'.
Re: small script to make mp3 filenames more command line friendly
by betterworld (Curate) on May 14, 2007 at 13:11 UTC

    I like weird characters in mp3 file names, because I want the file names to contain the exact titles.

    Often it's not difficult to use the command line in a way that there's no problem with special characters. For instance, rather than writing Perl code like

    `mv "$url" "$title"`
    you can use
    system('mv', $url, $title)
    or on GNU:
    system('mv', '--', $url, $title)
    or even
    rename($url, $title)

    This will work perfectly with all kinds of strange characters in file names. Plus, it's more secure.

    Another problem some people face is shell stuff like this:

    for i in `ls`; do some_command $i done

    Of course, this will not work if there are file names with spaces (or globs).

    However, this will work fine:

    for i in *; do some_command "$i" done