Duration of audio files

I've been looking for a way to derive audio file duration in milliseconds for nearly two days to no avail.
Actually what I'm trying to do is mix audio on the fly from my program.
Does anybody have a very simple way to extract the exif data or get duration another way without using windows specific tools as I'm a Linux user?

Thanks for reading, Tom
Depends on format of the audio file in question. The ffmpeg library can handle pretty much any common audio/video format in existence, so you might want to look into that.

You can run:
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp3

where "input.mp3" is just an example of a filename you would give it; it could be a video file as well.
This will output the duration in seconds.

Source: https://ankitshah009.blogspot.com/2018/01/how-to-get-video-or-audio-duration-of.html

ffmpeg: https://ffmpeg.org/download.html
Last edited on
Thank you Ganado,
If I convert the files to mp3 that would provide enough resolution for what I'm experimenting with.

1
2
3
4
tom@tom-Latitude-E6420:~$ ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 test.wav
5.000000
tom@tom-Latitude-E6420:~$ ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 test.mp3
5.184000


but then it gets more complicated since I'd have to have it write the output to a file, read the file into a string, and convert the string into a double every time it runs.
Is there a more direct way of figuring the duration?
It got it using system() to call ffmpeg and write duration to a file,
then pulling it into a double "var0" using infile >> var0; and var0 = var0 * 1000;

system() being resource heavy I'm open to any suggestions to streamline the process.
Okay, I'll start off by saying that I'm not too experienced in using audio-related C/C++ libraries, so unless somebody else responds to your post with better information, my advice would be to do searches like: "C++ duration of mp3 file", "C++ duration of wav file" and you'll find results and suggested libraries/techniques to use.

Concerning ffmpeg...
I think ffmpeg is built on a set of libraries that you could call instead of spinning up a new process. If you want to use ffmpeg as a library, apparently you need to link to against libavcodec and libavformat (and maybe a few others?).
See: https://stackoverflow.com/questions/2401764/can-ffmpeg-be-used-as-a-library-instead-of-a-standalone-program

In addition to the above link, I would also check out: https://stackoverflow.com/questions/2641460/ffmpeg-c-api-documentation-tutorial

Explore both those links, and I think you'll find documentation/tutorials on how to actually use the functionality of ffmpeg directly as a library.

I also found this link: https://stackoverflow.com/questions/6451814/how-to-use-libavcodec-ffmpeg-to-find-duration-of-video-file

_________________________________________

That being said, even if you do eventually figure out how to get the above to work, I feel like it is using a sledgehammer on a fly, because ffmpeg/libav can do an enormous amount of multimedia stuff, and it sounds like you only need to a small portion of it. It may be simpler to use two libraries, one for reading/writing wav files, and one for reading/writing mp3 files. Depending on how simple the header format of a wav file or mp3 file is, you maybe able to directly read from the headers themselves to get the information you're looking for (open the file in binary mode).
https://stackoverflow.com/questions/23776115/get-mp3-length-in-c
https://stackoverflow.com/questions/13660777/c-reading-the-data-part-of-a-wav-file
Last edited on
Oh yeah I see the sledgehammer, I'm not against using ffmpeg so much as system().
I'll keep working on it it's great to have something working. I've already got it in a function so really if a better option comes it'll be a simple matter to swap out the function.
I'll read through those links, thank you for all the help.
Topic archived. No new replies allowed.