std::string detectVideoType(const std::string& filename) {
AVFormatContext* formatContext = nullptr;
const AVOutputFormat* outputFormat = nullptr;
// Open input file and get format context
int ret = avformat_open_input(&formatContext, filename.c_str(), nullptr, nullptr);
if (ret < 0) {
av_log(nullptr, AV_LOG_ERROR, "Cannot open input file\n");
return "Unknown";
}
// Retrieve stream information
ret = avformat_find_stream_info(formatContext, nullptr);
if (ret < 0) {
av_log(nullptr, AV_LOG_ERROR, "Cannot find stream information\n");
avformat_close_input(&formatContext);
return "Unknown";
}
// Guess format based on filename
outputFormat = av_guess_format(nullptr, filename.c_str(), nullptr);
if (!outputFormat) {
av_log(nullptr, AV_LOG_ERROR, "Cannot guess file format\n");
avformat_close_input(&formatContext);
return "Unknown";
}
avformat_close_input(&formatContext);
// Return format name
return outputFormat->name;
}
|