MediaArea / MediaInfo

Convenient unified display of the most relevant technical and tag data for video and audio files.
https://MediaArea.net/MediaInfo
BSD 2-Clause "Simplified" License
1.32k stars 158 forks source link

Duration's calcul for AAC audio files with ADTS header #55

Open Sami32 opened 8 years ago

Sami32 commented 8 years ago

Hello,

Do you plane to implement this ?

I just discover that the duration was not displayed with this kind of AAC files, so my open source media server that use your very useful program don't as well.

I search on Internet and find this explanation :

I believe these tools are not accurate because AAC files are an elementary stream, unlike MP4 or FLV which are containers. Streams are just a sequence of samples, without a global header.

To be able to determine the length of an AAC file exactly you would need to parse from start to end and count the samples.

The vast majoritiy of AAC files out there are not really written as raw AAC, but instead each sample has an ADTS header, which makes the parsing a lot easier. The format of the ADTS header is here.

The header will give you the sample rate (i.e. 48Khz, 44.1Khz, etc.) and the length of the sample. With the sample rate you can compute the exact duration of that sample, knowing that an AAC sample uncompresses to 1024 PCM samples. The sample length allows you to jump to the next sample, where you will find again the ADTS header.

Thank you for your amazing work :-D Regards

Sami32 commented 7 years ago

I don't know if it will acceptable or even follow the MediaInfo "convention", but i find an easyway to woraround that.

After having saw that renderers use some approximation algorithm for the duration, certainly the same as WMP, that are not accurate at all. I then played with FFmpeg and discovered that -bsf:a aac_adtstoasc filter will give me an immediate and accurate result: of course as M4A ;)

So i wondered if MediaInfo could use a similar filter for such case ?

I guess that you already knew about that, but as i was happy to find a way to solve my duration issue, and would like share it, in case that it can be useful to someone else as well.

JeromeMartinez commented 7 years ago

So i wondered if MediaInfo could use a similar filter for such case ?

Are you sure FFmpeg does not parse the whole file? pure AAC files are usually small so if you test on a single file you may miss the fact the whole file is parsed. Parsing the whole file and getting the duration is also possible, but it would not be by default (in case someone parses thousands of files, it would be too slow).

Sami32 commented 7 years ago

Sure...no :flushed: As it was too fast, i thought that it was, but you could be right. And yes, i'm thinking about thousand of files been parsed as well, so that's interest me, as some users have huge libraries of 20.000 files, far biggest than my little test library.

How can i make sure FFmpeg filter do not parse the file ? I admit that checking at the FFmpeg code could be incertain for me, so perhaps try create a huge ADTS(AAC) file ?

Sami32 commented 7 years ago

Sniff...you are right.

It taken 7.6 s for a 220 MB AAC-LC (ADTS) file (1H30mn), so it's really parsed.

EDIT: Thank you for the idea of impleming it as an option :+1: as i should have missed that it was parsed and that some delay was added to users libraries scanning.