LinHES Forums
http://forum.linhes.org/

Find and Copy illegibly named mp3 files using myth db
http://forum.linhes.org/viewtopic.php?f=3&t=10065
Page 1 of 1

Author:  hophead18 [ Tue May 16, 2006 10:13 am ]
Post subject:  Find and Copy illegibly named mp3 files using myth db

Certain mp3 players (the iPod for instance) rename your mp3 files to meaningless filenames. Once they've been renamed, it makes it diffucult to copy an album, or certain songs, to some other device or computer.

I've written a perl script which uses the mythtv database to allow you easily copy groups of mp3's by a simple aritist, album and/or title search.

Note that you have to first import your ipod library into mythmusic.

The script should work without modification on your knoppmyth box, unless you've used non-standard directories or changed your "mythtv" user's mysql password.

Copy the script below the dashes, paste into a file (mp3cp for instance), and do 'chmod u+x mp3cp'

------------------------------------------------


#!/usr/bin/perl

use strict;
use DBI();
use Getopt::Std;
use File::Copy;
use Cwd;

my $basedir = '/myth/music';
my $mythdbuser = 'mythtv';
my $mythdbpass = 'mythtv';
my $mythdb = 'mythconverg';

our $opt_h;
our $opt_A;
our $opt_L;
our $opt_T;
our $opt_D;
our $opt_R;

#
# Command line options processing
#
sub init()
{

getopts('A:L:T:D:hR');

usage() if $opt_h;
usage() if ( ! defined($opt_A) ) and ( ! defined($opt_L) and ( ! defined($opt_T)) );

}

#
# Message about this program and how to use it
#
sub usage()
{

print "This program copies songs from your mythMusic database to a specified directory

usage: $0 [-A artist] [-L album] [-T title] [-D destination] [-R]

-h : this (help) message
-A artist : artist name [% = wildcard]
-L album : album name [% = wildcard]
-T title : song title [% = wildcard]
-D dest : destination directory [defaults to .]
-R : rename the file based on its artist/title information

example: $0 -A 'Billy%' -L '%Hits' -D /home/user\n\n";
exit 0;
}

init();

# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=$mythdb;host=localhost",
$mythdbuser, $mythdbpass,
{'RaiseError' => 1});

my $whereclause;

if (defined($opt_A)) {
$whereclause = " upper(artist) like upper(\'$opt_A\') ";
}

if (defined($opt_L)) {
if (defined($opt_A)) {
$whereclause .= " and ";
}

$whereclause .= " upper(album) like upper(\'$opt_L\') ";
}

if (defined($opt_T)) {
if (defined($opt_A) || defined($opt_L)) {
$whereclause .= " and ";
}

$whereclause .= " upper(title) like upper(\'$opt_T\') ";
}

#print $whereclause . "\n";

my $dest;
if (defined($opt_D)) {
$dest = $opt_D;
} else {
$dest = cwd;

}

die "Invalid destination directory $dest\n" unless chdir $dest;

my $sql = "select * from musicmetadata where " .
$whereclause . " order by artist, album";

# Now retrieve data from the table.
my $filecnt = 0;
my $sth = $dbh->prepare($sql);
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
$filecnt ++;
print "$ref->{'artist'} : $ref->{'title'}\n";
}
$sth->finish();

if ($filecnt == 0) {
print "No songs to copy.\n";
exit 0;
}

print "$filecnt files will be copied to $dest \n Continue? (y/n) ";

my $tofile;
my $key = <STDIN>;

chomp($key);
$key = uc($key);

if ($key eq 'Y') {
my $file;
$sth = $dbh->prepare($sql);
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
$file = $basedir . "/" . $ref->{'filename'};
print "Copying $file \n";
if (defined($opt_R)) {
$tofile = $ref->{'artist'} . '-' .
$ref->{'title'} . '.mp3';

$tofile =~ s/ /_/g;

# remove "bad" characters from filename
$tofile =~ s/[^\w\s.-]+//g;

# and then $1-ize it
($tofile) = $tofile =~ /([\w\s.-]+)/;

$tofile = $dest . '/' . $tofile;

#print $tofile;

copy($file, $tofile) or die "File cannot be copied."
} else {
copy($file, $dest) or die "File cannot be copied."
}
}

$sth->finish();

}


# Disconnect from the database.
$dbh->disconnect();

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/