[tracker-miners/sam/libav-testing: 9/15] tracker-extract-libav: Extract Musicbrainz and AcoustID tags for audio files




commit f4b1d5151ca76a08bf99e4ec2734ea4a6ef96266
Author: Sam Thursfield <sam thursfield codethink co uk>
Date:   Wed Aug 31 12:10:46 2022 +0200

    tracker-extract-libav: Extract Musicbrainz and AcoustID tags for audio files
    
    This brings the ffmpeg/libav backend in line with the GStreamer
    extractor.

 src/tracker-extract/tracker-extract-libav.c | 109 ++++++++++++++++++++++++----
 1 file changed, 95 insertions(+), 14 deletions(-)
---
diff --git a/src/tracker-extract/tracker-extract-libav.c b/src/tracker-extract/tracker-extract-libav.c
index 9404aa7d5..2ca7cac3b 100644
--- a/src/tracker-extract/tracker-extract-libav.c
+++ b/src/tracker-extract/tracker-extract-libav.c
@@ -117,8 +117,24 @@ extract_video_info(TrackerResource *metadata,
        }
 }
 
+static TrackerResource *
+ensure_file_resource (TrackerResource *resource,
+                      const gchar     *file_url)
+{
+       TrackerResource *file_resource;
+
+       file_resource = tracker_resource_get_first_relation (resource, "nie:isStoredAs");
+       if (!file_resource) {
+               file_resource = tracker_resource_new (file_url);
+               tracker_resource_set_take_relation (resource, "nie:isStoredAs", file_resource);
+       }
+
+       return file_resource;
+}
+
 static void
