# How to get a audio file's duration time in Mathematica?

I have a *.avi file and if I want to know the file's duration time: I can use this code to get the result:

Import["test.avi", "Duration"]


or the code to get the same result

(Import["test.avi", #] & /@{"FrameCount","FrameRate"}) /. {a_, b_} -> a/b


But if I want to know the duration time of a audio file,I try to use the similar code,but failed.

you can see the result don't involve the Duration

Import["test.mp3","Elements"](*only version10 supports MP3 format*)


{"AudioChannels", "Data", "SampledSoundList", "SampleRate", "Sound"}

Can I get the duration of audio files?

I guess the Jlink can do this job easily...

Now,in Mathematica 11. Using Duration function make it more easily:

Import["ExampleData/car.mp3", "Duration"]


Or

Duration@Audio["ExampleData/car.mp3"]


You can calculate the duration by counting the number of samples, and dividing by the sampling rate. For example, importing the stereo file="soundfile.wav"

sr = Import[file, "SampleRate"];
{channels, samples} = Dimensions[Import[file, "Data"]];
duration = samples/sr // N
`
• But it's very slow,if a audio file is too large,it can't stored in the memory. – partida Dec 14 '14 at 10:12
• the way may make the mma throws a SystemException["MemoryAllocationFailure",...],the code is: i = FileNames["E:\\ipod\*.mp3"];data = Table[snd = Import[file]; sr = snd[[1, 2]]; {chan, samples} = Dimensions[snd[[1, 1]]]; {samples/sr // N, MemoryInUse[]}, {file, Take[i, 20]}]; – partida Dec 14 '14 at 10:18
• If you get memory errors, then don't load all the files into memory at once! – bill s Dec 14 '14 at 18:16