MythTV  master
metaioavfcomment.cpp
Go to the documentation of this file.
1 
2 // libmythmetadata
3 #include "metaioavfcomment.h"
4 #include "musicmetadata.h"
5 
6 // libmyth
7 #include <mythconfig.h>
8 #include <mythcontext.h>
9 
10 extern "C" {
11 #include <libavformat/avformat.h>
12 #include <libavcodec/avcodec.h>
13 }
14 
18 bool MetaIOAVFComment::write(const QString & /*filename*/, MusicMetadata* /*mdata*/)
19 {
20  // Wont implement....
21  return false;
22 }
23 
27 MusicMetadata* MetaIOAVFComment::read(const QString &filename)
28 {
29  QString artist, compilation_artist, album, title, genre;
30  int year = 0, tracknum = 0, length = 0;
31 
32  AVFormatContext* p_context = nullptr;
33  AVInputFormat* p_inputformat = nullptr;
34 
35  QByteArray local8bit = filename.toLocal8Bit();
36  if ((avformat_open_input(&p_context, local8bit.constData(),
37  p_inputformat, nullptr) < 0))
38  {
39  return nullptr;
40  }
41 
42  if (avformat_find_stream_info(p_context, nullptr) < 0)
43  return nullptr;
44 
45  AVDictionaryEntry *tag = av_dict_get(p_context->metadata, "title", nullptr, 0);
46  if (!tag)
47  {
48  readFromFilename(filename, artist, album, title, genre, tracknum);
49  }
50  else
51  {
52  title = tag->value;
53 
54  tag = av_dict_get(p_context->metadata, "author", nullptr, 0);
55  if (tag)
56  artist += tag->value;
57 
58  // compilation_artist???
59  tag = av_dict_get(p_context->metadata, "album", nullptr, 0);
60  if (tag)
61  album += tag->value;
62 
63  tag = av_dict_get(p_context->metadata, "genre", nullptr, 0);
64  if (tag)
65  genre += tag->value;
66 
67  tag = av_dict_get(p_context->metadata, "year", nullptr, 0);
68  if (tag)
69  year = atoi(tag->value);
70 
71  tag = av_dict_get(p_context->metadata, "tracknum", nullptr, 0);
72  if (tag)
73  tracknum = atoi(tag->value);
74  }
75 
76  length = getTrackLength(p_context);
77 
78  MusicMetadata *retdata = new MusicMetadata(filename, artist, compilation_artist, album,
79  title, genre, year, tracknum, length);
80 
81  retdata->determineIfCompilation();
82 
83  avformat_close_input(&p_context);
84 
85  return retdata;
86 }
87 
94 int MetaIOAVFComment::getTrackLength(const QString &filename)
95 {
96  AVFormatContext* p_context = nullptr;
97  AVInputFormat* p_inputformat = nullptr;
98 
99  // Open the specified file and populate the metadata info
100  QByteArray local8bit = filename.toLocal8Bit();
101  if ((avformat_open_input(&p_context, local8bit.constData(),
102  p_inputformat, nullptr) < 0))
103  {
104  return 0;
105  }
106 
107  if (avformat_find_stream_info(p_context, nullptr) < 0)
108  return 0;
109 
110  int rv = getTrackLength(p_context);
111 
112  avformat_close_input(&p_context);
113 
114  return rv;
115 }
116 
123 int MetaIOAVFComment::getTrackLength(AVFormatContext* pContext)
124 {
125  if (!pContext)
126  return 0;
127 
128  return (pContext->duration / AV_TIME_BASE) * 1000;
129 }
MusicMetadata * read(const QString &filename) override
Reads MusicMetadata from a file.
void readFromFilename(const QString &filename, QString &artist, QString &album, QString &title, QString &genre, int &tracknum)
Reads MusicMetadata based on the folder/filename.
Definition: metaio.cpp:103
bool write(const QString &filename, MusicMetadata *mdata) override
Writes all metadata back to a file.
bool determineIfCompilation(bool cd=false)
int getTrackLength(const QString &filename) override
Find the length of the track (in seconds)