-extract_music_piece_info(TrackerResource *metadata,
+extract_music_piece_info(const char      *file_url,
+                         TrackerResource *metadata,
                          AVFormatContext *format,
                          AVStream        *audio_stream)
 {
@@ -159,6 +175,16 @@ extract_music_piece_info(TrackerResource *metadata,
 
        if (artist) {
                tracker_resource_set_relation (metadata, "nmm:artist", artist);
+
+               if ((tag = find_tag (format, audio_stream, NULL, "MUSICBRAINZ_ARTISTID"))) {
+                       const char *mb_artist_id = tag->value;
+                       g_autofree char *mb_artist_uri = g_strdup_printf("https://musicbrainz.org/artist/%s";, 
mb_artist_id);
+                       TrackerResource *mb_artist = 
tracker_extract_new_external_reference("https://musicbrainz.org/doc/Artist";,
+                                                                                           mb_artist_id,
+                                                                                           mb_artist_uri);
+
+                       tracker_resource_add_take_relation (artist, "tracker:hasExternalReference", 
mb_artist);
+               }
        }
 
        if (performer) {
@@ -170,6 +196,38 @@ extract_music_piece_info(TrackerResource *metadata,
                tracker_resource_set_relation (metadata, "nmm:composer", composer);
                g_object_unref (composer);
        }
+
+       if ((tag = find_tag (format, audio_stream, NULL, "MUSICBRAINZ_TRACKID"))) {
+               const char *mb_recording_id = tag->value;
+               g_autofree char *mb_recording_uri = g_strdup_printf("https://musicbrainz.org/recording/%s";, 
mb_recording_id);
+               TrackerResource *mb_recording = 
tracker_extract_new_external_reference("https://musicbrainz.org/doc/Recording";,
+                                                                                      mb_recording_id,
+                                                                                      mb_recording_uri);
+
+               tracker_resource_add_take_relation (metadata, "tracker:hasExternalReference", mb_recording);
+       }
+
+       if ((tag = find_tag (format, audio_stream, NULL, "MUSICBRAINZ_RELEASETRACKID"))) {
+               const char *mb_track_id = tag->value;
+               g_autofree char *mb_track_uri = g_strdup_printf("https://musicbrainz.org/track/%s";, 
mb_track_id);
+               TrackerResource *mb_track = 
tracker_extract_new_external_reference("https://musicbrainz.org/doc/Track";,
+                                                                                  mb_track_id,
+                                                                                  mb_track_uri);
+
+               tracker_resource_add_take_relation (metadata, "tracker:hasExternalReference", mb_track);
+       }
+
+       if ((tag = find_tag (format, audio_stream, NULL, "ACOUSTID_FINGERPRINT"))) {
+               const char *acoustid_fingerprint = tag->value;
+               TrackerResource *hash_resource = tracker_resource_new (NULL);
+               TrackerResource *file_resource = ensure_file_resource (metadata, file_url);
+
+               tracker_resource_set_uri (hash_resource, "rdf:type", "nfo:FileHash");
+               tracker_resource_set_string (hash_resource, "nfo:hashValue", acoustid_fingerprint);
+               tracker_resource_set_string (hash_resource, "nfo:hashAlgorithm", "chromaprint");
+
+               tracker_resource_add_take_relation (file_resource, "nfo:hasHash", hash_resource);
+       }
 }
 
 static void
@@ -177,33 +235,56 @@ extract_music_album_info(TrackerResource *metadata,
                          AVFormatContext *format,
                          AVStream        *audio_stream)
 {
-       g_autoptr(TrackerResource) album_artist = NULL;
+       g_autoptr(TrackerResource) album_artist = NULL, album_disc = NULL, album = NULL;
        const char *album_artist_name = NULL;
        const char *album_title = NULL;
+       const char *content_created;
+       int disc_number = 1;
        AVDictionaryEntry *tag = NULL;
 
        if ((tag = find_tag (format, audio_stream, NULL, "album"))) {
                album_title = tag->value;
        }
 
-       if (album_title && (tag = find_tag (format, audio_stream, NULL, "album_artist"))) {
+       if (!album_title) {
+               return;
+       }
+
+       if ((tag = find_tag (format, audio_stream, NULL, "album_artist"))) {
                album_artist_name = tag->value;
                album_artist = tracker_extract_new_artist (album_artist_name);
        }
 
-       if (album_title) {
-               const char *content_created = tracker_resource_get_first_string (metadata, 
"nie:contentCreated");
-               int disc_number = 1;
-               TrackerResource *album_disc;
+       if ((tag = find_tag (format, audio_stream, NULL, "disc"))) {
+               disc_number = atoi (tag->value);
+       }
 
-               if ((tag = find_tag (format, audio_stream, NULL, "disc"))) {
-                       disc_number = atoi (tag->value);
-               }
+       content_created = tracker_resource_get_first_string (metadata, "nie:contentCreated");
+
+       album_disc = tracker_extract_new_music_album_disc (album_title, album_artist, disc_number, 
content_created);
+       album = tracker_resource_get_first_relation (album_disc, "nmm:albumDiscAlbum");
+
+       tracker_resource_set_relation (metadata, "nmm:musicAlbumDisc", album_disc);
+       tracker_resource_set_relation (metadata, "nmm:musicAlbum", tracker_resource_get_first_relation 
(album_disc, "nmm:albumDiscAlbum"));
+
+       if ((tag = find_tag (format, audio_stream, NULL, "MUSICBRAINZ_ALBUMID"))) {
+               const char *mb_release_id = tag->value;
+               g_autofree char *mb_release_uri = g_strdup_printf("https://musicbrainz.org/release/%s";, 
mb_release_id);
+               TrackerResource *mb_release = 
tracker_extract_new_external_reference("https://musicbrainz.org/doc/Release";,
+                                                                                    mb_release_id,
+                                                                                    mb_release_uri);
+
+               tracker_resource_add_take_relation (album, "tracker:hasExternalReference", mb_release);
+       }
 
-               album_disc = tracker_extract_new_music_album_disc (album_title, album_artist, disc_number, 
content_created);
+       if ((tag = find_tag (format, audio_stream, NULL, "MUSICBRAINZ_RELEASEGROUPID"))) {
+               const char *mb_release_group_id = tag->value;
+               g_autofree char *mb_release_group_uri = 
g_strdup_printf("https://musicbrainz.org/release-group/%s";, mb_release_group_id);
+               TrackerResource *mb_release_group = 
tracker_extract_new_external_reference("https://musicbrainz.org/doc/Release_Group";,
+                                                                                          
mb_release_group_id,
+                                                                                          
mb_release_group_uri);
 
-               tracker_resource_set_relation (metadata, "nmm:musicAlbumDisc", album_disc);
-               tracker_resource_set_relation (metadata, "nmm:musicAlbum", 
tracker_resource_get_first_relation (album_disc, "nmm:albumDiscAlbum"));
+               tracker_resource_add_take_relation (album, "tracker:hasExternalReference", mb_release_group);
        }
 }
 
@@ -264,7 +345,7 @@ tracker_extract_get_metadata (TrackerExtractInfo  *info,
        if (video_stream && !(video_stream->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
                extract_video_info (metadata, format, video_stream);
        } else if (audio_stream) {
-               extract_music_piece_info (metadata, format, audio_stream);
+               extract_music_piece_info (uri, metadata, format, audio_stream);
                extract_music_album_info (metadata, format, audio_stream);
        }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]