Do not use exceptions when scan file error occurs
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
- handle access rights problems (instead of aborting)
|
- handle access rights problems (instead of aborting)
|
||||||
- add a global play counter for tracks. This will help people to spot most popular files
|
- add a global play counter for tracks. This will help people to spot most popular files
|
||||||
- rework the exception process in av/metadata/updater in case of bad files
|
- rework the exception process in av/metadata/updater in case of bad files
|
||||||
|
- Use the WServer::post method to notify the end of the database scan? (with results?)
|
||||||
|
|
||||||
[Metadata]
|
[Metadata]
|
||||||
- OGG metadata -> properly handle metadata nested in the audio stream
|
- OGG metadata -> properly handle metadata nested in the audio stream
|
||||||
|
|||||||
@@ -87,14 +87,17 @@ InputFormatContext::getBestStreamIdx(AVMediaType type, Stream::Idx& index)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
bool
|
||||||
InputFormatContext::findStreamInfo(void)
|
InputFormatContext::findStreamInfo(void)
|
||||||
{
|
{
|
||||||
AvError err = avformat_find_stream_info(native(), NULL);
|
AvError err = avformat_find_stream_info(native(), NULL);
|
||||||
if (err) {
|
if (err)
|
||||||
|
{
|
||||||
LMS_LOG(MOD_AV, SEV_ERROR) << "Couldn't find stream information: " << err;
|
LMS_LOG(MOD_AV, SEV_ERROR) << "Couldn't find stream information: " << err;
|
||||||
throw std::runtime_error("av_find_stream_info failed!");
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ class InputFormatContext : public FormatContext
|
|||||||
Dictionary getMetadata(void); // metadata access
|
Dictionary getMetadata(void); // metadata access
|
||||||
|
|
||||||
// Scan file
|
// Scan file
|
||||||
void findStreamInfo();
|
bool findStreamInfo();
|
||||||
|
|
||||||
|
|
||||||
// Get attached pictures
|
// Get attached pictures
|
||||||
|
|||||||
@@ -386,7 +386,9 @@ Updater::processAudioFile( const boost::filesystem::path& file, Stats& stats)
|
|||||||
}
|
}
|
||||||
|
|
||||||
MetaData::Items items;
|
MetaData::Items items;
|
||||||
_metadataParser.parse(file, items);
|
|
||||||
|
if (!_metadataParser.parse(file, items))
|
||||||
|
return;
|
||||||
|
|
||||||
// We estimate this is a audio file if:
|
// We estimate this is a audio file if:
|
||||||
// - we found a least one audio stream
|
// - we found a least one audio stream
|
||||||
|
|||||||
+114
-122
@@ -32,154 +32,146 @@
|
|||||||
namespace MetaData
|
namespace MetaData
|
||||||
{
|
{
|
||||||
|
|
||||||
void
|
bool
|
||||||
AvFormat::parse(const boost::filesystem::path& p, Items& items)
|
AvFormat::parse(const boost::filesystem::path& p, Items& items)
|
||||||
{
|
{
|
||||||
|
|
||||||
try {
|
Av::InputFormatContext input(p);
|
||||||
|
|
||||||
Av::InputFormatContext input(p);
|
if (!input.findStreamInfo())
|
||||||
input.findStreamInfo(); // needed by input.getDurationSecs
|
return false;
|
||||||
|
|
||||||
std::map<std::string, std::string> metadata;
|
std::map<std::string, std::string> metadata;
|
||||||
input.getMetadata().get(metadata);
|
input.getMetadata().get(metadata);
|
||||||
|
|
||||||
// HACK or OGG files
|
// HACK or OGG files
|
||||||
// If we did not find tags, searched metadata in streams
|
// If we did not find tags, searched metadata in streams
|
||||||
if (metadata.empty())
|
if (metadata.empty())
|
||||||
|
{
|
||||||
|
// Get input streams
|
||||||
|
std::vector<Av::Stream> streams = input.getStreams();
|
||||||
|
|
||||||
|
BOOST_FOREACH(Av::Stream& stream, streams)
|
||||||
{
|
{
|
||||||
// Get input streams
|
stream.getMetadata().get(metadata);
|
||||||
std::vector<Av::Stream> streams = input.getStreams();
|
|
||||||
|
|
||||||
BOOST_FOREACH(Av::Stream& stream, streams)
|
if (!metadata.empty())
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Stream info
|
||||||
|
{
|
||||||
|
std::vector<Av::Stream> avStreams = input.getStreams();
|
||||||
|
|
||||||
|
std::vector<AudioStream> audioStreams;
|
||||||
|
std::vector<VideoStream> videoStreams;
|
||||||
|
std::vector<SubtitleStream> subtitleStreams;
|
||||||
|
|
||||||
|
BOOST_FOREACH(Av::Stream& avStream, avStreams)
|
||||||
|
{
|
||||||
|
switch(avStream.getCodecContext().getType())
|
||||||
{
|
{
|
||||||
stream.getMetadata().get(metadata);
|
case AVMEDIA_TYPE_VIDEO:
|
||||||
|
if (!avStream.hasAttachedPic())
|
||||||
|
{
|
||||||
|
VideoStream stream;
|
||||||
|
stream.bitRate = avStream.getCodecContext().getBitRate();
|
||||||
|
videoStreams.push_back(stream);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
if (!metadata.empty())
|
case AVMEDIA_TYPE_AUDIO:
|
||||||
|
{
|
||||||
|
AudioStream stream;
|
||||||
|
stream.nbChannels = avStream.getCodecContext().getNbChannels();
|
||||||
|
stream.bitRate = avStream.getCodecContext().getBitRate();
|
||||||
|
audioStreams.push_back(stream);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case AVMEDIA_TYPE_SUBTITLE:
|
||||||
|
{
|
||||||
|
subtitleStreams.push_back( SubtitleStream() );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!videoStreams.empty())
|
||||||
|
items.insert( std::make_pair(MetaData::Type::VideoStreams, videoStreams));
|
||||||
|
if (!audioStreams.empty())
|
||||||
|
items.insert( std::make_pair(MetaData::Type::AudioStreams, audioStreams));
|
||||||
|
if (!subtitleStreams.empty())
|
||||||
|
items.insert( std::make_pair(MetaData::Type::SubtitleStreams, subtitleStreams));
|
||||||
|
|
||||||
// Stream info
|
}
|
||||||
{
|
|
||||||
std::vector<Av::Stream> avStreams = input.getStreams();
|
|
||||||
|
|
||||||
std::vector<AudioStream> audioStreams;
|
// Duration
|
||||||
std::vector<VideoStream> videoStreams;
|
items.insert( std::make_pair(MetaData::Type::Duration, boost::posix_time::time_duration( boost::posix_time::seconds( input.getDurationSecs() )) ));
|
||||||
std::vector<SubtitleStream> subtitleStreams;
|
|
||||||
|
|
||||||
BOOST_FOREACH(Av::Stream& avStream, avStreams)
|
// Cover
|
||||||
{
|
items.insert( std::make_pair(MetaData::Type::HasCover, input.getNbPictures() > 0));
|
||||||
switch(avStream.getCodecContext().getType())
|
|
||||||
{
|
|
||||||
case AVMEDIA_TYPE_VIDEO:
|
|
||||||
if (!avStream.hasAttachedPic())
|
|
||||||
{
|
|
||||||
VideoStream stream;
|
|
||||||
stream.bitRate = avStream.getCodecContext().getBitRate();
|
|
||||||
videoStreams.push_back(stream);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case AVMEDIA_TYPE_AUDIO:
|
|
||||||
{
|
|
||||||
AudioStream stream;
|
|
||||||
stream.nbChannels = avStream.getCodecContext().getNbChannels();
|
|
||||||
stream.bitRate = avStream.getCodecContext().getBitRate();
|
|
||||||
audioStreams.push_back(stream);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case AVMEDIA_TYPE_SUBTITLE:
|
|
||||||
{
|
|
||||||
subtitleStreams.push_back( SubtitleStream() );
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!videoStreams.empty())
|
|
||||||
items.insert( std::make_pair(MetaData::Type::VideoStreams, videoStreams));
|
|
||||||
if (!audioStreams.empty())
|
|
||||||
items.insert( std::make_pair(MetaData::Type::AudioStreams, audioStreams));
|
|
||||||
if (!subtitleStreams.empty())
|
|
||||||
items.insert( std::make_pair(MetaData::Type::SubtitleStreams, subtitleStreams));
|
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
if (boost::iequals(it->first, "artist"))
|
||||||
|
items.insert( std::make_pair(MetaData::Type::Artist, string_trim( string_to_utf8(it->second)) ));
|
||||||
|
else if (boost::iequals(it->first, "album"))
|
||||||
|
items.insert( std::make_pair(MetaData::Type::Album, string_trim( string_to_utf8(it->second)) ));
|
||||||
|
else if (boost::iequals(it->first, "title"))
|
||||||
|
items.insert( std::make_pair(MetaData::Type::Title, string_trim( string_to_utf8(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::Type::TrackNumber, number ));
|
||||||
}
|
}
|
||||||
|
else if (boost::iequals(it->first, "disc"))
|
||||||
// Duration
|
|
||||||
items.insert( std::make_pair(MetaData::Type::Duration, boost::posix_time::time_duration( boost::posix_time::seconds( input.getDurationSecs() )) ));
|
|
||||||
|
|
||||||
// Cover
|
|
||||||
items.insert( std::make_pair(MetaData::Type::HasCover, input.getNbPictures() > 0));
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
{
|
{
|
||||||
if (boost::iequals(it->first, "artist"))
|
std::size_t number;
|
||||||
items.insert( std::make_pair(MetaData::Type::Artist, string_trim( string_to_utf8(it->second)) ));
|
if (readAs<std::size_t>(it->second, number))
|
||||||
else if (boost::iequals(it->first, "album"))
|
items.insert( std::make_pair(MetaData::Type::DiscNumber, number ));
|
||||||
items.insert( std::make_pair(MetaData::Type::Album, string_trim( string_to_utf8(it->second)) ));
|
}
|
||||||
else if (boost::iequals(it->first, "title"))
|
else if (boost::iequals(it->first, "date")
|
||||||
items.insert( std::make_pair(MetaData::Type::Title, string_trim( string_to_utf8(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::Type::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::Type::DiscNumber, number ));
|
|
||||||
}
|
|
||||||
else if (boost::iequals(it->first, "date")
|
|
||||||
|| boost::iequals(it->first, "year")
|
|| boost::iequals(it->first, "year")
|
||||||
|| boost::iequals(it->first, "WM/Year"))
|
|| boost::iequals(it->first, "WM/Year"))
|
||||||
{
|
{
|
||||||
boost::posix_time::ptime p;
|
boost::posix_time::ptime p;
|
||||||
if (readAsPosixTime(it->second, p))
|
if (readAsPosixTime(it->second, p))
|
||||||
items.insert( std::make_pair(MetaData::Type::Date, 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
|
|
||||||
{
|
|
||||||
boost::posix_time::ptime p;
|
|
||||||
if (readAsPosixTime(it->second, p))
|
|
||||||
items.insert( std::make_pair(MetaData::Type::OriginalDate, p));
|
|
||||||
}
|
|
||||||
else if (boost::iequals(it->first, "genre"))
|
|
||||||
{
|
|
||||||
std::list<std::string> genres;
|
|
||||||
if (readList(it->second, ";,", genres))
|
|
||||||
items.insert( std::make_pair(MetaData::Type::Genres, genres));
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (boost::iequals(it->first, "MusicBrainz Artist Id"))
|
|
||||||
{
|
|
||||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzArtistID, string_trim( string_to_utf8(it->second)) ));
|
|
||||||
}
|
|
||||||
else if (boost::iequals(it->first, "MusicBrainz Album Id"))
|
|
||||||
{
|
|
||||||
items.insert( std::make_pair(MetaData::Type::MusicBrainzAlbumID, string_trim( string_to_utf8(it->second)) ));
|
|
||||||
}
|
|
||||||
/* else
|
|
||||||
LMS_LOG(MOD_METADATA, SEV_DEBUG) << "key = " << it->first << ", value = " << it->second;
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
else if (boost::iequals(it->first, "TDOR") // Original release time (ID3v2 2.4)
|
||||||
|
|| boost::iequals(it->first, "TORY")) // Original release year
|
||||||
|
{
|
||||||
|
boost::posix_time::ptime p;
|
||||||
|
if (readAsPosixTime(it->second, p))
|
||||||
|
items.insert( std::make_pair(MetaData::Type::OriginalDate, p));
|
||||||
|
}
|
||||||
|
else if (boost::iequals(it->first, "genre"))
|
||||||
|
{
|
||||||
|
std::list<std::string> genres;
|
||||||
|
if (readList(it->second, ";,", genres))
|
||||||
|
items.insert( std::make_pair(MetaData::Type::Genres, genres));
|
||||||
|
|
||||||
}
|
}
|
||||||
catch(std::exception &e)
|
else if (boost::iequals(it->first, "MusicBrainz Artist Id"))
|
||||||
{
|
{
|
||||||
LMS_LOG(MOD_METADATA, SEV_ERROR) << "Parsing of '" << p << "' failed!";
|
items.insert( std::make_pair(MetaData::Type::MusicBrainzArtistID, string_trim( string_to_utf8(it->second)) ));
|
||||||
|
}
|
||||||
|
else if (boost::iequals(it->first, "MusicBrainz Album Id"))
|
||||||
|
{
|
||||||
|
items.insert( std::make_pair(MetaData::Type::MusicBrainzAlbumID, string_trim( string_to_utf8(it->second)) ));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace MetaData
|
} // namespace MetaData
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class AvFormat : public Parser
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void parse(const boost::filesystem::path& p, Items& items);
|
bool parse(const boost::filesystem::path& p, Items& items);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ namespace MetaData
|
|||||||
|
|
||||||
typedef std::shared_ptr<Parser> pointer;
|
typedef std::shared_ptr<Parser> pointer;
|
||||||
|
|
||||||
virtual void parse(const boost::filesystem::path& p, Items& items) = 0;
|
virtual bool parse(const boost::filesystem::path& p, Items& items) = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <boost/foreach.hpp>
|
|
||||||
|
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
|
|
||||||
@@ -47,7 +46,9 @@ InputMediaFile::InputMediaFile(const boost::filesystem::path& p)
|
|||||||
: _path(p)
|
: _path(p)
|
||||||
{
|
{
|
||||||
Av::InputFormatContext input(_path);
|
Av::InputFormatContext input(_path);
|
||||||
input.findStreamInfo();
|
|
||||||
|
if (!input.findStreamInfo())
|
||||||
|
throw std::runtime_error("Cannot find stream info in file: " + p.string());
|
||||||
|
|
||||||
// Calculate estimated duration
|
// Calculate estimated duration
|
||||||
if (input.getDurationSecs())
|
if (input.getDurationSecs())
|
||||||
@@ -83,7 +84,7 @@ InputMediaFile::InputMediaFile(const boost::filesystem::path& p)
|
|||||||
|
|
||||||
avMediaTypes.unique();
|
avMediaTypes.unique();
|
||||||
// Scan for best streams
|
// Scan for best streams
|
||||||
BOOST_FOREACH(enum AVMediaType type, avMediaTypes)
|
for (enum AVMediaType type : avMediaTypes)
|
||||||
{
|
{
|
||||||
Av::Stream::Idx index;
|
Av::Stream::Idx index;
|
||||||
|
|
||||||
@@ -102,7 +103,7 @@ std::vector<Stream>
|
|||||||
InputMediaFile::getStreams(Stream::Type type) const
|
InputMediaFile::getStreams(Stream::Type type) const
|
||||||
{
|
{
|
||||||
std::vector<Stream> res;
|
std::vector<Stream> res;
|
||||||
BOOST_FOREACH(const Stream& stream, _streams)
|
for (const Stream& stream : _streams)
|
||||||
{
|
{
|
||||||
if (stream.getType() == type)
|
if (stream.getType() == type)
|
||||||
res.push_back(stream);
|
res.push_back(stream);
|
||||||
@@ -113,7 +114,7 @@ InputMediaFile::getStreams(Stream::Type type) const
|
|||||||
const Stream&
|
const Stream&
|
||||||
InputMediaFile::getStream(Stream::Id index) const
|
InputMediaFile::getStream(Stream::Id index) const
|
||||||
{
|
{
|
||||||
BOOST_FOREACH(const Stream& stream, _streams)
|
for (const Stream& stream : _streams)
|
||||||
{
|
{
|
||||||
if (stream.getId() == index)
|
if (stream.getId() == index)
|
||||||
return stream;
|
return stream;
|
||||||
|
|||||||
Reference in New Issue
Block a user