Simplified download decrypt progress update.

Removed unused fields and properties.
This commit is contained in:
Michael Bucari-Tovo 2021-06-24 18:09:43 -06:00
parent 792207caee
commit 0c159df6ca
2 changed files with 12 additions and 37 deletions

View File

@ -144,8 +144,7 @@ namespace FileLiberator.AaxcDownloadDecrypt
var aaxcProcesser = new FFMpegAaxcProcesser(DecryptSupportLibraries.ffmpegPath); var aaxcProcesser = new FFMpegAaxcProcesser(DecryptSupportLibraries.ffmpegPath);
aaxcProcesser.ProgressUpdate += (_, e) => aaxcProcesser.ProgressUpdate += AaxcProcesser_ProgressUpdate;
DecryptProgressUpdate?.Invoke(this, (int)e.ProgressPercent);
aaxcProcesser.ProcessBook( aaxcProcesser.ProcessBook(
downloadLicense.DownloadUrl, downloadLicense.DownloadUrl,
@ -160,6 +159,13 @@ namespace FileLiberator.AaxcDownloadDecrypt
return aaxcProcesser.Succeeded; return aaxcProcesser.Succeeded;
} }
private void AaxcProcesser_ProgressUpdate(object sender, TimeSpan e)
{
double progressPercent = Math.Max(100 * e.TotalSeconds / tags.duration.TotalSeconds, 1);
DecryptProgressUpdate?.Invoke(this, (int)progressPercent);
}
private static string GenerateFfmpegChapters(ChapterInfo chapters) private static string GenerateFfmpegChapters(ChapterInfo chapters)
{ {
var stringBuilder = new System.Text.StringBuilder(); var stringBuilder = new System.Text.StringBuilder();

View File

@ -6,27 +6,19 @@ using System.Threading.Tasks;
namespace FileLiberator.AaxcDownloadDecrypt namespace FileLiberator.AaxcDownloadDecrypt
{ {
public class AaxcProgress : EventArgs
{
public TimeSpan ProcessedTime { get; set; }
public TimeSpan AudioDuration { get; set; }
public double ProgressPercent => Math.Round(100 * ProcessedTime.TotalSeconds / AudioDuration.TotalSeconds,2);
}
/// <summary> /// <summary>
/// Download audible aaxc, decrypt, remux, add metadata, and insert cover art. /// Download audible aaxc, decrypt, remux, add metadata, and insert cover art.
/// </summary> /// </summary>
class FFMpegAaxcProcesser class FFMpegAaxcProcesser
{ {
public event EventHandler<AaxcProgress> ProgressUpdate; public event EventHandler<TimeSpan> ProgressUpdate;
public string FFMpegPath { get; } public string FFMpegPath { get; }
public bool IsRunning { get; set; } = false; public bool IsRunning { get; set; } = false;
public bool Succeeded { get; private set; } public bool Succeeded { get; private set; }
private static Regex processedTimeRegex = new Regex("time=(\\d{2}):(\\d{2}):(\\d{2}).\\d{2}", RegexOptions.IgnoreCase | RegexOptions.Compiled); private static Regex processedTimeRegex = new Regex("time=(\\d{2}):(\\d{2}):(\\d{2}).\\d{2}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static Regex durationRegex = new Regex("Duration: (\\d{2}):(\\d{2}):(\\d{2}).\\d{2}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private TimeSpan duration { get; set; }
private TimeSpan position { get; set; }
public FFMpegAaxcProcesser(string ffmpegPath) public FFMpegAaxcProcesser(string ffmpegPath)
{ {
FFMpegPath = ffmpegPath; FFMpegPath = ffmpegPath;
@ -50,11 +42,9 @@ namespace FileLiberator.AaxcDownloadDecrypt
IsRunning = true; IsRunning = true;
downloader.ErrorDataReceived += Downloader_ErrorDataReceived;
remuxer.ErrorDataReceived += Remuxer_ErrorDataReceived; remuxer.ErrorDataReceived += Remuxer_ErrorDataReceived;
downloader.Start(); downloader.Start();
downloader.BeginErrorReadLine();
var pipedOutput = downloader.StandardOutput.BaseStream; var pipedOutput = downloader.StandardOutput.BaseStream;
@ -91,22 +81,6 @@ namespace FileLiberator.AaxcDownloadDecrypt
IsRunning = false; IsRunning = false;
Succeeded = downloader.ExitCode == 0 && remuxer.ExitCode == 0; Succeeded = downloader.ExitCode == 0 && remuxer.ExitCode == 0;
} }
private void Downloader_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data) && durationRegex.IsMatch(e.Data))
{
//get total audio stream duration
var match = durationRegex.Match(e.Data);
int hours = int.Parse(match.Groups[1].Value);
int minutes = int.Parse(match.Groups[2].Value);
int seconds = int.Parse(match.Groups[3].Value);
duration = new TimeSpan(hours, minutes, seconds);
}
}
private void Remuxer_ErrorDataReceived(object sender, DataReceivedEventArgs e) private void Remuxer_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{ {
if (!string.IsNullOrEmpty(e.Data) && processedTimeRegex.IsMatch(e.Data)) if (!string.IsNullOrEmpty(e.Data) && processedTimeRegex.IsMatch(e.Data))
@ -118,13 +92,9 @@ namespace FileLiberator.AaxcDownloadDecrypt
int minutes = int.Parse(match.Groups[2].Value); int minutes = int.Parse(match.Groups[2].Value);
int seconds = int.Parse(match.Groups[3].Value); int seconds = int.Parse(match.Groups[3].Value);
position = new TimeSpan(hours, minutes, seconds); var position = new TimeSpan(hours, minutes, seconds);
ProgressUpdate?.Invoke(sender, new AaxcProgress ProgressUpdate?.Invoke(sender, position);
{
ProcessedTime = position,
AudioDuration = duration
});
} }
} }
@ -133,7 +103,6 @@ namespace FileLiberator.AaxcDownloadDecrypt
{ {
FileName = FFMpegPath, FileName = FFMpegPath,
RedirectStandardOutput = true, RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true, CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden, WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false, UseShellExecute = false,