Add support for locating mp3 audiobooks

This commit is contained in:
MBucari 2023-08-20 11:38:43 -06:00
parent 51b8cfe71f
commit 92d283187d

View File

@ -8,6 +8,7 @@ using Dinah.Core;
using System.Threading.Tasks;
using System.Threading;
using FileManager;
using AaxDecrypter;
#nullable enable
namespace LibationFileManager
@ -113,7 +114,7 @@ namespace LibationFileManager
{
RecurseSubdirectories = true,
IgnoreInaccessible = true,
MatchCasing = MatchCasing.CaseInsensitive
AttributesToSkip = FileAttributes.Hidden,
};
protected override LongPath? GetFilePathCustom(string productId)
@ -157,22 +158,40 @@ namespace LibationFileManager
{
ArgumentValidator.EnsureNotNull(searchDirectory, nameof(searchDirectory));
foreach (LongPath path in Directory.EnumerateFiles(searchDirectory, "*.M4B", enumerationOptions))
foreach (LongPath path in Directory.EnumerateFiles(searchDirectory, "*.*", enumerationOptions))
{
if (cancellationToken.IsCancellationRequested)
yield break;
if (getFormatByExtension(path) is not OutputFormat format)
continue;
FilePathCache.CacheEntry? audioFile = default;
try
{
using var fileStream = File.OpenRead(path);
if (format is OutputFormat.M4b)
{
var mp4File = await Task.Run(() => new AAXClean.Mp4File(fileStream), cancellationToken);
if (mp4File?.AppleTags?.Asin is not null)
audioFile = new FilePathCache.CacheEntry(mp4File.AppleTags.Asin, FileType.Audio, path);
}
else
{
var id3 = NAudio.Lame.ID3.Id3Tag.Create(fileStream);
var asin
= id3?.Children
.OfType<NAudio.Lame.ID3.TXXXFrame>()
.FirstOrDefault(f => f.FieldName == "AUDIBLE_ASIN")
?.FieldValue;
if (!string.IsNullOrWhiteSpace(asin))
audioFile = new FilePathCache.CacheEntry(asin, FileType.Audio, path);
}
}
catch (Exception ex)
{
@ -186,6 +205,15 @@ namespace LibationFileManager
if (audioFile is not null)
yield return audioFile;
}
static OutputFormat? getFormatByExtension(string path)
{
var ext = Path.GetExtension(path).ToLower();
return ext == ".mp3" ? OutputFormat.Mp3
: ext == ".m4b" ? OutputFormat.M4b
: null;
}
}
}
}