1
votes

I am using the following implementation in some JS code to get durations of audio files loaded in: http://jsfiddle.net/derickbailey/s4P2v/ (Taken from https://lostechies.com/derickbailey/2013/09/23/getting-audio-file-information-with-htmls-file-api-and-audio-element/.)

My implementation is the same, even using createObjectURL. It works with MP3, AAC (.m4a), and FLAC fine, but not ALAC (also .m4a).

let length = 0;
let url = URL.createObjectURL(file);
let audio = new Audio(url);
audio.addEventListener('loadeddata', function () {
    length = audio.duration;
}

Does anyone know how I can get the duration of ALAC files using pure JavaScript? I am not using any frameworks and am new to JS.

Thanks

2
Which browser are you using? Does it support ALAC codec? Firefox for one doesn't, Chrome neither, Safari does and there it works fine.Kaiido
@Kaiido I'm on Chrome in Windows. That must be the reason. Everything else works in the program for ALAC except for obtaining length, but since length is asynchronous in JS (I think?) I have the rest of the code going after length is obtained. No wonder, this explains some of the issues I am currently debugging as well since everything (but length) worked for ALAC with this code removed. Do you have any ideas how I could possibly get length for ALAC files without this implementation since there's no browser support?IDK a good name for this

2 Answers

2
votes

In the article Web audio codec guide on MDN it shows only Safari in the browser support section for the ALAC codec.

Here is the full Browser compatibility table from that article:

Feature Chrome Edge Firefox Internet Explorer Opera Safari
ALAC support No No No No No Yes
0
votes

I think only Safari is able to natively decode ALAC codec, and there, it works.

But in all other browsers, since they can't decode it, they won't be able to give you the correct duration.

ffmpeg seems to be able to decode it though and to get its duration, so you may want to grab this info from server side, or have a look at this project which aims to port ffmpeg to js.
I never tried it myself, but it claims to have ported the mp4 decoder, so you might have some luck in there.