Search code examples
c#.netasp.net-coremp3taglib

memory stream is not expandable on taglib File.save


I used taglib to change some of the mp3 tags it works fine with most files but I get memory stream is not expandable error sometimes here's my code:

    using var music = TagLib.File.Create(new FileBytesAbstraction(dto.MusicFile.Name, dto.MusicFile.File.GetBytes()));
            var lenght = music.Properties.Duration.TotalSeconds;
            music.Tag.Performers = await db.Artists.Where(a => dto.Artists.Contains(a.Id))
                                                   .Select(a => a.ArtisticName)
                                                   .ToArrayAsync();
            music.Tag.Album = (await db.Albums.Where(a => a.Id == dto.AlbumId).Select(a => a.Title).FirstOrDefaultAsync()) ?? "Single";

            music.Tag.Year = (uint)dto.ReleaseDate.Year;

            music.Tag.Title = dto.Title;

            music.Tag.Genres = await db.Genres.Where(g => dto.Genres.Contains(g.Id))
                                              .Select(g => g.Title)
                                              .ToArrayAsync();

            music.Save(); //error on this line

            var byteMusic = new ByteVector();
            music.RFind(byteMusic);

please help I spent 2 weeks on this error

I tried chatgpt, using temp files, sending file stream

P.S:i am using taglib.Audio lib


Solution

  • I solved My problem I just used a path Instead FileBytesAbstraction that I made custom for pass Byte[] to taglib File

      string tempFilePath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".mp3";
            try
            {
                using (var tempFileStream = System.IO.File.Create(tempFilePath))
                {
                    await dto.MusicFile.File.CopyToAsync(tempFileStream);
                }
                using var music = TagLib.File.Create(tempFilePath);
                var lenght = music.Properties.Duration.TotalSeconds;
                music.Tag.Performers = await db.Artists.Where(a => dto.Artists.Contains(a.Id))
                                                       .Select(a => a.ArtisticName)
                                                       .ToArrayAsync();
                music.Tag.Album = (await db.Albums.Where(a => a.Id == dto.AlbumId).Select(a => a.Title).FirstOrDefaultAsync()) ?? "Single";
    
                music.Tag.Year = (uint)dto.ReleaseDate.Year;
    
                music.Tag.Title = dto.Title;
    
                music.Tag.Genres = await db.Genres.Where(g => dto.Genres.Contains(g.Id))
                                                  .Select(g => g.Title)
                                                  .ToArrayAsync();
    
                music.Save();
    
                var byteMusic = new ByteVector();
                music.RFind(byteMusic);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                System.IO.File.Delete(tempFilePath);
            }