Zeugma440 / atldotnet

Fully managed, portable and easy-to-use C# library to read and edit audio data and metadata (tags) from various audio formats, playlists and CUE sheets
MIT License
442 stars 60 forks source link

Adding picture to MP3 intermittently works #170

Closed scott-lin closed 1 year ago

scott-lin commented 1 year ago

The problem

I'm adding a Front picture to an mp3 and it intermittently succeeds.

Coincidentally, it actually succeeds exactly 50% of the time. I have a jpg in the same folder as my audio file, and my code (see below) tries to add the PictureInfo to the files embedded pictures. What I'm doing to repro, is actually just running my console app subsequent times. I can see every other time the image is added. I am clearing the embedded pictures list each attempt.

Based on the logs below from ATL, it seems to be deallocating the space required for the image if it already is present on the file.

Side question: is clearing the picture list to remove all existing images a misuse of the library? Should I remove all images another way?

Environment

Details

Note, my image is 190 KB.

Execution attempt 1 -> Succeeds when no prior image is embedded. This attempt results in the image being embedded properly. ATL log:

========================================
    Found 1 regions
    #0 : 0->3331(3.25 KB) IsBufferable = True
    ========================================
    ------------ REGION 0
    ------ ZONE default@0
    Allocating 3.25 KB
    newTagSize : 193.26 KB
    Disk stream operation (direct) : Lengthening (delta=190.01 KB)

Execution attempt 2 -> Fails when image is already embedded This attempt results in the image being removed and not being replaced (see my code). ATL log:

========================================
    Found 1 regions
    #0 : 0->197903(193.26 KB) IsBufferable = True
    ========================================
    ------------ REGION 0
    ------ ZONE default@0
    Allocating 193.26 KB
    newTagSize : 3.25 KB
    Disk stream operation (direct) : Shortening (delta=-190.01 KB)

The next execution attempt succeeds, then fails, then succeeds, and so on with the same logs above.

Code To Reproduce Issue

var track = new Track( audioFilePath );
PictureInfo coverArtInfo = PictureInfo.fromBinaryData( File.ReadAllBytes( coverArtFilePath ), PictureInfo.PIC_TYPE.Front );
track.EmbeddedPictures.Clear();
track.EmbeddedPictures.Add( coverArtInfo );

var log = new AtlLog();
track.Save();
foreach ( ATL.Logging.Log.LogItem item in log.Items )
{
    Console.WriteLine( item.Message );
}

Let me know if you need more details. Thanks.

Zeugma440 commented 1 year ago

I'll get back to you soon. Real life is kinda demanding at the moment 😆

Zeugma440 commented 1 year ago

The issue has been reproduced. The fix has been done and will be available for next release

Zeugma440 commented 1 year ago

The fix is available on today's v4.14

scott-lin commented 1 year ago

Thank you!