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