Upgrade AAXClean.Codecs to 0.5.10 and fix #459

This commit is contained in:
Michael Bucari-Tovo 2023-01-23 14:54:24 -07:00
parent d0727b5a85
commit 7029409792
8 changed files with 57 additions and 45 deletions

View File

@ -13,7 +13,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AAXClean.Codecs" Version="0.5.0" /> <PackageReference Include="AAXClean.Codecs" Version="0.5.10" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -10,6 +10,7 @@ namespace AaxDecrypter
public event EventHandler<AppleTags> RetrievedMetadata; public event EventHandler<AppleTags> RetrievedMetadata;
protected AaxFile AaxFile; protected AaxFile AaxFile;
protected Mp4Operation aaxConversion;
protected AaxcDownloadConvertBase(string outFileName, string cacheDirectory, IDownloadOptions dlOptions) protected AaxcDownloadConvertBase(string outFileName, string cacheDirectory, IDownloadOptions dlOptions)
: base(outFileName, cacheDirectory, dlOptions) { } : base(outFileName, cacheDirectory, dlOptions) { }
@ -101,9 +102,9 @@ namespace AaxDecrypter
public override async Task CancelAsync() public override async Task CancelAsync()
{ {
IsCanceled = true; IsCanceled = true;
if (AaxFile != null) if (aaxConversion != null)
await AaxFile.CancelAsync(); await aaxConversion.CancelAsync();
AaxFile?.Dispose(); AaxFile?.Close();
CloseInputFileStream(); CloseInputFileStream();
} }
} }

View File

