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']
92 my @releases = @{ $response->{'releases'} };
93 my $releasenum = $start;
96 foreach my $release (@releases) {
98 if (@{ $release->{'artist-credit'} } > 0) {
99 $a_artist = @{ $release->{'artist-credit'} }[0]->{'name'};
103 if ($a_artist =~ /Various Artists/) {
107 my @release_events = @{ $release->{'release-events'} };
109 if (@release_events > 0) {
110 $rel_year = substr(@release_events[0]->{'date'},0,4);
114 open (OUT, "> $workdir/cddbread.$releasenum");
115 binmode OUT, ":utf8";
116 print OUT "# xmcd style database file\n";
118 print OUT "# Track frame offsets:\n";
120 my @offsets = @{ $response->{'offsets'}};
121 foreach my $offset (@offsets) {
122 printf OUT "# %d\n", $offset;
125 # Locate the media that contains a disc with the discid we requested
126 # initially. The API may return multiple media associated with the
127 # release, including media with different discids
129 my @disks = @{ $_->{'discs'} };
130 grep { $_->{'id'} eq $discid } @disks;
131 } @{ $release->{'media'} };
134 # This release doesn't have a media with our requested dicsid
135 # Shouldn't happen (?), skip it
139 # Only consider the first medium
140 my $medium = @mediums[0];
141 my @tracks = @{ $medium->{'tracks'} };
144 for (my $i = 0; $i < scalar(@tracks); $i++) {
145 my $track = $tracks[$i];
146 $total_len += $track->{'length'};
150 printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0;
152 print OUT "# Submitted via: XXXXXX\n";
154 print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n";
155 print OUT "#CATEGORY=none\n";
156 print OUT "DISCID=" . $discid . "\n";
157 print OUT "DTITLE=" . $a_artist. " / " . $release->{'title'} . "\n";
158 print OUT "DYEAR=" . $rel_year . "\n";
159 print OUT "DGENRE=\n";
161 for (my $i = 0; $i < scalar(@tracks); $i++) {
162 my $track = $tracks[$i];
163 my $t_name = $track->{'title'};
164 if ($va and @{ $track->{'artist-credit'} } > 0) {
165 my $t_artist = $track->{'artist-credit'}->[0]->{'name'};
166 printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name;
168 printf OUT "TTITLE%d=%s\n", $i, $t_name;
173 for (my $i = 0; $i < scalar(@tracks); $i++) {
174 printf OUT "EXTT%d=\n", $i;
176 print OUT "PLAYORDER=\n";
181 open (OUT, "> $workdir/mbid.$releasenum");
182 print OUT $release->{'id'};
186 open (OUT, "> $workdir/asin.$releasenum");
187 print OUT $release->{'asin'};
190 # Check to see that this entry is unique; generate a checksum
191 # and compare to any previous checksums
192 my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
193 foreach my $sum (@sums) {
194 if ($checksum eq $sum) {
195 unlink("$workdir/cddbread.$releasenum");
200 push (@sums, $checksum);
202 } elsif ($command =~ m/calcid/) {
203 # Calculate MusicBrainz ID from disc offsets; see
204 # https://musicbrainz.org/doc/DiscIDCalculation
206 if ($#discinfo < 5) {
207 print STDERR "Insufficient or missing discinfo data.\n";
210 my ($first, $last, $leadin, $leadout, @offsets) = @discinfo;
212 my $s = Digest::SHA->new(1);
213 $s->add(sprintf "%02X", int($first));
214 $s->add(sprintf "%02X", int($last));
217 for (my $i = 0; $i < 100; $i++) {
221 foreach my $o ($leadout, @offsets) {
222 $a[$i++] = int($o) + int($leadin);
224 for (my $i = 0; $i < 100; $i++) {
225 $s->add(sprintf "%08X", $a[$i]);
228 my $id = $s->b64digest;
229 # CPAN Digest modules do not pad their Base64 output, so we have to do it.
230 while (length($id) % 4) {
239 if (-t STDOUT) { print "\n"; }
241 print STDERR "Unknown command given.\n";
249 abcde-musicbrainz-tool - Musicbrainz query tool
253 abcde-musicbrainz-tool [options]
256 --command {id|data|calcid} mode of operation (default: id)
257 --device <DEV> read from CD-ROM device DEV (default: /dev/cdrom)
258 --discid <ID> Disc ID to query with --command data.
259 --discinfo <F> <L> <LI> <LO> <TRK1OFF> [<TRK2OFF> [...]]
260 Disc information for --command calcid.
261 --workdir <DIR> working directory (default: /tmp)
262 --help print option summary
263 --man full documentation
269 =item B<--command> I<{id|data|calcid}>
271 Select mode of operation:
277 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:
279 ID TRACKCOUNT OFFSET1 [OFFSET2 [...]] LENGTH_S
283 Query MusicBrainz web service and store data into the workdir into cddbread.1, cddbread.2, ... files in the workdir.
287 Calculate MusicBrainz ID from given B<--discinfo> data.
293 Specify CD-ROM drive's device name, to read ID from with B<--command id>.
297 Supply disc ID for B<--command data>.
299 =item B<--discinfo> I<<first track> <last track> <lead-in sector> <lead-out sector> <track1 offset> [<track2 offset> [...]]>
301 Supply disc information for B<--command calcid>.
303 =item B<--workdir> I<directory>
305 The cddbread.* output files from B<--command data> go into this directory.
309 Print a brief help message and exit.
313 Display full manual and exit.