Skip to content

Commit

Permalink
Better FFmpeg download progress reporting (#840)
Browse files Browse the repository at this point in the history
* Add FFmpeg download progress reporting to WPF

* Update translations

* Move expensive console writing off of FFmpeg download thread.
  • Loading branch information
ScrubN authored Oct 6, 2023
1 parent 8d7982d commit a7d3bc4
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 9 deletions.
35 changes: 29 additions & 6 deletions TwitchDownloaderCLI/Tools/FfmpegHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Mono.Unix;
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using TwitchDownloaderCLI.Modes.Arguments;
using Xabe.FFmpeg;
using Xabe.FFmpeg.Downloader;
Expand All @@ -24,8 +27,7 @@ private static void DownloadFfmpeg()
{
Console.Write("[INFO] - Downloading FFmpeg");

var progressHandler = new Progress<ProgressInfo>();
progressHandler.ProgressChanged += new XabeProgressHandler().OnProgressReceived;
using var progressHandler = new XabeProgressHandler();

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand Down Expand Up @@ -63,20 +65,41 @@ public static void DetectFfmpeg(string ffmpegPath)
Environment.Exit(1);
}

private class XabeProgressHandler
private sealed class XabeProgressHandler : IProgress<ProgressInfo>, IDisposable
{
private int _lastPercent = -1;
private readonly ConcurrentQueue<int> _percentQueue = new();
private readonly Timer _timer;

internal void OnProgressReceived(object sender, ProgressInfo e)
public XabeProgressHandler()
{
var percent = (int)(e.DownloadedBytes / (double)e.TotalBytes * 100);
_timer = new Timer(Callback, _percentQueue, 0, 100);

static void Callback(object state)
{
if (state is not ConcurrentQueue<int> { IsEmpty: false } queue) return;

var currentPercent = queue.Max();
Console.Write($"\r[INFO] - Downloading FFmpeg {currentPercent}%");
}
}

public void Report(ProgressInfo value)
{
var percent = (int)(value.DownloadedBytes / (double)value.TotalBytes * 100);

if (percent > _lastPercent)
{
_lastPercent = percent;
Console.Write($"\r[INFO] - Downloading FFmpeg {percent}%");
_percentQueue.Enqueue(percent);
}
}

public void Dispose()
{
_timer?.Dispose();
_percentQueue.Clear();
}
}
}
}
39 changes: 36 additions & 3 deletions TwitchDownloaderWPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net;
using System.Windows;
using TwitchDownloaderWPF.Properties;
using Xabe.FFmpeg;
using Xabe.FFmpeg.Downloader;

namespace TwitchDownloaderWPF
Expand Down Expand Up @@ -69,11 +70,16 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
Settings.Default.Save();
}

var currentVersion = Version.Parse("1.53.2");
Title = $"Twitch Downloader v{currentVersion}";

// TODO: extract FFmpeg handling to a dedicated service
if (!File.Exists("ffmpeg.exe"))
{
var oldTitle = Title;
try
{
await FFmpegDownloader.GetLatestVersion(FFmpegVersion.Full);
await FFmpegDownloader.GetLatestVersion(FFmpegVersion.Full, new FfmpegDownloadProgress());
}
catch (Exception ex)
{
Expand All @@ -88,10 +94,10 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
MessageBox.Show(ex.ToString(), Translations.Strings.VerboseErrorOutput, MessageBoxButton.OK, MessageBoxImage.Error);
}
}

Title = oldTitle;
}

Version currentVersion = new Version("1.53.2");
Title = $"Twitch Downloader v{currentVersion}";
AutoUpdater.InstalledVersion = currentVersion;
#if !DEBUG
if (AppContext.BaseDirectory.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)))
Expand All @@ -102,5 +108,32 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
AutoUpdater.Start("https://downloader-update.twitcharchives.workers.dev");
#endif
}

private class FfmpegDownloadProgress : IProgress<ProgressInfo>
{
private int _lastPercent = -1;

public void Report(ProgressInfo value)
{
var percent = (int)(value.DownloadedBytes / (double)value.TotalBytes * 100);

if (percent > _lastPercent)
{
var window = Application.Current.MainWindow;
if (window is null) return;

_lastPercent = percent;

var oldTitle = window.Title;
if (oldTitle.IndexOf('-') == -1) oldTitle += " -";

window.Title = string.Concat(
oldTitle.AsSpan(0, oldTitle.IndexOf('-')),
"- ",
string.Format(Translations.Strings.StatusDownloaderFFmpeg, percent.ToString())
);
}
}
}
}
}
9 changes: 9 additions & 0 deletions TwitchDownloaderWPF/Translations/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions TwitchDownloaderWPF/Translations/Strings.es.resx
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,7 @@
<data name="LabelVideosPerPage" xml:space="preserve">
<value>Videos per page:</value>
</data>
<data name="StatusDownloaderFFmpeg" xml:space="preserve">
<value>Downloading FFmpeg {0}%</value>
</data>
</root>
3 changes: 3 additions & 0 deletions TwitchDownloaderWPF/Translations/Strings.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,7 @@
<data name="LabelVideosPerPage" xml:space="preserve">
<value>Vidéos par page:</value>
</data>
<data name="StatusDownloaderFFmpeg" xml:space="preserve">
<value>Downloading FFmpeg {0}%</value>
</data>
</root>
3 changes: 3 additions & 0 deletions TwitchDownloaderWPF/Translations/Strings.pl.resx
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,7 @@
<data name="LabelVideosPerPage" xml:space="preserve">
<value>Videos per page:</value>
</data>
<data name="StatusDownloaderFFmpeg" xml:space="preserve">
<value>Downloading FFmpeg {0}%</value>
</data>
</root>
3 changes: 3 additions & 0 deletions TwitchDownloaderWPF/Translations/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -773,4 +773,7 @@
<data name="LabelVideosPerPage" xml:space="preserve">
<value>Videos per page:</value>
</data>
<data name="StatusDownloaderFFmpeg" xml:space="preserve">
<value>Downloading FFmpeg {0}%</value>
</data>
</root>
3 changes: 3 additions & 0 deletions TwitchDownloaderWPF/Translations/Strings.ru.resx
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,7 @@
<data name="LabelVideosPerPage" xml:space="preserve">
<value>Videos per page:</value>
</data>
<data name="StatusDownloaderFFmpeg" xml:space="preserve">
<value>Downloading FFmpeg {0}%</value>
</data>
</root>
3 changes: 3 additions & 0 deletions TwitchDownloaderWPF/Translations/Strings.tr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,7 @@
<data name="LabelVideosPerPage" xml:space="preserve">
<value>Videos per page:</value>
</data>
<data name="StatusDownloaderFFmpeg" xml:space="preserve">
<value>Downloading FFmpeg {0}%</value>
</data>
</root>
3 changes: 3 additions & 0 deletions TwitchDownloaderWPF/Translations/Strings.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -773,4 +773,7 @@
<data name="LabelVideosPerPage" xml:space="preserve">
<value>Videos per page:</value>
</data>
<data name="StatusDownloaderFFmpeg" xml:space="preserve">
<value>Downloading FFmpeg {0}%</value>
</data>
</root>

0 comments on commit a7d3bc4

Please sign in to comment.