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
456 stars 61 forks source link

File corruption when saving tags to mp4 audio files #60

Closed mphill closed 4 years ago

mphill commented 4 years ago

The problem

When adding tags to an mp4 file (ISO Media, MPEG v4 system, Dynamic Adaptive Streaming over HTTP) the file become unplayable afterwards

Environment

Details

There are no errors produced.

Code To Reproduce Issue [ Good To Have ]

A basic example will reproduce the issue:

        var track = new Track(file.FullName);

        track.Title = metaData.Title;
        track.Artist = metaData.Author;
        track.Description = metaData.Description;
        track.TrackNumber = (int)metaData.Track;
        track.TrackTotal = (int)metaData.TrackCount;
        track.Save();

This is an exiftool dump from the original file:

MIME Type                       : video/mp4
Major Brand                     : Unknown (dash)
Minor Version                   : 0.0.0
Compatible Brands               : iso6, mp41
Movie Header Version            : 0
Create Date                     : 2020:02:21 16:44:25
Modify Date                     : 2020:02:21 16:44:25
Time Scale                      : 44100
Duration                        : 0:06:03
Preferred Rate                  : 1
Preferred Volume                : 100.00%
Preview Time                    : 0 s
Preview Duration                : 0 s
Poster Time                     : 0 s
Selection Time                  : 0 s
Selection Duration              : 0 s
Current Time                    : 0 s
Next Track ID                   : 2
Track Header Version            : 0
Track Create Date               : 2020:02:21 16:44:25
Track Modify Date               : 2020:02:21 16:44:25
Track ID                        : 1
Track Duration                  : 0:06:03
Track Layer                     : 0
Track Volume                    : 100.00%
Matrix Structure                : 1 0 0 0 1 0 0 0 1
Media Header Version            : 0
Media Create Date               : 2020:02:21 16:44:25
Media Modify Date               : 2020:02:21 16:44:25
Media Time Scale                : 44100
Media Duration                  : 0:06:03
Media Language Code             : und
Handler Type                    : Audio Track
Audio Format                    : mp4a
Audio Channels                  : 2
Audio Bits Per Sample           : 16
Audio Sample Rate               : 44100
Balance                         : 0
Movie Data Size                 : 57508
Movie Data Offset               : 5819630
Avg Bitrate                     : 128 kbps

After saving tags:

File Type                       : MP3
File Type Extension             : mp3
MIME Type                       : audio/mpeg
ID3 Size                        : 44617
Artist                          : Dynatron - Topic
Title                           : Pulse Power
Warning                         : Invalid ID3 frame size

The file's metadata seems to get mutated into an mp3

Zeugma440 commented 4 years ago

Hello and thanks for your feedback.

I'll need the MP4 file to test this one. Could you send a download link please?

mphill commented 4 years ago

I have uploaded an original and the file after tags are added: https://github.com/mphill/atldotnet/tree/master/TestSamples

Once the tags are added, the file no longer will play.

Thanks for looking at this!

Zeugma440 commented 4 years ago

Got it. I'll take a look and let you know.

Zeugma440 commented 4 years ago

That bug happens because the file you used doesn't have any udta atom. While its absence is legit according to specs, I never encountered it so far, and the current library doesn't know how to handle it.

As a consequence, it uses a fallback behaviour, that is writing an ID3v2 tag instead of the standard MP4 tag.

Stay tuned for the fix 😉

Zeugma440 commented 4 years ago

I've pushed a fix to the main repo. Tell me if you can test it or if you prefer a compiled library.

I'll publish it to NuGet as soon as you confirm it works for you.

Cheers~

mphill commented 4 years ago

The issue appears to be resolved. Thank you! Even the embedded images work.

Another issue, unrelated to your library - but it might be able to help.

On MacOS and iOS these files appear to be double the length of the file, so the audio plays, then half way is silent for the remainder. I.E., a 5 minute audio file with show as 10 minutes, with the second half silent.

I suspect this is because the native audio players (QuickTime on MacOS and AVPlayer on iOS) see 2 tracks, one for video that does not exist and another for the audio. That's my theory at least.

Can your library remove these video meta tags - essentially "cleaning up" the file?

Zeugma440 commented 4 years ago

Awesome, thanks for your fast reply 👍

Does the issue you describe appear with the edited file ? Or with the original file ?

mphill commented 4 years ago

Both files have the duration issue. Before going through and ATL and after. I am wondering if I might be able to completely remove the video meta data from this file using ATL. I think that might clear up the duration issue.

I didn't see anything in the docs that might expose that meta data so I can take it out, I do see a Remove method that takes an int.

Zeugma440 commented 4 years ago

While you were replying, I've been able to reproduce that issue by opening the original with iTunes. All other players I have (foobar, VLC, Winamp, AIMP, MusicBee, Media player classic) open it with the correct duration but have issues when trying to jump in the middle of the track.

Imho, the potential issue is not a video vs. audio track issue, as it has one unique audio track.

By examining it closely (with AtomicParsler), it has something very different than the usual MP4's I'm used to working with : instead of one "physical data" mdat atom, it has a bunch of them, interleaved with moof atoms, which makes it a fragmented MP4. I'll do some more research and keep you informed on a separate issue, as this one is solved.

Zeugma440 commented 4 years ago

Follow-up in #61. Please subscribe to the new issue

Zeugma440 commented 4 years ago

Fixed by v3.05 (now available from NuGet)

mphill commented 4 years ago

This issue appears to still show with video mp4s that come from youtube. Let me know if you need a sample. Thanks.

Zeugma440 commented 4 years ago

The library hasn't been designed with videos in mind, so this in uncharted territory.

I'll open a new issue, do some tests and ask you if I need samples.

Zeugma440 commented 4 years ago

Wait, which issue are you talking about ? Double length or file seen as an MP3 by exiftool ?

mphill commented 4 years ago

The length is actually fine in the case of the video.

If you download a video, it will play fine. Add some tags with ATL, and the video will no longer play.

I believe this specific to only youtube mp4s - but could be applicable to other mp4s

Zeugma440 commented 4 years ago

Follow-up created in #62 as what you found is on video files