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"]


| improve this answer | |

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
| improve this answer | |
  • $\begingroup$ But it's very slow,if a audio file is too large,it can't stored in the memory. $\endgroup$ – partida Dec 14 '14 at 10:12
  • $\begingroup$ 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]}]; $\endgroup$ – partida Dec 14 '14 at 10:18
  • $\begingroup$ If you get memory errors, then don't load all the files into memory at once! $\endgroup$ – bill s Dec 14 '14 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.