• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • services
  • fileindexer
  • indexer
ffmpegextractor.cpp
Go to the documentation of this file.
1 /*
2  <one line to give the library's name and an idea of what it does.>
3  Copyright (C) 2012 Vishesh Handa <me@vhanda.in>
4 
5  Code adapted from Strigi FFmpeg Analyzer -
6  Copyright (C) 2010 Evgeny Egorochkin <phreedom.stdin@gmail.com>
7  Copyright (C) 2011 Tirtha Chatterjee <tirtha.p.chatterjee@gmail.com>
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 
24 
25 #include "ffmpegextractor.h"
26 
27 #ifdef __cplusplus
28  #define __STDC_CONSTANT_MACROS
29 #ifdef _STDINT_H
30  #undef _STDINT_H
31 #endif
32  # include <stdint.h>
33 #endif
34 
35 extern "C" {
36 #include <libavformat/avformat.h>
37 #include <libavutil/dict.h>
38 #include <libavcodec/avcodec.h>
39 }
40 
41 #include "nmm.h"
42 #include "nie.h"
43 #include "nfo.h"
44 #include "nco.h"
45 
46 #include <Soprano/Vocabulary/NAO>
47 #include <Soprano/Vocabulary/RDF>
48 
49 #include <KDebug>
50 #include <QtCore/QDateTime>
51 
52 using namespace Nepomuk2::Vocabulary;
53 using namespace Soprano::Vocabulary;
54 
55 namespace Nepomuk2 {
56 
57 FFmpegExtractor::FFmpegExtractor(QObject* parent, const QVariantList&)
58 : ExtractorPlugin(parent)
59 {
60 
61 }
62 
63 QStringList FFmpegExtractor::mimetypes()
64 {
65  QStringList types;
66 
67  types << QLatin1String("video/x-ms-asf");
68  types << QLatin1String("video/x-msvideo");
69  types << QLatin1String("video/x-flv");
70  types << QLatin1String("video/quicktime");
71  types << QLatin1String("video/mpeg");
72  types << QLatin1String("video/x-ms-wmv");
73  types << QLatin1String("video/mp4");
74  types << QLatin1String("video/x-matroska");
75  types << QLatin1String("video/webm");
76 
77  return types;
78 }
79 
80 SimpleResourceGraph FFmpegExtractor::extract(const QUrl& resUri, const QUrl& fileUrl, const QString& mimeType)
81 {
82  Q_UNUSED( mimeType );
83 
84  AVFormatContext *fmt_ctx = NULL;
85 
86  av_register_all();
87 
88  QByteArray arr = fileUrl.toLocalFile().toUtf8();
89 
90  fmt_ctx = avformat_alloc_context();
91  if ( int ret = avformat_open_input(&fmt_ctx, arr.data(), NULL, NULL) ) {
92  kError() << "avformat_open_input error: " << ret;
93  return SimpleResourceGraph();
94  }
95 
96  int ret = avformat_find_stream_info(fmt_ctx, NULL);
97  if( ret < 0 ) {
98  kError() << "avform_find_stream_info error: " << ret;
99  return SimpleResourceGraph();
100  }
101 
102  SimpleResourceGraph graph;
103  SimpleResource fileRes( resUri );
104 
105  if( fmt_ctx->nb_streams == 1 && fmt_ctx->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO ) {
106  fileRes.addType( NMM::MusicPiece() );
107  }
108  else {
109  fileRes.addType( NFO::Video() );
110  }
111 
112  int totalSecs = fmt_ctx->duration / AV_TIME_BASE;
113  int bitrate = fmt_ctx->bit_rate;
114 
115  fileRes.setProperty( NFO::duration(), totalSecs );
116  fileRes.setProperty( NFO::averageBitrate(), bitrate );
117 
118  for( uint i=0; i<fmt_ctx->nb_streams; i++ ) {
119  const AVStream* stream = fmt_ctx->streams[i];
120  const AVCodecContext* codec = stream->codec;
121 
122  if( codec->codec_type == AVMEDIA_TYPE_AUDIO || codec->codec_type == AVMEDIA_TYPE_VIDEO ) {
123  /*
124  if( codec->codec_type == AVMEDIA_TYPE_AUDIO ) {
125  subRes.addType( NFO::Audio() );
126  subRes.addProperty( NFO::sampleRate(), codec->sample_rate );
127  subRes.addProperty( NFO::channels(), codec->channels );
128 
129  //TODO: Fetch Sample Format
130  }*/
131 
132  if( codec->codec_type == AVMEDIA_TYPE_VIDEO ) {
133  int aspectRatio = codec->sample_aspect_ratio.num;
134  int frameRate = stream->avg_frame_rate.num;
135 
136  if( codec->sample_aspect_ratio.den )
137  aspectRatio /= codec->sample_aspect_ratio.den;
138  if( stream->avg_frame_rate.den )
139  frameRate /= stream->avg_frame_rate.den;
140 
141  fileRes.setProperty( NFO::width(), codec->width );
142  fileRes.setProperty( NFO::height(), codec->height );
143  fileRes.setProperty( NFO::aspectRatio(), aspectRatio );
144  fileRes.setProperty( NFO::frameRate(), frameRate );
145  }
146  }
147  }
148 
149  AVDictionary* dict = fmt_ctx->metadata;
150  AVDictionaryEntry *entry;
151 
152  entry = av_dict_get(dict, "title", NULL, 0);
153  if( entry ) {
154  fileRes.addProperty( NIE::title(), QString::fromUtf8( entry->value ) );
155  }
156 
157 
158  entry = av_dict_get(dict, "author", NULL, 0);
159  if( entry ) {
160  SimpleResource author;
161  author.addType( NCO::Contact() );
162  author.addProperty( NCO::fullname(), QString::fromUtf8( entry->value ) );
163  graph << author;
164 
165  fileRes.addProperty( NCO::creator(), author );
166  fileRes.addProperty( NAO::hasSubResource(), author );
167  }
168 
169  entry = av_dict_get(dict, "copyright", NULL, 0);
170  if( entry ) {
171  fileRes.addProperty( NIE::copyright(), QString::fromUtf8( entry->value ) );
172  }
173 
174  entry = av_dict_get(dict, "comment", NULL, 0);
175  if( entry ) {
176  fileRes.addProperty( NIE::comment(), QString::fromUtf8( entry->value ) );
177  }
178 
179  entry = av_dict_get(dict, "album", NULL, 0);
180  if( entry ) {
181  SimpleResource album;
182  album.addType( NMM::MusicAlbum() );
183  album.setProperty( NIE::title(), QString::fromUtf8( entry->value ) );
184  graph << album;
185 
186  fileRes.addProperty( NMM::musicAlbum(), album );
187  fileRes.addProperty( NAO::hasSubResource(), album );
188 
189  // Just in case
190  if( !fileRes.contains(RDF::type(), NMM::MusicPiece()) )
191  fileRes.addType( NMM::MusicPiece() );
192  }
193 
194  entry = av_dict_get(dict, "genre", NULL, 0);
195  if( entry ) {
196  QString value = QString::fromUtf8( entry->value );
197  fileRes.addProperty( NMM::genre(), value );
198  }
199 
200 
201  entry = av_dict_get(dict, "track", NULL, 0);
202  if( entry ) {
203  QString value = QString::fromUtf8( entry->value );
204 
205  bool ok = false;
206  int track = value.toInt( &ok );
207  if( ok && track )
208  fileRes.addProperty( NMM::trackNumber(), track );
209  }
210 
211  entry = av_dict_get(dict, "year", NULL, 0);
212  if( entry ) {
213  //FIXME: Parse date in different formats
214  QString value = QString::fromUtf8( entry->value );
215  QDate date = QDate::fromString("yyyy", value);
216  fileRes.addProperty( NIE::contentCreated(), date );
217  }
218 
219  avformat_close_input(&fmt_ctx);
220 
221  graph << fileRes;
222  return graph;
223 }
224 
225 }
226 
227 NEPOMUK_EXPORT_EXTRACTOR( Nepomuk2::FFmpegExtractor, "nepomukffmpegextractor" )
Nepomuk2::SimpleResource::setProperty
void setProperty(const QUrl &property, const QVariant &value)
Set a property overwriting existing values.
Definition: simpleresource.cpp:186
Nepomuk2::ExtractorPlugin
The ExtractorPlugin is the base class for all file metadata extractors.
Definition: extractorplugin.h:60
Nepomuk2::SimpleResource
Represents a snapshot of one Nepomuk resource.
Definition: simpleresource.h:46
QObject
Nepomuk2::SimpleResource::contains
bool contains(const QUrl &property) const
Definition: simpleresource.cpp:160
Nepomuk2::SimpleResource::addProperty
void addProperty(const QUrl &property, const QVariant &value)
Add a property.
Definition: simpleresource.cpp:206
Nepomuk2::SimpleResourceGraph
Definition: simpleresourcegraph.h:48
Nepomuk2::FFmpegExtractor::extract
virtual SimpleResourceGraph extract(const QUrl &resUri, const QUrl &fileUrl, const QString &mimeType)
The main function of the plugin that is responsible for extracting the data from the file url and ret...
Definition: ffmpegextractor.cpp:80
Nepomuk2::FFmpegExtractor
Definition: ffmpegextractor.h:28
NEPOMUK_EXPORT_EXTRACTOR
#define NEPOMUK_EXPORT_EXTRACTOR(classname, libname)
Export a Nepomuk file extractor.
Definition: extractorplugin.h:163
ffmpegextractor.h
Nepomuk2::SimpleResource::addType
void addType(const QUrl &type)
A convenience method which adds a property of type rdf:type.
Definition: simpleresource.cpp:257
Nepomuk2::FFmpegExtractor::FFmpegExtractor
FFmpegExtractor(QObject *parent, const QVariantList &)
Definition: ffmpegextractor.cpp:57
Nepomuk2::FFmpegExtractor::mimetypes
virtual QStringList mimetypes()
Provide a list of mimetypes which are supported by this plugin.
Definition: ffmpegextractor.cpp:63
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal