public async Task ApplyMetadata(string filePath, string filename, Track metadata)
        {
            try
            {
                string fullPath = Path.Combine(filePath, filename);

                using (MemoryStream modifiedFlac = new MemoryStream())
                {
                    using (FileStream originalFile = File.Open(fullPath, FileMode.Open))
                    {
                        FlacFile flac = new FlacFile(originalFile);

                        string albumArtLocation = Path.Combine(filePath, $"{metadata.Album}{Path.GetExtension(metadata.AlbumArtUrl)}");
                        if (File.Exists(albumArtLocation))
                        {
                            _logger.LogInformation("Album art was discovered. Adding it to the flac.");
                            using (FileStream stream = File.Open(albumArtLocation, FileMode.Open))
                            {
                                flac.AddPicture(stream);
                            }
                        }

                        flac.VorbisComment.CommentList[VorbisCommentType.Title]       = metadata?.Title;
                        flac.VorbisComment.CommentList[VorbisCommentType.TrackNumber] = metadata.TrackNumber?.ToString();
                        flac.VorbisComment.CommentList[VorbisCommentType.TrackTotal]  = metadata.TrackTotal?.ToString();
                        flac.VorbisComment.CommentList[VorbisCommentType.Year]        = metadata.Year;
                        flac.VorbisComment.CommentList[VorbisCommentType.Album]       = metadata.Album;
                        flac.VorbisComment.CommentList[VorbisCommentType.Artist]      = metadata.Artist;
                        flac.VorbisComment.CommentList[VorbisCommentType.Composer]    = metadata.Composer;
                        flac.VorbisComment.CommentList[VorbisCommentType.Copyright]   = metadata.Copyright;
                        flac.VorbisComment.CommentList[VorbisCommentType.DiscNumber]  = metadata.DiscNumber?.ToString();
                        flac.VorbisComment.CommentList[VorbisCommentType.DiscTotal]   = metadata.DiscTotal?.ToString();
                        flac.VorbisComment.CommentList[VorbisCommentType.Genre]       = metadata.Genre;
                        flac.VorbisComment.CommentList[VorbisCommentType.Lyricist]    = metadata.Lyricist;
                        flac.VorbisComment.CommentList[VorbisCommentType.Lyrics]      = metadata.Lyrics;

                        await flac.SaveAsync(modifiedFlac);
                    }

                    modifiedFlac.Position = 0;

                    using (FileStream originalFile = File.Open(fullPath, FileMode.Create))
                    {
                        await modifiedFlac.CopyToAsync(originalFile);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Failed to apply metadata.");
            }

            return;
        }
Beispiel #2
0
        public async Task TestBasicFlacFileFromStream()
        {
            var stream = await KnownFolders.MusicLibrary.GetFileAsync("test1.flac");

            var png = await KnownFolders.MusicLibrary.GetFileAsync("300367.png");

            var stream2 = await KnownFolders.MusicLibrary.CreateFileAsync("test2.flac", CreationCollisionOption.ReplaceExisting);

            var file = new FlacFile(await stream.OpenStreamForWriteAsync());

            file.AddPicture(PictureType.CoverFront, await png.OpenStreamForReadAsync());

            file.VorbisComment.CommentList[VorbisCommentType.Album] = "test";

            await file.SaveAsync(await stream2.OpenStreamForWriteAsync());
        }