2 # Copyright (c) 2012-2018 Steve McIntyre <93sam@debian.org>
3 # This code is hereby licensed for public consumption under either the
4 # GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
6 # You should have received a copy of the GNU General Public License along
7 # with this program; if not, write to the Free Software Foundation, Inc.,
8 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
10 # abcde-musicbrainz-tool
12 # Helper script for abcde to work with the MusicBrainz WS API (v2)
18 use MusicBrainz::DiscID;
19 use WebService::MusicBrainz 1.0.4;
23 my $FRAMES_PER_S = 75;
25 my ($device, $command, $discid, @discinfo, $workdir, $help, $man, $start);
26 Getopt::Long::Configure ('no_ignore_case');
27 Getopt::Long::Configure ('no_auto_abbrev');
28 GetOptions ("device=s" => \$device,
29 "command=s" => \$command,
30 "discid=s" => \$discid,
31 "discinfo=i{5,}" => \@discinfo,
32 "workdir=s" => \$workdir,
35 "man" => \$man) or pod2usage(-verbose => 0, -exitcode => 2);
37 print STDERR "Extraneous arguments given.\n";
38 pod2usage(-verbose => 0, -exitcode => 2);
40 pod2usage(-verbose => 1, -exitcode => 0) if $help;
41 pod2usage(-verbose => 2, -exitcode => 0) if $man;
44 if (!defined($device)) {
45 $device = "/dev/cdrom";
47 if (!defined($command)) {
50 if (!defined($workdir)) {
53 if (!defined($start)) {
59 my $s = Digest::SHA->new(1);
60 $s->addfile($filename);
64 if ($command =~ m/^id/) {
65 my $disc = new MusicBrainz::DiscID($device);
67 # read the disc in the default disc drive */
68 if ( $disc->read() == 0 ) {
69 printf STDERR "Error: %s\n", $disc->error_msg();
73 printf("%s ", $disc->id());
74 printf("%d ", $disc->last_track_num() + 1 - $disc->first_track_num());
76 for ( my $i = $disc->first_track_num;
77 $i <= $disc->last_track_num; $i++ ) {
78 printf("%d ", $disc->track_offset($i));
80 printf("%d\n", $disc->sectors() / $FRAMES_PER_S);
82 } elsif ($command =~ m/data/) {
83 if (!defined $discid or !$discid) {
84 print STDERR "Discid undefined.\n";
87 my $ws = WebService::MusicBrainz->new();
88 my $response = $ws->search(discid => {
90 inc => ['artists', 'artist-credits', 'recordings']
93 if ($response->{'error'}) {
94 print STDERR "MusicBrainz lookup returned an error \"$response->{'error'}\"\n";
98 my $releasenum = $start;
101 if ($response->{'releases'}) {
102 my @releases = @{ $response->{'releases'} };
103 foreach my $release (@releases) {
105 my $number_artists = @{ $release->{'artist-credit'}};
106 if ($number_artists > 0) {
107 for (my $i = 0; $i < $number_artists; $i++) {
109 $a_artist = $a_artist . @{ $release->{'artist-credit'} }[$i-1]->{'joinphrase'};
111 $a_artist = $a_artist . @{ $release->{'artist-credit'} }[$i]->{'name'};
116 if ($a_artist =~ /Various Artists/) {
120 if ($release->{'release-events'}) {
121 my @release_events = @{ $release->{'release-events'} };
122 if (@release_events > 0) {
123 $rel_year = substr(@release_events[0]->{'date'},0,4);
127 open (OUT, "> $workdir/cddbread.$releasenum");
128 binmode OUT, ":utf8";
129 print OUT "# xmcd style database file\n";
131 print OUT "# Track frame offsets:\n";
133 my @offsets = @{ $response->{'offsets'}};
134 foreach my $offset (@offsets) {
135 printf OUT "# %d\n", $offset;
138 # Locate the media that contains a disc with the discid we requested
139 # initially. The API may return multiple media associated with the
140 # release, including media with different discids
142 my @disks = @{ $_->{'discs'} };
143 grep { $_->{'id'} eq $discid } @disks;
144 } @{ $release->{'media'} };
147 # This release doesn't have a media with our requested dicsid
148 # Shouldn't happen (?), skip it
152 # Only consider the first medium
153 my $medium = @mediums[0];
154 my @tracks = @{ $medium->{'tracks'} };
157 for (my $i = 0; $i < scalar(@tracks); $i++) {
158 my $track = $tracks[$i];
159 $total_len += $track->{'length'};
163 printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0;
165 print OUT "# Submitted via: XXXXXX\n";
167 print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n";
168 print OUT "#CATEGORY=none\n";
169 print OUT "DISCID=" . $discid . "\n";
170 print OUT "DTITLE=" . $a_artist. " / " . $release->{'title'} . "\n";
171 print OUT "DYEAR=" . $rel_year . "\n";
172 print OUT "DGENRE=\n";
174 for (my $i = 0; $i < scalar(@tracks); $i++) {
175 my $track = $tracks[$i];
176 my $t_name = $track->{'title'};
177 my $number_artists = @{$track->{'recording'}->{'artist-credit'}};
178 if ($va and $number_artists > 0) {
180 for (my $j = 0; $j < $number_artists; $j++) {
182 $t_artist = $t_artist . @{$track->{'recording'}->{'artist-credit'}}[$j-1]->{'joinphrase'};
184 $t_artist = $t_artist . @{$track->{'recording'}->{'artist-credit'}}[$j]->{'name'};
186 printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name;
188 printf OUT "TTITLE%d=%s\n", $i, $t_name;
193 for (my $i = 0; $i < scalar(@tracks); $i++) {
194 printf OUT "EXTT%d=\n", $i;
196 print OUT "PLAYORDER=\n";
201 open (OUT, "> $workdir/mbid.$releasenum");
202 print OUT $release->{'id'};
206 open (OUT, "> $workdir/asin.$releasenum");
207 print OUT $release->{'asin'};
210 # Check to see that this entry is unique; generate a checksum
211 # and compare to any previous checksums
212 my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
213 foreach my $sum (@sums) {
214 if ($checksum eq $sum) {
215 unlink("$workdir/cddbread.$releasenum");
220 push (@sums, $checksum);
223 # No release events found - looks like we have a stub
224 # entry. We can parse it, but it's going to be very
226 print STDERR "MusicBrainz lookup only returned a stub, trying to cope\n";
233 if ($response->{'artist'}) {
234 $a_artist = $response->{'artist'};
236 if ($response->{'title'}) {
237 $a_title = $response->{'title'};
239 if ($a_artist =~ /Various Artists/) {
244 open (OUT, "> $workdir/cddbread.$releasenum");
245 binmode OUT, ":utf8";
246 print OUT "# xmcd style database file\n";
248 print OUT "# Musicbrainz stub entry - check carefully!\n";
251 my @tracks = @{ $response->{'tracks'} };
254 for (my $i = 0; $i < scalar(@tracks); $i++) {
255 my $track = $tracks[$i];
256 $total_len += $track->{'length'};
259 printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0;
261 print OUT "# Submitted via: XXXXXX\n";
263 print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n";
264 print OUT "#CATEGORY=none\n";
265 print OUT "DISCID=" . $discid . "\n";
266 print OUT "DTITLE=" . $a_artist. " / " . $a_title . "\n";
267 print OUT "DYEAR=" . $rel_year . "\n";
268 print OUT "DGENRE=\n";
270 for (my $i = 0; $i < scalar(@tracks); $i++) {
271 my $track = $tracks[$i];
272 my $t_name = $track->{'title'};
274 my $t_artist = $track->{'artist'};
275 printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name;
277 printf OUT "TTITLE%d=%s\n", $i, $t_name;
282 for (my $i = 0; $i < scalar(@tracks); $i++) {
283 printf OUT "EXTT%d=\n", $i;
285 print OUT "PLAYORDER=\n";
289 # Check to see that this entry is unique; generate a checksum
290 # and compare to any previous checksums
291 my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
292 foreach my $sum (@sums) {
293 if ($checksum eq $sum) {
294 unlink("$workdir/cddbread.$releasenum");
299 push (@sums, $checksum);
301 } elsif ($command =~ m/calcid/) {
302 # Calculate MusicBrainz ID from disc offsets; see
303 # https://musicbrainz.org/doc/DiscIDCalculation
305 if ($#discinfo < 5) {
306 print STDERR "Insufficient or missing discinfo data.\n";
309 my ($first, $last, $leadin, $leadout, @offsets) = @discinfo;
311 my $s = Digest::SHA->new(1);
312 $s->add(sprintf "%02X", int($first));
313 $s->add(sprintf "%02X", int($last));
316 for (my $i = 0; $i < 100; $i++) {
320 foreach my $o ($leadout, @offsets) {
321 $a[$i++] = int($o) + int($leadin);
323 for (my $i = 0; $i < 100; $i++) {
324 $s->add(sprintf "%08X", $a[$i]);
327 my $id = $s->b64digest;
328 # CPAN Digest modules do not pad their Base64 output, so we have to do it.
329 while (length($id) % 4) {
338 if (-t STDOUT) { print "\n"; }
340 print STDERR "Unknown command given.\n";
348 abcde-musicbrainz-tool - Musicbrainz query tool
352 abcde-musicbrainz-tool [options]
355 --command {id|data|calcid} mode of operation (default: id)
356 --device <DEV> read from CD-ROM device DEV (default: /dev/cdrom)
357 --discid <ID> Disc ID to query with --command data.
358 --discinfo <F> <L> <LI> <LO> <TRK1OFF> [<TRK2OFF> [...]]
359 Disc information for --command calcid.
360 --workdir <DIR> working directory (default: /tmp)
361 --help print option summary
362 --man full documentation
368 =item B<--command> I<{id|data|calcid}>
370 Select mode of operation:
376 Read the disc-ID from the disc in the given device, and print it, the number of tracks, their start sectors, and the duration of the disc in seconds, to stdout. Format:
378 ID TRACKCOUNT OFFSET1 [OFFSET2 [...]] LENGTH_S
382 Query MusicBrainz web service and store data into the workdir into cddbread.1, cddbread.2, ... files in the workdir.
386 Calculate MusicBrainz ID from given B<--discinfo> data.
392 Specify CD-ROM drive's device name, to read ID from with B<--command id>.
396 Supply disc ID for B<--command data>.
398 =item B<--discinfo> I<<first track> <last track> <lead-in sector> <lead-out sector> <track1 offset> [<track2 offset> [...]]>
400 Supply disc information for B<--command calcid>.
402 =item B<--workdir> I<directory>
404 The cddbread.* output files from B<--command data> go into this directory.
408 Print a brief help message and exit.
412 Display full manual and exit.