-
Notifications
You must be signed in to change notification settings - Fork 0
/
stalag13-cleanup-music-directory-02-rename.pl
executable file
·147 lines (128 loc) · 4.51 KB
/
stalag13-cleanup-music-directory-02-rename.pl
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/perl
#
# Copyright (c) 2011-2012 Mathieu Roy <yeupou--gnu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
use strict "vars";
use Fcntl ':flock';
use POSIX qw(strftime);
use File::Basename;
use File::Copy;
use Getopt::Long;
my $user = "klink";
my $maindir = "/storage/abstract/musique";
my $importdir = "/storage/abstract/musique/.A TRIER";
my $debug = 0;
my $getopt;
# get standard opts with getopt
eval {
$getopt = GetOptions("debug" => \$debug);
};
# enter working directories
chdir($maindir) or die "Unable to enter $maindir. Exit";
chdir($importdir) or die "Unable to enter $importdir. Exit";
# in $importdir, there should be dirs containing, each one, an album,
# with a file named import with the following content
# style|band|album|year
opendir(IMPORT, $importdir);
while (defined(my $dir = readdir(IMPORT))) {
# silently ignores anything but standard directories
next unless -d $dir;
next if $dir eq "." or $dir eq "..";
# ignores directories with no import file within
print "Pas de fichier $dir/import (style|band|year|album), dossier ignoré.\n" unless -e "$dir/import";
next unless -e "$dir/import";
# otherwise, find out band name and all
open(ALBUMINFO, "< $dir/import");
my $style;
my $band;
my $album;
my $year = "0000";
while (<ALBUMINFO>) {
chomp($_);
($style,$band,$year,$album) = split(/\|/, $_);
last;
}
close(ALBUMINFO);
# check we have something valid
die "style = $style; band = $band ; album = $album, exit working $dir " unless ($style and $band and $album);
# create the destination directory,
my $destdir = "$maindir/$style/$band/$album";
$destdir = "$maindir/$style/$band/$year-$album" if $year;
if (-d "$destdir") {
print "$destdir existe déjà!\n";
<STDIN>;
} else {
system("/bin/mkdir", "-p", $destdir) unless $debug;
print "/bin/mkdir -p $destdir\n";
}
# now deals with each file within:
# - move images to new dir
# - ogg or mp3, update tags, rename
opendir(ALBUMDIR, $dir);
while (defined(my $file = readdir(ALBUMDIR))) {
# ignore dirs
next if -d $file;
# find out suffix, ignore file if none found
my $suffix = 0;
my $realfile;
if ($file =~ /^(.*)(\.[^.]*)$/) { $suffix = $2; $realfile = $1; }
next unless $suffix && $realfile;
# if image, simply move it
if ($suffix eq ".png" or $suffix eq ".jpg") {
print "mv $file $destdir/\n";
move("$importdir/$dir/$file", $destdir) unless $debug;
}
# if mp3 or ogg, use lltag to update tag and rename
if ($suffix eq ".ogg" or $suffix eq ".mp3" or $suffix eq ".flac") {
system("lltag", "--dry-run", "--preserve-time", "--yes",
"--id3v2",
"--ARTIST", $band,
"--ALBUM", $album,
"--DATE", $year,
"--maj",
"--GENRE", $style,
"--rename-min",
"--rename-slash", "_",
"--rename", "$destdir/%a-%d-%A-%n-%t",
"$importdir/$dir/$file") if $debug;
system("lltag", "--preserve-time", "--yes", "--quiet",
"--id3v2",
"--ARTIST", $band,
"--ALBUM", $album,
"--DATE", $year,
"--maj",
"--GENRE", $style,
"--rename-min",
"--rename-slash", "_",
"--rename", "$destdir/%a-%d-%A-%n-%t",
"$importdir/$dir/$file") unless $debug;
}
}
closedir(ALBUMDIR);
# more cleanups
print "/usr/bin/urlize -D $destdir\n";
system("/usr/bin/urlize", "-D", $destdir) unless $debug;
print "/bin/chown -R $user:$user $maindir/$style/band/\n";
system("/bin/chown", "-R", "$user:$user", "$maindir/$style/$band/") unless $debug;
print "/bin/chmod -R a+r $maindir/$style/$band/\n";
system("/bin/chmod", "-R", "a+r", "$maindir/$style/$band/") unless $debug;
# if we get here, everything was moved, we can safely eraze initial dir
print "rm -rvf $importdir/$dir\n";
system("/bin/rm", "-rf", "$importdir/$dir") unless $debug;
}
closedir(IMPORT);
# EOF