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 
28 {
29  QString artist;
30  QString compilation_artist;
31  QString album;
32  QString title;
33  QString genre;
34  int year = 0;
35  int tracknum = 0;
36 
37  AVFormatContext* p_context = nullptr;
38  AVInputFormat* p_inputformat = nullptr;
39 
40  QByteArray local8bit = filename.toLocal8Bit();
41  if ((avformat_open_input(&p_context, local8bit.constData(),
42  p_inputformat, nullptr) < 0))
43  {
44  return nullptr;
45  }
46 
47  if (avformat_find_stream_info(p_context, nullptr) < 0)
48  return nullptr;
49 
50  AVDictionaryEntry *tag = av_dict_get(p_context->metadata, "title", nullptr, 0);
51  if (!tag)
52  {
53  readFromFilename(filename, artist, album, title, genre, tracknum);
54  }
55  else
56  {
57  title = tag->value;
58 
59  tag = av_dict_get(p_context->metadata, "author", nullptr, 0);
60  if (tag)
61  artist += tag->value;
62 
63  // compilation_artist???
64  tag = av_dict_get(p_context->metadata, "album", nullptr, 0);
65  if (tag)
66  album += tag->value;
67 
68  tag = av_dict_get(p_context->metadata, "genre", nullptr, 0);
69  if (tag)
70  genre += tag->value;
71 
72  tag = av_dict_get(p_context->metadata, "year", nullptr, 0);
73  if (tag)
74  year = atoi(tag->value);
75 
76  tag = av_dict_get(p_context->metadata, "tracknum", nullptr, 0);
77  if (tag)
78  tracknum = atoi(tag->value);
79  }
80 
81  int length = getTrackLength(p_context);
82 
83  auto *retdata = new MusicMetadata(filename, artist, compilation_artist, album,
84  title, genre, year, tracknum, length);
85 
86  retdata->determineIfCompilation();
87 
88  avformat_close_input(&p_context);
89 
90  return retdata;
91 }
92 
100 {
101  AVFormatContext* p_context = nullptr;
102  AVInputFormat* p_inputformat = nullptr;
103 
104  // Open the specified file and populate the metadata info
105  QByteArray local8bit = filename.toLocal8Bit();
106  if ((avformat_open_input(&p_context, local8bit.constData(),
107  p_inputformat, nullptr) < 0))
108  {
109  return 0;
110  }
111 
112  if (avformat_find_stream_info(p_context, nullptr) < 0)
113  return 0;
114 
115  int rv = getTrackLength(p_context);
116 
117  avformat_close_input(&p_context);
118 
119  return rv;
120 }
121 
128 int MetaIOAVFComment::getTrackLength(AVFormatContext* pContext)
129 {
130  if (!pContext)
131  return 0;
132 
133  return (pContext->duration / AV_TIME_BASE) * 1000;
134 }
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:102
bool write(const QString &filename, MusicMetadata *mdata) override
Writes all metadata back to a file.
int getTrackLength(const QString &filename) override
Find the length of the track (in seconds)