简体   繁体   中英

libavformat: get formats Mime Type in C++

I work on an C++ Application running on Arch Linux that should use libavformat to obtain a media files mime type. Currently using the following lines:

std::string path = "/path/to/file.extension";

av_register_all();
AVFormatContext* pFormatCtx = avformat_alloc_context();
avformat_open_input(&pFormatCtx, path.c_str(), NULL, NULL);
avformat_find_stream_info(pFormatCtx, NULL);

std::string mimeType(pFormatCtx->iformat->mime_type);

Now, that will work as expected with *.mkv (Matroska) files. Returning the expected Comma seperated mimeType String "video/x-matroska,...". But with any other File Format like *.mp4 or *.avi the iformat->mime_type will always return NULL.

How do i get the Mime Types of the other Container Formats?

It seems that avformat_find_stream_info sets only the iformat and that most AVInputFormat variables don't initialize the mime_type field.

You can also use

AVOutputFormat* format = av_guess_format(NULL,path.c_str(),NULL);
if(format)
  printf("%s\n",format->mime_type);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM