-
Notifications
You must be signed in to change notification settings - Fork 1
/
sortmp3dir
executable file
·75 lines (64 loc) · 1.9 KB
/
sortmp3dir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
use MP3::Info;
use DirHandle;
sub my_rename($$) {
print STDERR "$_[0] => $_[1]\n";
return(rename($_[0], $_[1]));
}
sub my_mkdir($$) {
print STDERR "-> $_[0]\n";
return(mkdir($_[0], $_[1]));
}
my $dir=new DirHandle "." or die "Failed to read current directory: $!\n";
for my $file (grep { -f $_ and /\.mp3/i } $dir->read) {
my $tag=get_mp3tag($file) or next;
my $tracknum;
if (exists($tag->{TRACKNUM})) {
$tracknum=$tag->{TRACKNUM};
$tracknum=~s/[^\w\-\.]/_/g;
}
my $title;
if (exists($tag->{TITLE})) {
$title=$tag->{TITLE};
$title=~s/[^\w\-\.]/_/g;
}
my $filename;
if (defined($tracknum) and $tracknum and defined($title) and $title) {
$filename=$tracknum . "_-_" . $title . ".mp3";
} elsif (defined($title) and $title) {
$filename=$title . ".mp3";
} else {
$filename=$file;
}
$filename=~s/^\.+//;
if ($tag->{ARTIST}) {
my $artist=$tag->{ARTIST};
$artist=~s/[^\w\-\.]/_/g;
$artist=~s/^\.+//;
die "Failed to create $artist: $!\n"
if (!(my_mkdir($artist, 0755)) and ($! != EEXIST));
if ($tag->{ALBUM}) {
my $album=$tag->{ALBUM};
$album=~s/[^\w\-\.]/_/g;
$album=~s/^\.+//;
die "Failed to create $artist/$album: $!\n"
if (!(my_mkdir("$artist/$album", 0755)) and ($! != EEXIST));
if (!-f "$artist/$album/$filename") {
my_rename($file, "$artist/$album/$filename")
or die "Failed to create $artist/$album/$filename: $!\n";
} else {
warn "$artist/$album/$filename already exists linking $file!";
}
} else {
if (!-f "$artist/$filename") {
my_rename($file, "$artist/$filename")
or die "Failed to create $artist/$filename: $!\n";
} else {
warn "$artist/$filename already exists linking $file!";
}
}
}
}