D
D
DanyaFantomas2019-02-07 16:57:29
C++ / C#
DanyaFantomas, 2019-02-07 16:57:29

[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

1 answer(s)
S
SerJook, 2019-02-11
@SerJook

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);
}

As practice has shown, this code does not download the file completely, but only the initial piece.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question