Prepare multi cluster tag parsing
This commit is contained in:
+67
-82
@@ -30,19 +30,23 @@
|
||||
namespace MetaData
|
||||
{
|
||||
|
||||
bool
|
||||
AvFormat::parse(const boost::filesystem::path& p, Items& items)
|
||||
AvFormat::AvFormat(const std::map<std::string, std::string>& clusterMap)
|
||||
: _clusterMap(clusterMap)
|
||||
{
|
||||
}
|
||||
|
||||
boost::optional<Items>
|
||||
AvFormat::parse(const boost::filesystem::path& p)
|
||||
{
|
||||
Items items;
|
||||
|
||||
Av::MediaFile mediaFile(p);
|
||||
|
||||
if (!mediaFile.open())
|
||||
return false;
|
||||
return boost::none;
|
||||
|
||||
if (!mediaFile.scan())
|
||||
return false;
|
||||
|
||||
std::map<std::string, std::string> metadata = mediaFile.getMetaData();
|
||||
return boost::none;
|
||||
|
||||
// Stream info
|
||||
{
|
||||
@@ -63,41 +67,6 @@ AvFormat::parse(const boost::filesystem::path& p, Items& items)
|
||||
items.insert( std::make_pair(MetaData::Type::AudioStreams, audioStreams));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<VideoStream> videoStreams;
|
||||
|
||||
std::vector<Av::Stream> streams = mediaFile.getStreams(Av::Stream::Type::Video);
|
||||
|
||||
for (Av::Stream& stream : streams)
|
||||
{
|
||||
VideoStream videoStream;
|
||||
videoStream.desc = stream.desc;
|
||||
videoStream.bitRate = stream.bitrate;
|
||||
|
||||
videoStreams.push_back(videoStream);
|
||||
}
|
||||
|
||||
if (!videoStreams.empty())
|
||||
items.insert( std::make_pair(MetaData::Type::VideoStreams, videoStreams));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<SubtitleStream> subtitleStreams;
|
||||
|
||||
std::vector<Av::Stream> streams = mediaFile.getStreams(Av::Stream::Type::Subtitle);
|
||||
|
||||
for (Av::Stream& stream : streams)
|
||||
{
|
||||
SubtitleStream subtitleStream;
|
||||
subtitleStream.desc = stream.desc;
|
||||
|
||||
subtitleStreams.push_back(subtitleStream);
|
||||
}
|
||||
|
||||
if (!subtitleStreams.empty())
|
||||
items.insert( std::make_pair(MetaData::Type::SubtitleStreams, subtitleStreams));
|
||||
}
|
||||
|
||||
// Duration
|
||||
items.insert( std::make_pair(MetaData::Type::Duration, mediaFile.getDuration() ));
|
||||
|
||||
@@ -106,19 +75,27 @@ AvFormat::parse(const boost::filesystem::path& p, Items& items)
|
||||
|
||||
// Embedded MetaData
|
||||
// Make sure to convert strings into UTF-8
|
||||
std::map<std::string, std::string>::const_iterator it;
|
||||
for (it = metadata.begin(); it != metadata.end(); ++it)
|
||||
|
||||
MetaData::Clusters clusters;
|
||||
|
||||
std::map<std::string, std::string> metadataMap = mediaFile.getMetaData();
|
||||
for (auto metadata : metadataMap)
|
||||
{
|
||||
if (boost::iequals(it->first, "artist"))
|
||||
items.insert( std::make_pair(MetaData::Type::Artist, stringTrim( stringToUTF8(it->second)) ));
|
||||
else if (boost::iequals(it->first, "album"))
|
||||
items.insert( std::make_pair(MetaData::Type::Album, stringTrim( stringToUTF8(it->second)) ));
|
||||
else if (boost::iequals(it->first, "title"))
|
||||
items.insert( std::make_pair(MetaData::Type::Title, stringTrim( stringToUTF8(it->second)) ));
|
||||
else if (boost::iequals(it->first, "track"))
|
||||
const std::string tag = boost::to_upper_copy<std::string>(metadata.first);
|
||||
const std::string value = metadata.second;
|
||||
#if 0
|
||||
std::cout << "TAG = " << tag << ", VAL = " << value << std::endl;
|
||||
#endif
|
||||
if (tag == "ARTIST")
|
||||
items.insert( std::make_pair(MetaData::Type::Artist, stringTrim( stringToUTF8(value)) ));
|
||||
else if (tag == "ALBUM")
|
||||
items.insert( std::make_pair(MetaData::Type::Album, stringTrim( stringToUTF8(value)) ));
|
||||
else if (tag == "TITLE")
|
||||
items.insert( std::make_pair(MetaData::Type::Title, stringTrim( stringToUTF8(value)) ));
|
||||
else if (tag == "TRACK")
|
||||
{
|
||||
// Expecting 'Number/Total'
|
||||
auto strings = splitString(it->second, "/");
|
||||
auto strings = splitString(value, "/");
|
||||
|
||||
if (strings.size() > 0)
|
||||
{
|
||||
@@ -134,10 +111,10 @@ AvFormat::parse(const boost::filesystem::path& p, Items& items)
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (boost::iequals(it->first, "disc"))
|
||||
else if (tag == "DISC")
|
||||
{
|
||||
// Expecting 'Number/Total'
|
||||
auto strings = splitString(it->second, "/");
|
||||
auto strings = splitString(value, "/");
|
||||
|
||||
if (strings.size() > 0)
|
||||
{
|
||||
@@ -153,49 +130,57 @@ AvFormat::parse(const boost::filesystem::path& p, Items& items)
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (boost::iequals(it->first, "date")
|
||||
|| boost::iequals(it->first, "year")
|
||||
|| boost::iequals(it->first, "WM/Year"))
|
||||
else if (tag == "DATE"
|
||||
|| tag == "YEAR"
|
||||
|| tag == "WM/Year")
|
||||
{
|
||||
boost::posix_time::ptime p;
|
||||
if (readAsPosixTime(it->second, p))
|
||||
if (readAsPosixTime(value, p))
|
||||
items.insert( std::make_pair(MetaData::Type::Date, p));
|
||||
}
|
||||
else if (boost::iequals(it->first, "TDOR") // Original release time (ID3v2 2.4)
|
||||
|| boost::iequals(it->first, "TORY")) // Original release year
|
||||
else if (tag == "TDOR" // Original release time (ID3v2 2.4)
|
||||
|| tag == "TORY") // Original release year
|
||||
{
|
||||
boost::posix_time::ptime p;
|
||||
if (readAsPosixTime(it->second, p))
|
||||
if (readAsPosixTime(value, p))
|
||||
items.insert( std::make_pair(MetaData::Type::OriginalDate, p));
|
||||
}
|
||||
else if (boost::iequals(it->first, "genre"))
|
||||
else if (tag == "MUSICBRAINZ ARTIST ID"
|
||||
|| tag == "MUSICBRAINZ_ARTISTID")
|
||||
{
|
||||
// TODO use splitStrings
|
||||
std::list<std::string> genres;
|
||||
if (readList(it->second, ";,\\", genres))
|
||||
items.insert( std::make_pair(MetaData::Type::Genres, genres));
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzArtistID, stringTrim( stringToUTF8(value)) ));
|
||||
}
|
||||
else if (tag == "MUSICBRAINZ ALBUM ID"
|
||||
|| tag == "MUSICBRAINZ_ALBUMID")
|
||||
{
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzAlbumID, stringTrim( stringToUTF8(value)) ));
|
||||
}
|
||||
else if (tag == "MUSICBRAINZ RELEASE TRACK ID"
|
||||
|| tag == "MUSICBRAINZ_RELEASETRACKID"
|
||||
|| tag == "MUSICBRAINZ_TRACKID")
|
||||
{
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzTrackID, stringTrim( stringToUTF8(value)) ));
|
||||
}
|
||||
else if (tag == "ACOUSTID ID")
|
||||
{
|
||||
items.insert( std::make_pair(MetaData::Type::AcoustID, stringTrim( stringToUTF8(value)) ));
|
||||
}
|
||||
else if (_clusterMap.find(tag) != _clusterMap.end())
|
||||
{
|
||||
std::vector<std::string> clusterNames = splitString(value, ";,\\");
|
||||
|
||||
}
|
||||
else if (boost::iequals(it->first, "MusicBrainz Artist Id")
|
||||
|| boost::iequals(it->first, "MUSICBRAINZ_ARTISTID"))
|
||||
{
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzArtistID, stringTrim( stringToUTF8(it->second)) ));
|
||||
}
|
||||
else if (boost::iequals(it->first, "MusicBrainz Album Id")
|
||||
|| boost::iequals(it->first, "MUSICBRAINZ_ALBUMID"))
|
||||
{
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzAlbumID, stringTrim( stringToUTF8(it->second)) ));
|
||||
}
|
||||
else if (boost::iequals(it->first, "MusicBrainz Release Track Id")
|
||||
|| boost::iequals(it->first, "MUSICBRAINZ_RELEASETRACKID")
|
||||
|| boost::iequals(it->first, "MUSICBRAINZ_TRACKID"))
|
||||
{
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzTrackID, stringTrim( stringToUTF8(it->second)) ));
|
||||
if (!clusterNames.empty())
|
||||
{
|
||||
clusters[_clusterMap[tag]] = std::set<std::string>(clusterNames.begin(), clusterNames.end());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
if (!clusters.empty())
|
||||
items.insert( std::make_pair(MetaData::Type::Clusters, clusters) );
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
} // namespace MetaData
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Emeric Poupon
|
||||
* Copyright (C) 2018 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
@@ -17,8 +17,10 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef METADATA_AVFORMAT_HPP
|
||||
#define METADATA_AVFORMAT_HPP
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "MetaData.hpp"
|
||||
|
||||
@@ -30,13 +32,19 @@ class AvFormat : public Parser
|
||||
{
|
||||
public:
|
||||
|
||||
bool parse(const boost::filesystem::path& p, Items& items);
|
||||
AvFormat(const std::map<std::string, std::string>& clusterMap
|
||||
= {
|
||||
{"GENRE", "Genre" },
|
||||
{"ALBUMGROUPING", "Group" }
|
||||
});
|
||||
|
||||
|
||||
boost::optional<Items> parse(const boost::filesystem::path& p);
|
||||
|
||||
private:
|
||||
|
||||
std::map<std::string,std::string> _clusterMap;
|
||||
};
|
||||
|
||||
|
||||
} // namespace MetaData
|
||||
|
||||
#endif
|
||||
|
||||
@@ -21,8 +21,10 @@
|
||||
#define METADATA_HPP
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include <boost/any.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace MetaData
|
||||
@@ -30,10 +32,11 @@ namespace MetaData
|
||||
|
||||
enum class Type
|
||||
{
|
||||
// Name Type of the value
|
||||
Artist, // string
|
||||
Title, // string
|
||||
Album, // string
|
||||
Genres, // list<string>
|
||||
Clusters, // Clusters, ex: { "genre", {"death metal", "brutal death"} }, { "albumgrouping", {"metal"} }
|
||||
Duration, // boost::posix_time::time_duration
|
||||
TrackNumber, // size_t
|
||||
DiscNumber, // size_t
|
||||
@@ -43,12 +46,11 @@ namespace MetaData
|
||||
OriginalDate, // boost::posix_time::ptime
|
||||
HasCover, // bool
|
||||
AudioStreams, // vector<AudioStream>
|
||||
VideoStreams, // vector<VideoStream>
|
||||
SubtitleStreams, // vector<SubtitleStream>
|
||||
MusicBrainzArtistID, // string
|
||||
MusicBrainzAlbumID, // string
|
||||
MusicBrainzTrackID, // string
|
||||
MusicBrainzRecordingID, // string
|
||||
AcoustID, // string
|
||||
};
|
||||
|
||||
// Used by Streams
|
||||
@@ -58,28 +60,16 @@ namespace MetaData
|
||||
std::size_t bitRate;
|
||||
};
|
||||
|
||||
struct VideoStream
|
||||
{
|
||||
std::string desc;
|
||||
std::size_t bitRate;
|
||||
};
|
||||
|
||||
struct SubtitleStream
|
||||
{
|
||||
std::string desc;
|
||||
};
|
||||
|
||||
// Type and associated data
|
||||
// See enum Type's comments
|
||||
typedef std::map<Type, boost::any> Items;
|
||||
using Items = std::map<Type, boost::any>;
|
||||
using Clusters = std::map<std::string, std::set<std::string>>;
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
|
||||
typedef std::shared_ptr<Parser> pointer;
|
||||
|
||||
virtual bool parse(const boost::filesystem::path& p, Items& items) = 0;
|
||||
virtual boost::optional<Items> parse(const boost::filesystem::path& p) = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -32,18 +32,25 @@
|
||||
namespace MetaData
|
||||
{
|
||||
|
||||
bool
|
||||
TagLibParser::parse(const boost::filesystem::path& p, Items& items)
|
||||
TagLibParser::TagLibParser(const std::map<std::string, std::string>& clusterMap)
|
||||
: _clusterMap(clusterMap)
|
||||
{
|
||||
}
|
||||
|
||||
boost::optional<Items>
|
||||
TagLibParser::parse(const boost::filesystem::path& p)
|
||||
{
|
||||
TagLib::FileRef f(p.string().c_str(),
|
||||
true, // read audio properties
|
||||
TagLib::AudioProperties::Average);
|
||||
|
||||
if (f.isNull())
|
||||
return false;
|
||||
return boost::none;
|
||||
|
||||
if (!f.audioProperties())
|
||||
return false;
|
||||
return boost::none;
|
||||
|
||||
Items items;
|
||||
|
||||
{
|
||||
TagLib::AudioProperties *properties = f.audioProperties();
|
||||
@@ -70,32 +77,46 @@ TagLibParser::parse(const boost::filesystem::path& p, Items& items)
|
||||
|
||||
if (f.tag())
|
||||
{
|
||||
TagLib::PropertyMap tags = f.file()->properties();
|
||||
MetaData::Clusters clusters;
|
||||
TagLib::PropertyMap properties = f.file()->properties();
|
||||
|
||||
for(TagLib::PropertyMap::ConstIterator itElem = tags.begin(); itElem != tags.end(); ++itElem)
|
||||
for(auto property : properties)
|
||||
{
|
||||
const std::string tag = itElem->first.to8Bit(true);
|
||||
const TagLib::StringList &values = itElem->second;
|
||||
const std::string tag = property.first.upper().to8Bit(true);
|
||||
const TagLib::StringList& values = property.second;
|
||||
|
||||
if (tag.empty() || values.isEmpty() || values.front().isEmpty())
|
||||
continue;
|
||||
|
||||
// TODO validate MBID format
|
||||
|
||||
#if 0
|
||||
std::cout << "TAG = '" << tag << "'" << std::endl;
|
||||
for (auto value : values)
|
||||
{
|
||||
std::cout << "\t'" << value.to8Bit(true) << "'" << std::endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (tag == "ARTIST")
|
||||
items.insert( std::make_pair(MetaData::Type::Artist, stringTrim( values.front().to8Bit(true))));
|
||||
else if (tag == "ALBUM")
|
||||
items.insert( std::make_pair(MetaData::Type::Album, stringTrim( values.front().to8Bit(true))));
|
||||
else if (tag == "TITLE")
|
||||
items.insert( std::make_pair(MetaData::Type::Title, stringTrim( values.front().to8Bit(true))));
|
||||
else if (tag == "MUSICBRAINZ_RELEASETRACKID")
|
||||
else if (tag == "MUSICBRAINZ_RELEASETRACKID"
|
||||
|| tag == "MUSICBRAINZ RELEASE TRACK ID")
|
||||
{
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzTrackID, stringTrim( values.front().to8Bit(true))));
|
||||
}
|
||||
else if (tag == "MUSICBRAINZ_ARTISTID")
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzArtistID, stringTrim( values.front().to8Bit(true))));
|
||||
else if (tag == "MUSICBRAINZ_ALBUMID")
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzAlbumID, stringTrim( values.front().to8Bit(true))));
|
||||
else if (tag == "MUSICBRAINZ_TRACKID")
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzRecordingID, stringTrim( tags["MUSICBRAINZ_TRACKID"].front().to8Bit(true))));
|
||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzRecordingID, stringTrim( values.front().to8Bit(true))));
|
||||
else if (tag == "ACOUSTID_ID")
|
||||
items.insert( std::make_pair(MetaData::Type::AcoustID, stringTrim( values.front().to8Bit(true))));
|
||||
else if (tag == "TRACKNUMBER")
|
||||
{
|
||||
// Expecting 'Number/Total'
|
||||
@@ -160,24 +181,29 @@ TagLibParser::parse(const boost::filesystem::path& p, Items& items)
|
||||
items.insert( std::make_pair(MetaData::Type::OriginalDate, p));
|
||||
}
|
||||
}
|
||||
else if (tag == "GENRE")
|
||||
{
|
||||
std::list<std::string> genreList;
|
||||
for (TagLib::StringList::ConstIterator itGenre = values.begin(); itGenre != values.end(); ++itGenre)
|
||||
genreList.push_back(itGenre->to8Bit(true));
|
||||
|
||||
items.insert( std::make_pair(MetaData::Type::Genres, genreList) );
|
||||
}
|
||||
else if (tag == "METADATA_BLOCK_PICTURE")
|
||||
{
|
||||
// Only add once
|
||||
if (items.find(MetaData::Type::HasCover) == items.end())
|
||||
items.insert( std::make_pair(MetaData::Type::HasCover, true));
|
||||
}
|
||||
// Check if a hit a cluster tag
|
||||
else if (_clusterMap.find(tag) != _clusterMap.end())
|
||||
{
|
||||
std::set<std::string> clusterNames;
|
||||
for (const auto& value : values)
|
||||
clusterNames.insert(value.to8Bit(true));
|
||||
|
||||
if (!clusterNames.empty())
|
||||
clusters[_clusterMap[tag]] = clusterNames;
|
||||
}
|
||||
}
|
||||
|
||||
if (!clusters.empty())
|
||||
items.insert( std::make_pair(MetaData::Type::Clusters, clusters) );
|
||||
}
|
||||
|
||||
return true;
|
||||
return items;
|
||||
}
|
||||
|
||||
} // namespace MetaData
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Emeric Poupon
|
||||
* Copyright (C) 2018 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
@@ -19,6 +19,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "MetaData.hpp"
|
||||
|
||||
namespace MetaData
|
||||
@@ -28,10 +31,19 @@ namespace MetaData
|
||||
class TagLibParser : public Parser
|
||||
{
|
||||
public:
|
||||
bool parse(const boost::filesystem::path& p, Items& items);
|
||||
|
||||
// Provide a map for TagLib name -> Cluster name
|
||||
TagLibParser(const std::map<std::string, std::string>& clusterMap
|
||||
= {
|
||||
{"GENRE", "Genre" },
|
||||
{"ALBUMGROUPING", "Group" }
|
||||
});
|
||||
|
||||
boost::optional<Items> parse(const boost::filesystem::path& p);
|
||||
|
||||
private:
|
||||
|
||||
std::map<std::string,std::string> _clusterMap;
|
||||
};
|
||||
|
||||
} // namespace MetaData
|
||||
|
||||
Reference in New Issue
Block a user