Made some audio info fields now mandatory, otherwise consider parsing failed

This commit is contained in:
emeric
2025-11-03 08:48:25 +01:00
parent b25e3b9b1e
commit 9611cc09e3
10 changed files with 61 additions and 70 deletions
+18 -6
View File
@@ -37,18 +37,30 @@ namespace lms::audio::ffmpeg
const auto containerInfo{ audioFile.getContainerInfo() };
const auto bestStreamInfo{ audioFile.getBestStreamInfo() };
if (!bestStreamInfo)
throw AudioFileParsingException{ audioFile.getPath(), "Cannot find best audio stream" };
throw AudioFileParsingException{ "Cannot find best audio stream" };
if (!containerInfo.container)
throw AudioFileParsingException{ audioFile.getPath(), "Unhandled container type '" + containerInfo.containerName + "'" };
throw AudioFileParsingException{ "Unhandled container type '" + containerInfo.containerName + "'" };
if (!bestStreamInfo->codec)
throw AudioFileParsingException{ "Unhandled codec type '" + bestStreamInfo->codecName + "'" };
if (!bestStreamInfo->bitrate || *bestStreamInfo->bitrate == 0)
throw AudioFileParsingException{ "Cannot determine bitrate" };
if (!bestStreamInfo->channelCount || *bestStreamInfo->channelCount == 0)
throw AudioFileParsingException{ "Cannot determine channel count" };
if (!bestStreamInfo->sampleRate || *bestStreamInfo->sampleRate == 0)
throw AudioFileParsingException{ "Cannot determine sample rate" };
audioProperties.container = *containerInfo.container;
audioProperties.duration = containerInfo.duration;
audioProperties.codec = bestStreamInfo->codec;
audioProperties.bitrate = bestStreamInfo->bitrate;
audioProperties.codec = *bestStreamInfo->codec;
audioProperties.bitrate = *bestStreamInfo->bitrate;
audioProperties.channelCount = *bestStreamInfo->channelCount;
audioProperties.sampleRate = *bestStreamInfo->sampleRate;
audioProperties.bitsPerSample = bestStreamInfo->bitsPerSample;
audioProperties.channelCount = bestStreamInfo->channelCount;
audioProperties.sampleRate = bestStreamInfo->sampleRate;
return audioProperties;
}
+20 -4
View File
@@ -60,9 +60,20 @@ namespace lms::audio::taglib
// Common properties
audioProperties.bitrate = static_cast<std::size_t>(properties.bitrate() * 1000);
if (audioProperties.bitrate == 0)
throw AudioFileParsingException{ "Cannot determine bitrate" };
audioProperties.channelCount = static_cast<std::size_t>(properties.channels());
if (audioProperties.channelCount == 0)
throw AudioFileParsingException{ "Cannot determine channel count" };
audioProperties.duration = std::chrono::milliseconds{ properties.lengthInMilliseconds() };
if (audioProperties.duration == decltype(audioProperties.duration)::zero())
throw AudioFileParsingException{ "Cannot determine duration" };
audioProperties.sampleRate = static_cast<std::size_t>(properties.sampleRate());
if (audioProperties.sampleRate == 0)
throw AudioFileParsingException{ "Cannot determine sample rate" };
// Guess container from the file type
if (const auto* apeFile{ dynamic_cast<const ::TagLib::APE::File*>(&file) })
@@ -90,8 +101,7 @@ namespace lms::audio::taglib
audioProperties.codec = CodecType::WMA9Pro;
break;
case ::TagLib::ASF::Properties::Codec::Unknown:
audioProperties.codec = std::nullopt;
break;
throw AudioFileParsingException{ "Unhandled ASF codec type" };
}
audioProperties.bitsPerSample = asfFile->audioProperties()->bitsPerSample();
@@ -122,8 +132,7 @@ namespace lms::audio::taglib
audioProperties.codec = CodecType::ALAC;
break;
case ::TagLib::MP4::Properties::Codec::Unknown:
audioProperties.codec = std::nullopt;
break;
throw AudioFileParsingException{ "Unhandled MP4 codec type" };
}
audioProperties.bitsPerSample = mp4File->audioProperties()->bitsPerSample();
@@ -195,6 +204,13 @@ namespace lms::audio::taglib
audioProperties.codec = CodecType::WavPack;
audioProperties.bitsPerSample = wavPackFile->audioProperties()->bitsPerSample();
}
else
{
throw AudioFileParsingException{ "Unhandled file type" };
}
if (audioProperties.bitsPerSample && *audioProperties.bitsPerSample == 0)
audioProperties.bitsPerSample.reset();
return audioProperties;
}
+2 -8
View File
@@ -229,16 +229,10 @@ namespace lms::audio::taglib::utils
}
if (!file)
{
LMS_LOG(METADATA, ERROR, "File " << p << ": parsing failed");
throw AudioFileParsingException{ p, "Parsing failed" };
}
throw AudioFileParsingException{ "Parsing failed" };
if (!file->audioProperties())
{
LMS_LOG(METADATA, ERROR, "File " << p << ": no audio properties");
throw AudioFileNoAudioPropertiesException{ p };
}
throw AudioFileParsingException{ "No audio properties" };
return file;
}
+6 -6
View File
@@ -74,12 +74,12 @@ namespace lms::audio
struct AudioProperties
{
std::optional<ContainerType> container;
std::optional<CodecType> codec;
std::chrono::milliseconds duration{};
std::optional<unsigned> bitrate;
ContainerType container;
CodecType codec;
std::chrono::milliseconds duration;
unsigned bitrate;
unsigned channelCount;
unsigned sampleRate;
std::optional<unsigned> bitsPerSample;
std::optional<unsigned> channelCount;
std::optional<unsigned> sampleRate;
};
} // namespace lms::audio
@@ -43,19 +43,7 @@ namespace lms::audio
class AudioFileParsingException : public Exception
{
public:
AudioFileParsingException(const std::filesystem::path& path, std::string_view error = "")
: Exception{ error }, _path{ path } {}
const std::filesystem::path& getPath() const { return _path; }
private:
std::filesystem::path _path;
};
class AudioFileNoAudioPropertiesException : public AudioFileParsingException
{
public:
using AudioFileParsingException::AudioFileParsingException;
using Exception::Exception;
};
class IOException : public Exception
@@ -539,10 +539,6 @@ namespace lms::scanner
index++;
});
}
catch (const audio::AudioFileNoAudioPropertiesException&)
{
addError<NoAudioTrackFoundError>(getFilePath());
}
catch (const audio::IOException& e)
{
addError<IOScanError>(getFilePath(), e.getErrorCode());
@@ -672,11 +668,11 @@ namespace lms::scanner
track.modify()->setScanVersion(getScannerSettings().audioScanVersion);
// Audio properties
track.modify()->setBitrate(_file->audioProperties.bitrate ? *_file->audioProperties.bitrate : 0);
track.modify()->setBitrate(_file->audioProperties.bitrate);
track.modify()->setBitsPerSample(_file->audioProperties.bitsPerSample ? *_file->audioProperties.bitsPerSample : 0);
track.modify()->setChannelCount(_file->audioProperties.channelCount ? *_file->audioProperties.channelCount : 0);
track.modify()->setChannelCount(_file->audioProperties.channelCount);
track.modify()->setDuration(_file->audioProperties.duration);
track.modify()->setSampleRate(_file->audioProperties.sampleRate ? *_file->audioProperties.sampleRate : 0);
track.modify()->setSampleRate(_file->audioProperties.sampleRate);
track.modify()->setFileSize(getFileSize());
track.modify()->setLastWriteTime(getLastWriteTime());
@@ -120,10 +120,7 @@ namespace lms::api::subsonic
{
const auto audioFile{ audio::parseAudioFile(trackPath) };
if (!audioFile->getAudioProperties().codec)
throw RequestedDataNotFoundError{}; // TODO 404?
return isCodecCompatibleWithOutputFormat(*audioFile->getAudioProperties().codec, outputFormat);
return isCodecCompatibleWithOutputFormat(audioFile->getAudioProperties().codec, outputFormat);
}
catch (const audio::Exception& e)
{
+3 -6
View File
@@ -147,12 +147,9 @@ namespace lms::ui
{
if (const auto audioFile{ audio::parseAudioFile(track->getAbsoluteFilePath()) })
{
if (audioFile->getAudioProperties().codec)
{
releaseInfo->setCondition("if-has-codec", true);
releaseInfo->bindString("codec", audio::codecTypeToString(*audioFile->getAudioProperties().codec).c_str(), Wt::TextFormat::Plain);
break;
}
releaseInfo->setCondition("if-has-codec", true);
releaseInfo->bindString("codec", audio::codecTypeToString(audioFile->getAudioProperties().codec).c_str(), Wt::TextFormat::Plain);
break;
}
}
catch (const audio::Exception& e)
+2 -5
View File
@@ -133,11 +133,8 @@ namespace lms::ui::TrackListHelpers
{
if (const auto audioFile{ audio::parseAudioFile(track->getAbsoluteFilePath()) })
{
if (audioFile->getAudioProperties().codec)
{
trackInfo->setCondition("if-has-codec", true);
trackInfo->bindString("codec", audio::codecTypeToString(*audioFile->getAudioProperties().codec).c_str(), Wt::TextFormat::Plain);
}
trackInfo->setCondition("if-has-codec", true);
trackInfo->bindString("codec", audio::codecTypeToString(audioFile->getAudioProperties().codec).c_str(), Wt::TextFormat::Plain);
}
}
catch (const audio::Exception& e)
+5 -11
View File
@@ -39,19 +39,13 @@ namespace lms::audio
std::ostream& operator<<(std::ostream& os, const AudioProperties& audioProperties)
{
os << "\tDuration: " << std::fixed << std::setprecision(2) << std::chrono::duration_cast<std::chrono::duration<float>>(audioProperties.duration) << std::endl;
if (audioProperties.container)
os << "\tContainer: " << containerTypeToString(*audioProperties.container) << std::endl;
if (audioProperties.codec)
os << "\tCodec: " << codecTypeToString(*audioProperties.codec) << std::endl;
if (audioProperties.bitrate)
os << "\tBitrate: " << *audioProperties.bitrate << " bps" << std::endl;
os << "\tContainer: " << containerTypeToString(audioProperties.container) << std::endl;
os << "\tCodec: " << codecTypeToString(audioProperties.codec) << std::endl;
os << "\tBitrate: " << audioProperties.bitrate << " bps" << std::endl;
if (audioProperties.bitsPerSample)
os << "\tBitsPerSample: " << *audioProperties.bitsPerSample << std::endl;
if (audioProperties.channelCount)
os << "\tChannelCount: " << *audioProperties.channelCount << std::endl;
if (audioProperties.sampleRate)
os << "\tSampleRate: " << *audioProperties.sampleRate << std::endl;
os << "\tChannelCount: " << audioProperties.channelCount << std::endl;
os << "\tSampleRate: " << audioProperties.sampleRate << std::endl;
return os;
}