-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatemusic.pl
executable file
·214 lines (181 loc) · 8.76 KB
/
createmusic.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env perl
# Copyright 2015 Martijn Brekhof
#
# This file is part of dummymediafiles.
#
# dummymediafiles 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 3 of the License, or
# (at your option) any later version.
#
# dummymediafiles 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 dummymediafiles. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use Cwd;
use MP3::Tag;
use File::Copy qw(copy);
if (@ARGV < 2) {
print "Usage: createmusic.pl <MP3FILE> <DIR>...\n";
exit;
}
my $MP3FILE = shift @ARGV;
sub trim($) {
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s;
}
sub writeID3v2Tag($$$$$$$$$) {
my $filename = shift;
my $artist = shift;
my $album = shift;
my $albumartist = shift;
my $tracknumber = shift;
my $tracktitle = shift;
my $year = shift;
my $genre = shift;
my $disc = shift;
my $mp3 = MP3::Tag->new($filename);
my $id3v2 = $mp3->new_tag("ID3v2");
$id3v2->add_frame("TPE1", $artist) if defined $artist;
$id3v2->add_frame("TPE2", $albumartist) if defined $albumartist;
$id3v2->add_frame("TALB", $album) if defined $album;
$id3v2->add_frame("TIT2", $tracktitle) if defined $tracktitle;
$id3v2->add_frame("TRCK", $tracknumber) if defined $tracknumber;
$id3v2->add_frame("TCON", $genre) if defined $genre;
$id3v2->add_frame("TYER", $year) if defined $year;
$id3v2->add_frame("TPOS", $disc) if defined $disc;
$id3v2->write_tag();
$mp3->close();
}
sub createMusicFile($$$) {
my $artist = shift;
my $album = shift;
my $filename = shift;
print "Creating $artist/$album/$filename\n";
if( ! -e "${artist}" ) {
mkdir "${artist}" or die "Error: failed to create directory ${artist}";
}
if ( ! -e "${artist}/${album}" ) {
mkdir "${artist}/${album}" or die "Error: failed to create directory ${artist}/${album}";
}
my $file= "${artist}/${album}/${filename}.mp3";
# Create an empty file
copy("$MP3FILE", "${file}") or return undef;
return $file;
}
sub parseFreedbFile($) {
my $filename=$_[0];
my $artist;
my $album;
my $year;
my $genre;
open FH, $filename or die "Error opening file $filename\n";
while(<FH>) {
if ( /^DYEAR=([0-9]+)/ ) {
$year = trim $1;
} elsif ( /^DGENRE=(.+)/ ) {
$genre = trim $1;
} elsif ( /^DTITLE=(.*)\s\/\s(.*)$/ ) {
$artist = trim $1;
$album = trim $2;
$album =~ s/\//-/;
} elsif ( $_ =~ /^TTITLE(\d+)=(.*)$/ ) {
my $trackno = ( trim $1 ) + 1;
my $tracktitle = trim $2;
if( $trackno < 10 ) {
$trackno = "0$trackno";
}
my $file = $trackno;
my $trackartist = $artist;
my $trackname = $tracktitle;
if ( $tracktitle =~ /\// ) {
$tracktitle =~ /(.*)\/(.*)/;
$trackartist = trim $1;
$trackname = trim $2;
$file = $file . "-$trackartist-$trackname";
} else {
$file = $file . "-$tracktitle";
}
$file = createMusicFile($artist, $album, $file);
if( defined($file) ) {
writeID3v2Tag(${file}, ${trackartist}, ${album}, ${artist}, ${trackno}, ${trackname}, ${year}, ${genre}, undef);
}
}
}
}
foreach my $DIR (@ARGV) {
opendir DH, $DIR or die "Error opening dir $DIR\n";
while(my $filename = readdir(DH)) {
my $file = "${DIR}/${filename}";
if( -f $file ) {
parseFreedbFile($file);
}
}
}
# Setup corner cases
## Artist, no album, no title
my $file = createMusicFile("The Artist", "unknown", "01-the_artist_no_album");
writeID3v2Tag(${file}, "The Artist", undef, undef, undef, "Unknown", undef, undef, undef);
## No artist with album, no title
$file = createMusicFile("unknown", "The Album", "01-the_album_no_artist");
writeID3v2Tag(${file}, undef, "The Album", undef, undef, "Unknown", undef, undef, undef);
## No artist, no album, with title
$file = createMusicFile("unknown", "unknown", "01-the_title_no_artist_no_album");
writeID3v2Tag(${file}, undef, undef, undef, undef, "The Title", undef, undef, undef);
## No artist, no album, no title
$file = createMusicFile("unknown", "unknown", "01-no_title_no_artist_no_album");
writeID3v2Tag(${file}, undef, undef, undef, undef, undef, undef, undef, undef);
# Song with three artists
$file = createMusicFile("ThreeArtists", "ThreeArtistsAlbum", "01-threeartists");
writeID3v2Tag(${file}, "First artist\0Second artist\0Third artist", "ThreeArtistsAlbum", "First artist\0Second artist\0Third artist", 01, "threeartists", undef, undef, undef);
# Various artists album
$file = createMusicFile("Various Artists", "Various Artists Album", "01-first_artist");
writeID3v2Tag(${file}, "First artist", "Various Artists Album", "Various Artists", 01, "first song", undef, undef, undef);
$file = createMusicFile("Various Artists", "Various Artists Album", "02-second_artist");
writeID3v2Tag(${file}, "Second artist", "Various Artists Album", "Various Artists", 02, "second song", undef, undef, undef);
$file = createMusicFile("Various Artists", "Various Artists Album", "03-third_artist");
writeID3v2Tag(${file}, "Third artist", "Various Artists Album", "Various Artists", 03, "third song", undef, undef, undef);
# Various artists album, no album artist
$file = createMusicFile("unknown", "Various Artists Album No Album Artist", "01-first_artist_no_album_artist");
writeID3v2Tag(${file}, "First artist", "Various Artists Album No Album Artist", undef, 01, "first song no album artist", undef, undef, undef);
$file = createMusicFile("unknown", "Various Artists Album No Album Artist", "02-second_artist_no_album_artist");
writeID3v2Tag(${file}, "Second artist", "Various Artists Album No Album Artist", undef, 02, "second song no album artist", undef, undef, undef);
$file = createMusicFile("unknown", "Various Artists Album No Album Artist", "03-third_artist_no_album_artist");
writeID3v2Tag(${file}, "Third artist", "Various Artists Album No Album Artist", undef, 03, "third song no album artist", undef, undef, undef);
# Various artists album, no song artist, with album artist
$file = createMusicFile("unknown", "Various Artists Album with Album Artist but no Artist", "01-first_album_artist_no_song_artist");
writeID3v2Tag(${file}, undef, "Various Artists Album No Song Artist", "Various Artists", 01, "first song album artist no song artist", undef, undef, undef);
$file = createMusicFile("unknown", "Various Artists Album with Album Artist but no Artist", "02-second_album_artist_no_song_artist");
writeID3v2Tag(${file}, undef, "Various Artists Album No Song Artist", "Various Artists", 02, "second song album artist no song artist", undef, undef, undef);
$file = createMusicFile("unknown", "Various Artists Album with Album Artist but no Artist", "03-third_album_artist_no_song_artist");
writeID3v2Tag(${file}, undef, "Various Artists Album No Song Artist", "Various Artists", 03, "third song album artist no song artist", undef, undef, undef);
# Multi-disc album
$file = createMusicFile("Multi disc artist", "Multi disc album", "01-first_disc_first_track");
writeID3v2Tag(${file}, "Multi disc artist", "Multi disc album", "Multi disc artist", 01, "First disc first song", undef, undef, "1/3");
$file = createMusicFile("Multi disc artist", "Multi disc album", "02-first_disc_second_track");
writeID3v2Tag(${file}, "Multi disc artist", "Multi disc album", "Multi disc artist", 02, "First disc second song", undef, undef, "1/3");
$file = createMusicFile("Multi disc artist", "Multi disc album", "01-second_disc_first_track");
writeID3v2Tag(${file}, "Multi disc artist", "Multi disc album", "Multi disc artist", 01, "Second disc first song", undef, undef, "2/3");
$file = createMusicFile("Multi disc artist", "Multi disc album", "02-second_disc_second_track");
writeID3v2Tag(${file}, "Multi disc artist", "Multi disc album", "Multi disc artist", 02, "Second disc second song", undef, undef, "2/3");
$file = createMusicFile("Multi disc artist", "Multi disc album", "01-third_disc_first_track");
writeID3v2Tag(${file}, "Multi disc artist", "Multi disc album", "Multi disc artist", 01, "Third disc first song", undef, undef, "3/3");
$file = createMusicFile("Multi disc artist", "Multi disc album", "02-third_disc_second_track");
writeID3v2Tag(${file}, "Multi disc artist", "Multi disc album", "Multi disc artist", 02, "Third disc second song", undef, undef, "3/3");
$file = createMusicFile("Multi disc artist", "Multi disc album", "03-third_disc_third_track");
writeID3v2Tag(${file}, "Multi disc artist", "Multi disc album", "Multi disc artist", 03, "Third disc third song", undef, undef, "3/3");
my $filename = shift;
my $artist = shift;
my $album = shift;
my $albumartist = shift;
my $tracknumber = shift;
my $tracktitle = shift;
my $year = shift;
my $genre = shift;
my $disc = shift;