MythTV  master
metaioavfcomment.cpp
Go to the documentation of this file.
1 
2 extern "C" {
3 #include <libavformat/avformat.h>
4 #include <libavcodec/avcodec.h>
5 }
6 
7 // libmythmetadata
8 #include "metaioavfcomment.h"
9 #include "musicmetadata.h"
10 
11 // libmyth
12 #include "libmyth/mythcontext.h"
13 #include "libmythbase/mythconfig.h"
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  std::chrono::milliseconds 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 
99 std::chrono::milliseconds MetaIOAVFComment::getTrackLength(const QString &filename)
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 0ms;
110  }
111 
112  if (avformat_find_stream_info(p_context, nullptr) < 0)
113  return 0ms;
114 
115  std::chrono::milliseconds rv = getTrackLength(p_context);
116 
117  avformat_close_input(&p_context);
118 
119  return rv;
120 }
121 
128 std::chrono::milliseconds MetaIOAVFComment::getTrackLength(AVFormatContext* pContext)
129 {
130  if (!pContext)
131  return 0ms;
132 
133  auto time = av_duration(pContext->duration);
134  return duration_cast<std::chrono::milliseconds>(time);
135 }
MusicMetadata
Definition: musicmetadata.h:81
MetaIOAVFComment::getTrackLength
std::chrono::milliseconds getTrackLength(const QString &filename) override
Find the length of the track (in milliseconds)
Definition: metaioavfcomment.cpp:99
MetaIO::readFromFilename
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:95
MetaIOAVFComment::write
bool write(const QString &filename, MusicMetadata *mdata) override
Writes all metadata back to a file.
Definition: metaioavfcomment.cpp:18
metaioavfcomment.h
mythcontext.h
MetaIOAVFComment::read
MusicMetadata * read(const QString &filename) override
Reads MusicMetadata from a file.
Definition: metaioavfcomment.cpp:27
build_compdb.filename
filename
Definition: build_compdb.py:21
musicmetadata.h