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

Large file size increase and invalid MPEG streams after save when ID3 exists #216

Closed m0nkge closed 11 months ago

m0nkge commented 11 months ago

The problem

Saving metadata (image and a few chapters) to MP3s that contain ID3 tag corrupts the files.

Environment

Details

In my VB project using .net4.8, I am simply reading/writing images and chapters atm. Had been using ATL4.3.0 for a while with no issues but the files were clean (stripped of everything by mp3diags). Latest files, unknown to me, had ID3v2.2 tags in them. After saving the metadata, Windows Media Player v12 embeded in my app or not, becomes unresponsive when playing them. Inspecting the files using mp3diags revealed that the original files were clean but contained a ID3 v2.2 tag but after the save a whole lot of invalid MPEG streams appear in the file. Also the file size keeps increasing (had file 60mb go to >100MB) after repeated saves. Upgraded to ATL4.36.0 no difference.

Don't forget to include ATL.Logging.Log and/or console output

I would except I am not sure how to do this as my project is VB not C#

Code To Reproduce Issue [ Good To Have ]

   ATL.Settings.ID3v2_tagSubVersion = 3

    theTrack.Chapters = New System.Collections.Generic.List(Of ChapterInfo)

    For Each row As DataGridViewRow In dgvChapters.Rows

        Dim ch As ATL.ChapterInfo = New ATL.ChapterInfo()

        Dim ts As TimeSpan = row.Cells.Item("StartTime").Value
        ch.StartTime = ts.TotalMilliseconds

        'Milliseconds???
        'ch.StartOffset = 456

        ch.EndTime = getChapterEndTimeInSecs(row.Index)

        'MilliSeconds ??
        'ch.EndOffset = 101112

        ch.Title = row.Cells.Item("ChapterTitle").Value

        If hasChapterImage(row.Index) Then
            Dim pic As Bitmap = row.Cells.Item("image").Value
            Dim converter As New ImageConverter
            ch.Picture = PictureInfo.fromBinaryData(converter.ConvertTo(pic, GetType(Byte())))
        End If

        Dim url = row.Cells.Item("url").Value
        If url <> "" And isValidURL(url) Then
            ch.Url = New ChapterInfo.UrlInfo("Chapter Link", url)
        End If

        log("   Preping ch: " & (row.Index + 1).ToString("D2") & ", starting at pos " & ts.ToString)
        theTrack.Chapters.Add(ch)
    Next

    If pbFileArtwork.Tag <> "" Then
        Dim art As Image = pbFileArtwork.Image
        Dim ms As MemoryStream = New MemoryStream()
        art.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
        Dim pic As PictureInfo = PictureInfo.fromBinaryData(ms.ToArray, PictureInfo.PIC_TYPE.CD)
        theTrack.EmbeddedPictures.Add(pic)
    End If

    log("   Writing changes...")
    If theTrack.Save() Then
        log("   Save Complete.")
        flagFileUnModified()
    Else
        MessageBox.Show("Failed to save changes.", "Alert",
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
        log("   ERROR: save failed!")
    End If
m0nkge commented 11 months ago

Here are links to download a file used in my testing.

Original: https://www.dropbox.com/s/9kneers3nw3bnd9/file-with-v2.2-tag-original.mp3?dl=0 After saved by ATL: https://www.dropbox.com/s/jv8jjizpwkaekkw/file-with-v2.2-tag-post-ALT-save.mp3?dl=0

m0nkge commented 11 months ago

Just found another corrupt file which originally contained a v2.4 and v1 tag.

Zeugma440 commented 11 months ago

Hi there! Thanks for your feedback.

Unfortunately, I haven't been able to reproduce your issue yet.

Here are links to download a file used in my testing.

Original: https://www.dropbox.com/s/9kneers3nw3bnd9/file-with-v2.2-tag-original.mp3?dl=0 After saved by ATL: https://www.dropbox.com/s/9kneers3nw3bnd9/file-with-v2.2-tag-original.mp3?dl=0

You actually submitted the same file twice 😅 Could you upload the corrupted file please ?

m0nkge commented 11 months ago

Sorry for the dup, Updated original comment but here it is again,

https://www.dropbox.com/s/jv8jjizpwkaekkw/file-with-v2.2-tag-post-ALT-save.mp3?dl=0

Zeugma440 commented 11 months ago

Thanks for the file.

Here are my first notes :

Also the file size keeps increasing (had file 60mb go to >100MB) after repeated saves

I haven't diagnosed that yet. Stay tuned~

Zeugma440 commented 11 months ago

As a complement :

=> There's actually nothing wrong with that file

Could you please post a file that has inflated beyond 60MB?

m0nkge commented 11 months ago

OK, the size increase was my fault I think. Following your code example I was adding a new pictures to the list but not removing/replacing any of the existing ones. The image, originally as a 160kb png on disk, blew up to 9mb (according to foobar) when saved in the tag as bmp. 😠

It explains the >100MB files I saw.

PS: just saw your response a few mins ago.

Zeugma440 commented 11 months ago

Soooo... is there anything left to fix or explain? :-)

m0nkge commented 11 months ago

Give all other players are not complaining, and I can get around WMP's weird non-responsive behavior, you can close this off.

Thanks for the speedy response, much appreciated.

Zeugma440 commented 11 months ago

No problem man. Don't hesitate if you find anything else~