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.Tasks;
using System.Threading; using System.Threading;
using FileManager; using FileManager;
using AaxDecrypter;
#nullable enable #nullable enable
namespace LibationFileManager namespace LibationFileManager
@ -113,7 +114,7 @@ namespace LibationFileManager
{ {
RecurseSubdirectories = true, RecurseSubdirectories = true,
IgnoreInaccessible = true, IgnoreInaccessible = true,
MatchCasing = MatchCasing.CaseInsensitive AttributesToSkip = FileAttributes.Hidden,
}; };
protected override LongPath? GetFilePathCustom(string productId) protected override LongPath? GetFilePathCustom(string productId)
@ -157,22 +158,40 @@ namespace LibationFileManager
{ {
ArgumentValidator.EnsureNotNull(searchDirectory, nameof(searchDirectory)); 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) if (cancellationToken.IsCancellationRequested)
yield break; yield break;
if (getFormatByExtension(path) is not OutputFormat format)
continue;
FilePathCache.CacheEntry? audioFile = default; FilePathCache.CacheEntry? audioFile = default;
try try
{ {
using var fileStream = File.OpenRead(path); using var fileStream = File.OpenRead(path);
if (format is OutputFormat.M4b)
{
var mp4File = await Task.Run(() => new AAXClean.Mp4File(fileStream), cancellationToken); var mp4File = await Task.Run(() => new AAXClean.Mp4File(fileStream), cancellationToken);
if (mp4File?.AppleTags?.Asin is not null) if (mp4File?.AppleTags?.Asin is not null)
audioFile = new FilePathCache.CacheEntry(mp4File.AppleTags.Asin, FileType.Audio, path); 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) catch (Exception ex)
{ {
@ -186,6 +205,15 @@ namespace LibationFileManager
if (audioFile is not null) if (audioFile is not null)
yield return audioFile; yield return audioFile;
} }
static OutputFormat? getFormatByExtension(string path)
{
var ext = Path.GetExtension(path).ToLower();
return ext == ".mp3" ? OutputFormat.Mp3
: ext == ".m4b" ? OutputFormat.M4b
: null;
}
} }
} }
} }