Initial import from SVN
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
#include "AvFormat.hpp"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
#include "av/InputFormatContext.hpp"
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
namespace MetaData
|
||||
{
|
||||
|
||||
void
|
||||
AvFormat::parse(const boost::filesystem::path& p, Items& items)
|
||||
{
|
||||
|
||||
try {
|
||||
|
||||
Av::InputFormatContext input(p);
|
||||
input.findStreamInfo(); // needed by input.getDurationSecs
|
||||
|
||||
std::cout << "Processing file '" << p << "'" << std::endl;
|
||||
|
||||
std::map<std::string, std::string> metadata;
|
||||
input.getMetadata().get(metadata);
|
||||
|
||||
// HACK or OGG files
|
||||
// If we did not find tags, searched metadata in streams
|
||||
if (metadata.empty())
|
||||
{
|
||||
// Get input streams
|
||||
std::vector<Av::Stream> streams = input.getStreams();
|
||||
|
||||
BOOST_FOREACH(Av::Stream& stream, streams) {
|
||||
stream.getMetadata().get(metadata);
|
||||
|
||||
if (!metadata.empty())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Found " << metadata.size() << " tags!" << std::endl;
|
||||
|
||||
items.insert( std::make_pair(MetaData::Duration, boost::posix_time::time_duration( boost::posix_time::seconds( input.getDurationSecs() )) ));
|
||||
|
||||
std::map<std::string, std::string>::const_iterator it;
|
||||
for (it = metadata.begin(); it != metadata.end(); ++it)
|
||||
{
|
||||
if (boost::iequals(it->first, "artist"))
|
||||
items.insert( std::make_pair(MetaData::Artist, it->second ));
|
||||
else if (boost::iequals(it->first, "album"))
|
||||
items.insert( std::make_pair(MetaData::Album, it->second ));
|
||||
else if (boost::iequals(it->first, "title"))
|
||||
items.insert( std::make_pair(MetaData::Title, it->second ));
|
||||
else if (boost::iequals(it->first, "track")) {
|
||||
std::size_t number;
|
||||
if (readAs<std::size_t>(it->second, number))
|
||||
items.insert( std::make_pair(MetaData::TrackNumber, number ));
|
||||
}
|
||||
else if (boost::iequals(it->first, "disc"))
|
||||
{
|
||||
std::size_t number;
|
||||
if (readAs<std::size_t>(it->second, number))
|
||||
items.insert( std::make_pair(MetaData::DiscNumber, number ));
|
||||
}
|
||||
else if (boost::iequals(it->first, "date")
|
||||
|| boost::iequals(it->first, "year")
|
||||
|| boost::iequals(it->first, "WM/Year")
|
||||
|| boost::iequals(it->first, "TDOR") // Original date fallback
|
||||
|| boost::iequals(it->first, "TORY") // Original date fallback
|
||||
)
|
||||
{
|
||||
boost::posix_time::ptime p;
|
||||
if (readAsPosixTime(it->second, p))
|
||||
items.insert( std::make_pair(MetaData::CreationTime, p));
|
||||
}
|
||||
else if (boost::iequals(it->first, "genre"))
|
||||
{
|
||||
std::list<std::string> genres;
|
||||
if (readList(it->second, ";,", genres))
|
||||
items.insert( std::make_pair(MetaData::Genre, genres));
|
||||
|
||||
}
|
||||
/* else
|
||||
std::cout << "key = " << it->first << ", value = " << it->second << std::endl;
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
std::cerr << "Parsing of '" << p << "' failed!" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
} // namespace MetaData
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef METADATA_AVFORMAT_HPP
|
||||
#define METADATA_AVFORMAT_HPP
|
||||
|
||||
#include <extractor.h>
|
||||
|
||||
#include "MetaData.hpp"
|
||||
|
||||
namespace MetaData
|
||||
{
|
||||
|
||||
// Implements AVFORMAT library
|
||||
class AvFormat : public Parser
|
||||
{
|
||||
public:
|
||||
|
||||
void parse(const boost::filesystem::path& p, Items& items);
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace MetaData
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,207 @@
|
||||
|
||||
#include <boost/date_time/gregorian/gregorian.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
#include "Extractor.hpp"
|
||||
|
||||
namespace MetaData
|
||||
{
|
||||
|
||||
|
||||
extern "C" {
|
||||
|
||||
/**
|
||||
* Type of a function that libextractor calls for each
|
||||
* meta data item found.
|
||||
*
|
||||
* @param cls closure (user-defined)
|
||||
* @param plugin_name name of the plugin that produced this value;
|
||||
* special values can be used (i.e. '<zlib>' for zlib being
|
||||
* used in the main libextractor library and yielding
|
||||
* meta data).
|
||||
* @param type libextractor-type describing the meta data
|
||||
* @param format basic format information about data
|
||||
* @param data_mime_type mime-type of data (not of the original file);
|
||||
* can be NULL (if mime-type is not known)
|
||||
* @param data actual meta-data found
|
||||
* @param data_len number of bytes in data
|
||||
* @return 0 to continue extracting, 1 to abort
|
||||
*/
|
||||
|
||||
int processMetaData(void *cls,
|
||||
const char *plugin_name,
|
||||
enum EXTRACTOR_MetaType type,
|
||||
enum EXTRACTOR_MetaFormat format,
|
||||
const char *data_mime_type,
|
||||
const char *data,
|
||||
size_t data_len)
|
||||
{
|
||||
Items& items = *(reinterpret_cast<Items*>(cls));
|
||||
|
||||
switch (type) {
|
||||
case EXTRACTOR_METATYPE_ARTIST:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
items.insert( std::make_pair(MetaData::Artist, std::string( data, data_len ? data_len - 1 : 0)) );
|
||||
break;
|
||||
case EXTRACTOR_METATYPE_TITLE:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
items.insert( std::make_pair(MetaData::Title, std::string( data, data_len ? data_len - 1 : 0)) );
|
||||
break;
|
||||
case EXTRACTOR_METATYPE_ALBUM:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
items.insert( std::make_pair(MetaData::Album, std::string( data, data_len ? data_len - 1 : 0)) );
|
||||
break;
|
||||
case EXTRACTOR_METATYPE_GENRE:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
{
|
||||
std::list<std::string> genres;
|
||||
if (readList(std::string( data, data_len ? data_len - 1 : 0), ";-:/,", genres))
|
||||
items.insert( std::make_pair(MetaData::Genre, genres));
|
||||
}
|
||||
break;
|
||||
/* case EXTRACTOR_METATYPE_PICTURE:
|
||||
std::cout << "picture spotted" << std::endl;
|
||||
break;*/
|
||||
|
||||
case EXTRACTOR_METATYPE_COVER_PICTURE:
|
||||
{
|
||||
GenericData parsedData;
|
||||
const unsigned char* dataStart = reinterpret_cast<const unsigned char*>(data);
|
||||
parsedData.mimeType = std::string(data_mime_type);
|
||||
parsedData.data = std::vector<unsigned char>( &dataStart[0], &dataStart[data_len]);
|
||||
|
||||
items.insert( std::make_pair(MetaData::Cover, parsedData));
|
||||
}
|
||||
break;
|
||||
|
||||
/* case EXTRACTOR_METATYPE_EVENT_PICTURE:
|
||||
std::cout << "Event picture spotted" << std::endl;
|
||||
break;*/
|
||||
|
||||
/* case EXTRACTOR_METATYPE_CONTRIBUTOR_PICTURE:
|
||||
std::cout << "Contrib picture spotted" << std::endl;
|
||||
break;*/
|
||||
|
||||
/* case EXTRACTOR_METATYPE_SONG_COUNT:
|
||||
// How many songs in the album
|
||||
break;*/
|
||||
|
||||
/* case EXTRACTOR_METATYPE_AUDIO_CODEC:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
std::cout << "Aduio codec: " << std::string( data, data_len ? data_len - 1 : 0) << std::endl;
|
||||
break;*/
|
||||
case EXTRACTOR_METATYPE_PUBLICATION_YEAR:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
std::cout << "publication year = " << std::string( data, data_len ? data_len - 1 : 0) << std::endl;
|
||||
break;
|
||||
|
||||
case EXTRACTOR_METATYPE_PUBLICATION_DATE:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
std::cout << "publication date = " << std::string( data, data_len ? data_len - 1 : 0) << std::endl;
|
||||
break;
|
||||
|
||||
case EXTRACTOR_METATYPE_ORIGINAL_RELEASE_YEAR:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
std::cout << "original release year = " << std::string( data, data_len ? data_len - 1 : 0) << std::endl;
|
||||
break;
|
||||
|
||||
case EXTRACTOR_METATYPE_CREATION_TIME:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
{
|
||||
boost::posix_time::ptime p;
|
||||
if (readAs<boost::posix_time::ptime>(std::string( data, data_len ? data_len - 1 : 0), p))
|
||||
items.insert( std::make_pair(MetaData::CreationTime, p));
|
||||
else if (readAsPosixTime(std::string( data, data_len ? data_len - 1 : 0), p))
|
||||
items.insert( std::make_pair(MetaData::CreationTime, p));
|
||||
}
|
||||
break;
|
||||
case EXTRACTOR_METATYPE_DURATION:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
{
|
||||
boost::posix_time::time_duration duration;
|
||||
if (readAs<boost::posix_time::time_duration>( std::string( data, data_len ? data_len - 1 : 0), duration))
|
||||
items.insert( std::make_pair(MetaData::Duration, duration) );
|
||||
}
|
||||
break;
|
||||
case EXTRACTOR_METATYPE_TRACK_NUMBER:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
{
|
||||
std::size_t number(0);
|
||||
if (readAs<size_t>( std::string( data, data_len ? data_len - 1 : 0), number))
|
||||
items.insert( std::make_pair(MetaData::TrackNumber, number) );
|
||||
}
|
||||
break;
|
||||
case EXTRACTOR_METATYPE_DISC_NUMBER:
|
||||
assert( std::string(data_mime_type) == "text/plain" );
|
||||
{
|
||||
std::size_t number(0);
|
||||
if (readAs<size_t>( std::string( data, data_len ? data_len - 1 : 0), number))
|
||||
items.insert( std::make_pair(MetaData::DiscNumber, number) );
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/* if (std::string(data_mime_type) == "text/plain")
|
||||
std::cout << "TYPE = " << type << ", data = '" << std::string(data, data_len ? data_len - 1 : 0) << "'" << std::endl;
|
||||
else
|
||||
std::cout << "TYPE = " << type << ", data_mime_type = '" << data_mime_type << "', data len = " << data_len << std::endl;
|
||||
*/
|
||||
break;
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
Extractor::Extractor()
|
||||
: _plugins( nullptr )
|
||||
{
|
||||
// _plugins = EXTRACTOR_plugin_add_config (nullptr, "mp3:ogg:flac:wav:gstreamer", EXTRACTOR_OPTION_DEFAULT_POLICY);
|
||||
}
|
||||
|
||||
Extractor::~Extractor()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
Extractor::parse(const boost::filesystem::path& p, Items& items)
|
||||
{
|
||||
if (boost::filesystem::is_regular(p)) {
|
||||
_plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
|
||||
if (_plugins == nullptr) {
|
||||
throw std::runtime_error("EXTRACTOR_plugin_add_config failed!");
|
||||
}
|
||||
EXTRACTOR_extract (_plugins, p.string().c_str(), NULL, 0, &processMetaData, &items);
|
||||
EXTRACTOR_plugin_remove_all (_plugins);
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
Extractor::parseCover(const boost::filesystem::path& p, GenericData& data)
|
||||
{
|
||||
bool res = false;
|
||||
Items items;
|
||||
if (boost::filesystem::is_regular(p)) {
|
||||
_plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
|
||||
if (_plugins == nullptr) {
|
||||
throw std::runtime_error("EXTRACTOR_plugin_add_config failed!");
|
||||
}
|
||||
EXTRACTOR_extract (_plugins, p.string().c_str(), NULL, 0, &processMetaData, &items);
|
||||
EXTRACTOR_plugin_remove_all (_plugins);
|
||||
|
||||
if (items.find(MetaData::Cover) != items.end()) {
|
||||
data = boost::any_cast<GenericData>(items[MetaData::Cover]);
|
||||
res = true;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
} // namespace MetaData
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef EXTRACTOR_HPP
|
||||
#define EXTRACTOR_HPP
|
||||
|
||||
#include <extractor.h>
|
||||
|
||||
#include "MetaData.hpp"
|
||||
|
||||
|
||||
namespace MetaData
|
||||
{
|
||||
|
||||
// Implements GNU libextractor library
|
||||
class Extractor : public Parser
|
||||
{
|
||||
public:
|
||||
|
||||
Extractor();
|
||||
~Extractor();
|
||||
|
||||
void parse(const boost::filesystem::path& p, Items& items);
|
||||
|
||||
bool parseCover(const boost::filesystem::path& p, GenericData& data);
|
||||
|
||||
private:
|
||||
|
||||
struct EXTRACTOR_PluginList *_plugins;
|
||||
};
|
||||
|
||||
} // namespace MetaData
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef METADATA_HPP
|
||||
#define METADATA_HPP
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <boost/any.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace MetaData
|
||||
{
|
||||
|
||||
enum Type
|
||||
{
|
||||
Artist,
|
||||
Title,
|
||||
Album,
|
||||
Genre,
|
||||
Duration,
|
||||
TrackNumber,
|
||||
DiscNumber,
|
||||
CreationTime,
|
||||
Cover,
|
||||
};
|
||||
|
||||
// Used for Cover
|
||||
struct GenericData {
|
||||
std::string mimeType;
|
||||
std::vector<unsigned char> data;
|
||||
};
|
||||
|
||||
typedef std::map<Type, boost::any> Items;
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
|
||||
typedef std::shared_ptr<Parser> pointer;
|
||||
|
||||
virtual void parse(const boost::filesystem::path& p, Items& items) = 0;
|
||||
|
||||
};
|
||||
|
||||
} // namespace MetaData
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
namespace MetaData
|
||||
{
|
||||
|
||||
bool readAsPosixTime(const std::string& str, boost::posix_time::ptime& time)
|
||||
{
|
||||
const std::locale formats[] = {
|
||||
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%m-%d")),
|
||||
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%b-%d")),
|
||||
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%B-%d")),
|
||||
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y/%m/%d")),
|
||||
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%d.%m.%Y")),
|
||||
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%m")),
|
||||
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y/%m")),
|
||||
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y.%m")),
|
||||
std::locale(std::locale::classic(), new boost::posix_time::time_input_facet("%Y")),
|
||||
};
|
||||
for(size_t i=0; i < sizeof(formats)/sizeof(formats[0]); ++i)
|
||||
{
|
||||
std::istringstream iss(str);
|
||||
iss.imbue(formats[i]);
|
||||
if (iss >> time)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool readList(const std::string& str, const std::string& separators, std::list<std::string>& results)
|
||||
{
|
||||
std::string curStr;
|
||||
|
||||
BOOST_FOREACH(char c, str) {
|
||||
|
||||
if (separators.find(c) != std::string::npos) {
|
||||
results.push_back(curStr);
|
||||
curStr.clear();
|
||||
}
|
||||
else {
|
||||
if (curStr.empty() && std::isspace(c))
|
||||
continue;
|
||||
|
||||
curStr.push_back(c);
|
||||
}
|
||||
}
|
||||
|
||||
if (!curStr.empty())
|
||||
results.push_back(curStr);
|
||||
|
||||
return !str.empty();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef METADATA_UTILS_HPP
|
||||
#define METADATA_UTILS_HPP
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
namespace MetaData
|
||||
{
|
||||
|
||||
bool readAsPosixTime(const std::string& str, boost::posix_time::ptime& time);
|
||||
|
||||
bool readList(const std::string& str, const std::string& separators, std::list<std::string>& results);
|
||||
|
||||
template<typename T>
|
||||
static inline bool readAs(const std::string& str, T& data)
|
||||
{
|
||||
std::istringstream iss ( str );
|
||||
return iss >> data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user