1 ///
2 /// Read media file metadata using ffmpeg.
3 ///	@file		ffmpegmetadata.cpp - pianod2
4 ///	@author		Perette Barella
5 ///	@date		2015-12-01
6 ///	@copyright	Copyright (c) 2015-2016 Devious Fish. All rights reserved.
7 ///
8 
9 #include <config.h>
10 
11 #include <string>
12 #include <memory>
13 
14 #include "metadata.h"
15 #include "ffmpegmetadata.h"
16 
17 extern "C" {
18 #include <libavformat/avformat.h>
19 #include <libavcodec/avcodec.h>
20 #include <libavutil/replaygain.h>
21 #include <libavutil/dict.h>
22 }
23 
24 // This is hacky, but we need the base class of the audio player
25 #include "../audio/ffmpegplayer.h"
26 
27 namespace Media {
28 
LavMetadataReader(const std::string & path)29     LavMetadataReader::LavMetadataReader (const std::string &path) :
30     Audio::LibavMediaReader (path, 3) {
31         std::unique_ptr <AVCodecContext, CodecDeleter> codec;
32         int audio_stream = initializeStream (codec);
33         if (transport->streams [audio_stream]->duration != AV_NOPTS_VALUE) {
34             duration = (av_q2d (transport->streams [audio_stream]->time_base) *
35                         (double) transport->streams [audio_stream]->duration);
36         }
37 
38         av_dump_format(transport, 0, url.c_str(), false);
39         try {
40             AVDictionaryEntry *item;
41             item = av_dict_get (transport->metadata, "title", nullptr, 0);
42             if (item) {
43                 title = item->value;
44             }
45             item = av_dict_get (transport->metadata, "album", nullptr, 0);
46             if (item) {
47                 album = item->value;
48             }
49             item = av_dict_get (transport->metadata, "artist", nullptr, 0);
50             if (item) {
51                 artist = item->value;
52             }
53             item = av_dict_get (transport->metadata, "genre", nullptr, 0);
54             if (item) {
55                 genre = item->value;
56             }
57             item = av_dict_get (transport->metadata, "date", nullptr, 0);
58             if (item) {
59                 year = strtol (item->value, nullptr, 10);
60             }
61             item = av_dict_get (transport->metadata, "track", nullptr, 0);
62             if (item) {
63                 splitOf(item->value, &track_number, &track_count);
64             }
65             item = av_dict_get (transport->metadata, "TPA", nullptr, 0);
66             if (item) {
67                 splitOf(item->value, &disc_number, &disc_count);
68             }
69         } catch (...) {
70             avcodec_close (codec.get());
71         }
72     }
73 
setGain(float new_gain)74     void LavMetadataReader::setGain (float new_gain) {
75         gain = new_gain;
76     }
77 
78 }
79