12#include "ffmpegextractor.h"
13#include "kfilemetadata_debug.h"
15#include "config-kfilemetadata.h"
18#define __STDC_CONSTANT_MACROS
26#include <libavformat/avformat.h>
27#include <libavutil/dict.h>
28#include <libavcodec/avcodec.h>
33FFmpegExtractor::FFmpegExtractor(
QObject* parent)
39 QStringLiteral(
"video/mp2t"),
40 QStringLiteral(
"video/mp4"),
41 QStringLiteral(
"video/mpeg"),
42 QStringLiteral(
"video/ogg"),
43 QStringLiteral(
"video/quicktime"),
44 QStringLiteral(
"video/vnd.avi"),
45 QStringLiteral(
"video/webm"),
46 QStringLiteral(
"video/x-flv"),
47 QStringLiteral(
"video/x-matroska"),
48 QStringLiteral(
"video/x-ms-asf"),
49 QStringLiteral(
"video/x-ms-wmv"),
50 QStringLiteral(
"video/x-msvideo"),
55 return supportedMimeTypes;
60 AVFormatContext* fmt_ctx =
nullptr;
62#if LIBAVFORMAT_VERSION_MAJOR < 58
68 fmt_ctx = avformat_alloc_context();
69 if (
int ret = avformat_open_input(&fmt_ctx, arr.
data(),
nullptr,
nullptr)) {
70 qCWarning(KFILEMETADATA_LOG) <<
"avformat_open_input error: " << ret;
74 int ret = avformat_find_stream_info(fmt_ctx,
nullptr);
76 qCWarning(KFILEMETADATA_LOG) <<
"avform_find_stream_info error: " << ret;
82 if (result->
inputFlags() & ExtractionResult::ExtractMetaData) {
83 int totalSecs = fmt_ctx->duration / AV_TIME_BASE;
84 int bitrate = fmt_ctx->bit_rate;
89 const int index_stream = av_find_default_stream_index(fmt_ctx);
90 if (index_stream >= 0) {
91 AVStream* stream = fmt_ctx->streams[index_stream];
93 const AVCodecParameters* codec = stream->codecpar;
95 if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
99 AVRational avSampleAspectRatio = av_guess_sample_aspect_ratio(fmt_ctx, stream,
nullptr);
100 AVRational avDisplayAspectRatio;
101 av_reduce(&avDisplayAspectRatio.num, &avDisplayAspectRatio.den,
102 codec->width * avSampleAspectRatio.num,
103 codec->height * avSampleAspectRatio.den,
105 double displayAspectRatio = avDisplayAspectRatio.num;
106 if (avDisplayAspectRatio.den) {
107 displayAspectRatio /= avDisplayAspectRatio.den;
109 if (displayAspectRatio) {
113 AVRational avFrameRate = av_guess_frame_rate(fmt_ctx, stream,
nullptr);
114 double frameRate = avFrameRate.num;
115 if (avFrameRate.den) {
116 frameRate /= avFrameRate.den;
124 AVDictionary* dict = fmt_ctx->metadata;
127 if (!dict && fmt_ctx->nb_streams > 0) {
128 dict = fmt_ctx->streams[0]->metadata;
131 AVDictionaryEntry* entry;
133 entry = av_dict_get(dict,
"title",
nullptr, 0);
139 entry = av_dict_get(dict,
"author",
nullptr, 0);
144 entry = av_dict_get(dict,
"copyright",
nullptr, 0);
149 entry = av_dict_get(dict,
"comment",
nullptr, 0);
154 entry = av_dict_get(dict,
"album",
nullptr, 0);
159 entry = av_dict_get(dict,
"genre",
nullptr, 0);
164 entry = av_dict_get(dict,
"track",
nullptr, 0);
169 int track = value.
toInt(&ok);
175 entry = av_dict_get(dict,
"year",
nullptr, 0);
182 avformat_close_input(&fmt_ctx);
185#include "moc_ffmpegextractor.cpp"
QString fromUtf8(QByteArrayView str)
int toInt(bool *ok, int base) const const
QByteArray toUtf8() const const