From 94d155cff2601a570419192f6ee64f8968fd2661 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Mon, 20 Jun 2022 15:41:37 -0600 Subject: [PATCH] Add support for Audible's new hierarchical chapters. --- Source/FileLiberator/DownloadDecryptBook.cs | 27 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Source/FileLiberator/DownloadDecryptBook.cs b/Source/FileLiberator/DownloadDecryptBook.cs index 79cc5bf8..906acca3 100644 --- a/Source/FileLiberator/DownloadDecryptBook.cs +++ b/Source/FileLiberator/DownloadDecryptBook.cs @@ -165,6 +165,8 @@ namespace FileLiberator LameConfig = GetLameOptions(config) }; + var chapters = getChapters(contentLic.ContentMetadata.ChapterInfo.Chapters).OrderBy(c => c.StartOffsetMs).ToList(); + if (config.AllowLibationFixup || outputFormat == OutputFormat.Mp3) { long startMs = dlOptions.TrimOutputToChapterLength ? @@ -172,15 +174,15 @@ namespace FileLiberator dlOptions.ChapterInfo = new AAXClean.ChapterInfo(TimeSpan.FromMilliseconds(startMs)); - for (int i = 0; i < contentLic.ContentMetadata.ChapterInfo.Chapters.Length; i++) + for (int i = 0; i < chapters.Count; i++) { - var chapter = contentLic.ContentMetadata.ChapterInfo.Chapters[i]; + var chapter = chapters[i]; long chapLenMs = chapter.LengthMs; if (i == 0) chapLenMs -= startMs; - if (config.StripAudibleBrandAudio && i == contentLic.ContentMetadata.ChapterInfo.Chapters.Length - 1) + if (config.StripAudibleBrandAudio && i == chapters.Count - 1) chapLenMs -= contentLic.ContentMetadata.ChapterInfo.BrandOutroDurationMs; dlOptions.ChapterInfo.AddChapter(chapter.Title, TimeSpan.FromMilliseconds(chapLenMs)); @@ -190,6 +192,25 @@ namespace FileLiberator return dlOptions; } + private List getChapters(IEnumerable chapters) + { + List chaps = chapters.ToList(); + + foreach (var c in chapters) + { + if (c.Chapters is not null) + { + var children = getChapters(c.Chapters); + + foreach (var child in children) + child.Title = string.IsNullOrEmpty(c.Title) ? child.Title : $"{c.Title}: {child.Title}"; + + chaps.AddRange(children); + } + } + return chaps; + } + private static void downloadValidation(LibraryBook libraryBook) { string errorString(string field)