Answer the question
In order to leave comments, you need to log in
[C++] How to get audio file size and duration from URL?
Is there a library that will allow, regardless of the audio file transfer protocol (http / s, ftp) and, preferably, from its format, to find out its size and duration? I used the BASS_StreamCreateURL function from the BASS library, but it doesn't work on Linux.
Answer the question
In order to leave comments, you need to log in
You can use the ffmpeg
library . To do this, this library must be built with network support. For https connections, you need to build with openssl support.
Here is an approximate code that gets the duration and file size:
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
...
const char *url = "http://localhost/test.mp3";
AVFormatContext *pFormatCtx = NULL;
int ret = avformat_open_input(&pFormatCtx, url, NULL, NULL);
if (ret >= 0) {
avformat_find_stream_info(pFormatCtx, NULL);
int seconds = (pFormatCtx->duration / AV_TIME_BASE);
int64_t size = avio_size(pFormatCtx->pb);
avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question