@ -133,32 +133,33 @@ That naming may not be desirable for everyone, but it's an easy change to instea
try try
{ {
ConversionResult result;
AaxFile.ConversionProgressUpdate += AaxFile_ConversionProgressUpdate;
if (DownloadOptions.OutputFormat == OutputFormat.M4b) if (DownloadOptions.OutputFormat == OutputFormat.M4b)
result = await ConvertToMultiMp4a(splitChapters); aaxConversion = ConvertToMultiMp4a(splitChapters);
else else
result = await ConvertToMultiMp3(splitChapters); aaxConversion = ConvertToMultiMp3(splitChapters);
return result == ConversionResult.NoErrorsDetected; aaxConversion.ConversionProgressUpdate += AaxFile_ConversionProgressUpdate;
await aaxConversion;
return aaxConversion.IsCompletedSuccessfully;
} }
catch(Exception ex) catch(Exception ex)
{ {
Serilog.Log.Error(ex, "AAXClean Error"); Serilog.Log.Error(ex, "AAXClean Error");
workingFileStream?.Close(); workingFileStream?.Close();
FileUtility.SaferDelete(workingFileStream.Name); if (workingFileStream?.Name is not null)
FileUtility.SaferDelete(workingFileStream.Name);
return false; return false;
} }
finally finally
{ {
AaxFile.ConversionProgressUpdate -= AaxFile_ConversionProgressUpdate; if (aaxConversion is not null)
aaxConversion.ConversionProgressUpdate -= AaxFile_ConversionProgressUpdate;
Step_DownloadAudiobook_End(zeroProgress); Step_DownloadAudiobook_End(zeroProgress);
} }
} }
private Task<ConversionResult> ConvertToMultiMp4a(ChapterInfo splitChapters) private Mp4Operation ConvertToMultiMp4a(ChapterInfo splitChapters)
{ {
var chapterCount = 0; var chapterCount = 0;
return AaxFile.ConvertToMultiMp4aAsync return AaxFile.ConvertToMultiMp4aAsync
@ -169,7 +170,7 @@ That naming may not be desirable for everyone, but it's an easy change to instea
); );
} }
private Task<ConversionResult> ConvertToMultiMp3(ChapterInfo splitChapters) private Mp4Operation ConvertToMultiMp3(ChapterInfo splitChapters)
{ {
var chapterCount = 0; var chapterCount = 0;
return AaxFile.ConvertToMultiMp3Async return AaxFile.ConvertToMultiMp3Async

View File

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using AAXClean; using AAXClean;
using AAXClean.Codecs; using AAXClean.Codecs;
using FileManager; using FileManager;
using Mpeg4Lib.Util;
namespace AaxDecrypter namespace AaxDecrypter
{ {
@ -90,16 +91,20 @@ namespace AaxDecrypter
var outputFile = File.Open(OutputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); var outputFile = File.Open(OutputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
OnFileCreated(OutputFileName); OnFileCreated(OutputFileName);
AaxFile.ConversionProgressUpdate += AaxFile_ConversionProgressUpdate;
try try
{ {
ConversionResult decryptionResult = await decryptAsync(outputFile); aaxConversion = decryptAsync(outputFile);
var success = decryptionResult == ConversionResult.NoErrorsDetected && !IsCanceled; aaxConversion.ConversionProgressUpdate += AaxFile_ConversionProgressUpdate;
if (success) await aaxConversion;
base.OnFileCreated(OutputFileName);
return success; if (aaxConversion.IsCompletedSuccessfully)
{
outputFile.Close();
base.OnFileCreated(OutputFileName);
}
return aaxConversion.IsCompletedSuccessfully;
} }
catch(Exception ex) catch(Exception ex)
{ {
@ -110,13 +115,15 @@ namespace AaxDecrypter
finally finally
{ {
outputFile.Close(); outputFile.Close();
AaxFile.ConversionProgressUpdate -= AaxFile_ConversionProgressUpdate;
if (aaxConversion is not null)
aaxConversion.ConversionProgressUpdate += AaxFile_ConversionProgressUpdate;
Step_DownloadAudiobook_End(zeroProgress); Step_DownloadAudiobook_End(zeroProgress);
} }
} }
private Task<ConversionResult> decryptAsync(Stream outputFile) private Mp4Operation decryptAsync(Stream outputFile)
=> DownloadOptions.OutputFormat == OutputFormat.Mp3 ? => DownloadOptions.OutputFormat == OutputFormat.Mp3 ?
AaxFile.ConvertToMp3Async AaxFile.ConvertToMp3Async
( (

View File

@ -15,12 +15,12 @@ namespace FileLiberator
public class ConvertToMp3 : AudioDecodable public class ConvertToMp3 : AudioDecodable
{ {
public override string Name => "Convert to Mp3"; public override string Name => "Convert to Mp3";
private Mp4File m4bBook; private Mp4Operation Mp4Operation;
private TimeSpan bookDuration;
private long fileSize; private long fileSize;
private static string Mp3FileName(string m4bPath) => Path.ChangeExtension(m4bPath ?? "", ".mp3"); private static string Mp3FileName(string m4bPath) => Path.ChangeExtension(m4bPath ?? "", ".mp3");
public override Task CancelAsync() => m4bBook?.CancelAsync() ?? Task.CompletedTask; public override Task CancelAsync() => Mp4Operation?.CancelAsync() ?? Task.CompletedTask;
public static bool ValidateMp3(LibraryBook libraryBook) public static bool ValidateMp3(LibraryBook libraryBook)
{ {
@ -43,9 +43,9 @@ namespace FileLiberator
var proposedMp3Path = Mp3FileName(m4bPath); var proposedMp3Path = Mp3FileName(m4bPath);
if (File.Exists(proposedMp3Path) || !File.Exists(m4bPath)) continue; if (File.Exists(proposedMp3Path) || !File.Exists(m4bPath)) continue;
m4bBook = await Task.Run(() => new Mp4File(m4bPath, FileAccess.Read)); var m4bBook = await Task.Run(() => new Mp4File(m4bPath, FileAccess.Read));
m4bBook.ConversionProgressUpdate += M4bBook_ConversionProgressUpdate;
bookDuration = m4bBook.Duration;
fileSize = m4bBook.InputStream.Length; fileSize = m4bBook.InputStream.Length;
OnTitleDiscovered(m4bBook.AppleTags.Title); OnTitleDiscovered(m4bBook.AppleTags.Title);
@ -66,20 +66,20 @@ namespace FileLiberator
using var mp3File = File.OpenWrite(Path.GetTempFileName()); using var mp3File = File.OpenWrite(Path.GetTempFileName());
try try
{ {
var result = await m4bBook.ConvertToMp3Async(mp3File, lameConfig); Mp4Operation = m4bBook.ConvertToMp3Async(mp3File, lameConfig);
Mp4Operation.ConversionProgressUpdate += M4bBook_ConversionProgressUpdate;
await Mp4Operation;
var realMp3Path = FileUtility.SaferMoveToValidPath(mp3File.Name, proposedMp3Path, Configuration.Instance.ReplacementCharacters); if (Mp4Operation.IsCanceled)
OnFileCreated(libraryBook, realMp3Path);
if (result == ConversionResult.Failed)
{
FileUtility.SaferDelete(mp3File.Name);
}
else if (result == ConversionResult.Cancelled)
{ {
FileUtility.SaferDelete(mp3File.Name); FileUtility.SaferDelete(mp3File.Name);
return new StatusHandler { "Cancelled" }; return new StatusHandler { "Cancelled" };
} }
else
{
var realMp3Path = FileUtility.SaferMoveToValidPath(mp3File.Name, proposedMp3Path, Configuration.Instance.ReplacementCharacters, "mp3");
OnFileCreated(libraryBook, realMp3Path);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -88,6 +88,9 @@ namespace FileLiberator
} }
finally finally
{ {
if (Mp4Operation is not null)
Mp4Operation.ConversionProgressUpdate -= M4bBook_ConversionProgressUpdate;
m4bBook.InputStream.Close(); m4bBook.InputStream.Close();
mp3File.Close(); mp3File.Close();
} }
@ -102,14 +105,13 @@ namespace FileLiberator
private void M4bBook_ConversionProgressUpdate(object sender, ConversionProgressEventArgs e) private void M4bBook_ConversionProgressUpdate(object sender, ConversionProgressEventArgs e)
{ {
var duration = m4bBook.Duration; var remainingSecsToProcess = (bookDuration - e.ProcessPosition).TotalSeconds;
var remainingSecsToProcess = (duration - e.ProcessPosition).TotalSeconds;
var estTimeRemaining = remainingSecsToProcess / e.ProcessSpeed; var estTimeRemaining = remainingSecsToProcess / e.ProcessSpeed;
if (double.IsNormal(estTimeRemaining)) if (double.IsNormal(estTimeRemaining))
OnStreamingTimeRemaining(TimeSpan.FromSeconds(estTimeRemaining)); OnStreamingTimeRemaining(TimeSpan.FromSeconds(estTimeRemaining));
double progressPercent = 100 * e.ProcessPosition.TotalSeconds / duration.TotalSeconds; double progressPercent = 100 * e.ProcessPosition.TotalSeconds / bookDuration.TotalSeconds;
OnStreamingProgressChanged( OnStreamingProgressChanged(
new DownloadProgress new DownloadProgress

View File

@ -151,9 +151,9 @@ namespace FileManager
/// <br/>- Perform <see cref="SaferMove"/> /// <br/>- Perform <see cref="SaferMove"/>
/// <br/>- Return valid path /// <br/>- Return valid path
/// </summary> /// </summary>
public static string SaferMoveToValidPath(LongPath source, LongPath destination, ReplacementCharacters replacements) public static string SaferMoveToValidPath(LongPath source, LongPath destination, ReplacementCharacters replacements, string extension = null)
{ {
var extension = Path.GetExtension(source); extension = extension ?? Path.GetExtension(source);
destination = GetValidFilename(destination, replacements, extension); destination = GetValidFilename(destination, replacements, extension);
SaferMove(source, destination); SaferMove(source, destination);
return destination; return destination;

View File

@ -107,9 +107,9 @@ namespace LibationFileManager
.GetFilePath(fileExtension).PathWithoutPrefix; .GetFilePath(fileExtension).PathWithoutPrefix;
public const string DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; public const string DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
private static Regex fileDateTagRegex { get; } = new Regex(@"<file\s*?date\s*?(?:\[([^\[\]]*?)\]){0,1}\s*?>", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static Regex fileDateTagRegex { get; } = new Regex(@"<file\s*?date\s*?(?:\[([^\[\]]*?)\]\s*?)?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static Regex dateAddedTagRegex { get; } = new Regex(@"<date\s*?added\s*?(?:\[([^\[\]]*?)\]){0,1}\s*?>", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static Regex dateAddedTagRegex { get; } = new Regex(@"<date\s*?added\s*?(?:\[([^\[\]]*?)\]\s*?)?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static Regex datePublishedTagRegex { get; } = new Regex(@"<pub\s*?date\s*?(?:\[([^\[\]]*?)\]){0,1}\s*?>", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static Regex datePublishedTagRegex { get; } = new Regex(@"<pub\s*?date\s*?(?:\[([^\[\]]*?)\]\s*?)?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static Regex ifSeriesRegex { get; } = new Regex("<if series->(.*?)<-if series>", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static Regex ifSeriesRegex { get; } = new Regex("<if series->(.*?)<-if series>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
internal static FileNamingTemplate getFileNamingTemplate(LibraryBookDto libraryBookDto, string template, string dirFullPath, string extension, ReplacementCharacters replacements) internal static FileNamingTemplate getFileNamingTemplate(LibraryBookDto libraryBookDto, string template, string dirFullPath, string extension, ReplacementCharacters replacements)

View File

@ -153,6 +153,7 @@ namespace TemplatesTests
[DataRow("<filedate[h]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate[h].m4b")] [DataRow("<filedate[h]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate[h].m4b")]
[DataRow("< filedate[yyyy]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\ filedate[yyyy].m4b")] [DataRow("< filedate[yyyy]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\ filedate[yyyy].m4b")]
[DataRow("<filedate[yyyy][]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate[yyyy][].m4b")] [DataRow("<filedate[yyyy][]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate[yyyy][].m4b")]
[DataRow("<filedate[[yyyy]]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate[[yyyy]].m4b")]
[DataRow("<filedate[yyyy[]]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate[yyyy[]].m4b")] [DataRow("<filedate[yyyy[]]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate[yyyy[]].m4b")]
[DataRow("<filedate yyyy]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate yyyy].m4b")] [DataRow("<filedate yyyy]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate yyyy].m4b")]
[DataRow("<filedate ]yyyy]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate ]yyyy].m4b")] [DataRow("<filedate ]yyyy]>", @"C:\foo\bar", ".m4b", @"C:\foo\bar\filedate ]yyyy].m4b")]