Search code examples
c#.netc#-4.0mp3id3v2

How to write id3v tags to mp3 stream using idsharp


 FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read);
 MemoryStream ms = new MemoryStream();
 fs.CopyTo(ms);
 ms.Seek(0, SeekOrigin.Begin);     

 IdSharp.Tagging.ID3v2.IID3v2 tags = IdSharp.Tagging.ID3v2.ID3v2Helper.CreateID3v2(ms);                       
 // here i am changing year to new value
 tags.Year = "2012";       
 tags.Save("path"); // here it is asking for file path.

My task is to read and write id3v2 tags to mp3 stream. But save method in this case is taking string path as parameter. Is there any way to do this? Presently I am using idsharp dll.


Solution

  • You can take a look to the implementation of the save method.

    int originalTagSize = IdSharp.Tagging.ID3v2.ID3v2Tag.GetTagSize(filename);
    
    byte[] tagBytes = tags.GetBytes(originalTagSize);
    
    if (tagBytes.Length < originalTagSize)
    {
        // Eventually this won't be a problem, but for now we won't worry about shrinking tags
        throw new Exception("GetBytes() returned a size less than the minimum size");
    }
    else if (tagBytes.Length > originalTagSize)
    {
        //In the original implementation is done with:
        //ByteUtils.ReplaceBytes(path, originalTagSize, tagBytes);
        //Maybe you should write a 'ReplaceBytes' method that accepts a stream
    }
    else
    {
        // Write tag of equal length
        ms.Write(tagBytes, 0, tagBytes.Length);
    }
    

    I only reported the code of the current implementation, I have not tested it or compiled.