Skip to content

Commit

Permalink
🔜 Parse output and show download progress
Browse files Browse the repository at this point in the history
- ParseDlOutput: percentage, size, speed, ETA
- Show percentage string in download button text
- Show percentage in download button progress bar
- Removed some unnecessary indeterminate progress bar animations
  • Loading branch information
database64128 committed Sep 24, 2020
1 parent da05b1f commit 68a05a2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 5 deletions.
110 changes: 108 additions & 2 deletions YoutubeDl.Wpf/ViewModels/HomeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ public HomeViewModel(ISnackbarMessageQueue snackbarMessageQueue)
_downloadPath = "";
_output = "";

_freezeButton = false;
_downloadButtonProgressIndeterminate = false;
_formatsButtonProgressIndeterminate = false;
_downloadButtonProgressPercentageValue = 0.0; // 99 for 99%
_downloadButtonProgressPercentageString = "_Download";
_fileSizeString = "";
_downloadSpeedString = "";
_downloadETAString = "";

outputSeparators = new string[]
{
"[download]",
"of",
"at",
"ETA",
" "
};

_browseFolder = new DelegateCommand(OnBrowseFolder);
_openFolder = new DelegateCommand(OnOpenFolder, CanOpenFolder);
_startDownload = new DelegateCommand(OnStartDownload, CanStartDownload);
Expand Down Expand Up @@ -116,7 +134,16 @@ public HomeViewModel(ISnackbarMessageQueue snackbarMessageQueue)
private string _downloadPath;
private string _output;

private bool _freezeButton; // true when youtube-dl is started
private bool _freezeButton;
private bool _downloadButtonProgressIndeterminate;
private bool _formatsButtonProgressIndeterminate;
private double _downloadButtonProgressPercentageValue;
private string _downloadButtonProgressPercentageString;
private string _fileSizeString;
private string _downloadSpeedString;
private string _downloadETAString;

private readonly string[] outputSeparators;
private StringBuilder outputString = null!;
private Process dlProcess = null!;

Expand Down Expand Up @@ -198,6 +225,9 @@ private void DlProcess_Exited(object? sender, EventArgs e)
{
dlProcess.Dispose();
FreezeButton = false;
DownloadButtonProgressIndeterminate = false;
FormatsButtonProgressIndeterminate = false;
DownloadButtonProgressPercentageString = "_Download";
Application.Current.Dispatcher.Invoke(UpdateButtons);
}

Expand Down Expand Up @@ -238,6 +268,7 @@ private void OnOpenFolder(object commandParameter)
private void OnStartDownload(object commandParameter)
{
FreezeButton = true;
DownloadButtonProgressIndeterminate = true;
UpdateButtons();

outputString = new StringBuilder();
Expand Down Expand Up @@ -315,6 +346,7 @@ private void OnStartDownload(object commandParameter)
private void OnListFormats(object commandParameter)
{
FreezeButton = true;
FormatsButtonProgressIndeterminate = true;
UpdateButtons();

outputString = new StringBuilder();
Expand Down Expand Up @@ -381,6 +413,8 @@ private bool CanStartDownload(object commandParameter)
private void UpdateDl()
{
FreezeButton = true;
DownloadButtonProgressIndeterminate = true;
FormatsButtonProgressIndeterminate = true;
UpdateButtons();

outputString = new StringBuilder();
Expand Down Expand Up @@ -413,14 +447,44 @@ private void UpdateDl()

private void DlOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
if (!string.IsNullOrEmpty(outLine.Data))
{
ParseDlOutput(outLine.Data);
outputString.Append(outLine.Data);
outputString.Append(Environment.NewLine);
Output = outputString.ToString();
}
}

private void ParseDlOutput(string output)
{
var parsedStringArray = output.Split(outputSeparators, StringSplitOptions.RemoveEmptyEntries);
if (parsedStringArray.Length == 4) // valid [download] line
{
var parsedPercentageString = parsedStringArray[0];
if (parsedPercentageString.EndsWith('%')) // actual percentage
{
// show percentage on button
DownloadButtonProgressPercentageString = parsedPercentageString;
// get percentage value for progress bar
parsedPercentageString = parsedPercentageString.TrimEnd('%');
try
{
DownloadButtonProgressPercentageValue = double.Parse(parsedPercentageString);
DownloadButtonProgressIndeterminate = false;
}
catch
{

}
}
// save other info
FileSizeString = parsedStringArray[1];
DownloadSpeedString = parsedStringArray[2];
DownloadETAString = parsedStringArray[3];
}
}

public string Link
{
get => _link;
Expand Down Expand Up @@ -556,6 +620,48 @@ public bool FreezeButton
get => _freezeButton;
set => SetProperty(ref _freezeButton, value);
}

public bool DownloadButtonProgressIndeterminate
{
get => _downloadButtonProgressIndeterminate;
set => SetProperty(ref _downloadButtonProgressIndeterminate, value);
}

public bool FormatsButtonProgressIndeterminate
{
get => _formatsButtonProgressIndeterminate;
set => SetProperty(ref _formatsButtonProgressIndeterminate, value);
}

public double DownloadButtonProgressPercentageValue
{
get => _downloadButtonProgressPercentageValue;
set => SetProperty(ref _downloadButtonProgressPercentageValue, value);
}

public string DownloadButtonProgressPercentageString
{
get => _downloadButtonProgressPercentageString;
set => SetProperty(ref _downloadButtonProgressPercentageString, value);
}

public string FileSizeString
{
get => _fileSizeString;
set => SetProperty(ref _fileSizeString, value);
}

public string DownloadSpeedString
{
get => _downloadSpeedString;
set => SetProperty(ref _downloadSpeedString, value);
}

public string DownloadETAString
{
get => _downloadETAString;
set => SetProperty(ref _downloadETAString, value);
}
}

/// <summary>
Expand Down
8 changes: 5 additions & 3 deletions YoutubeDl.Wpf/Views/HomeView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@
x:Name="downloadButton"
Margin="8 0 8 0"
materialDesign:ButtonProgressAssist.IsIndicatorVisible="{Binding FreezeButton}"
materialDesign:ButtonProgressAssist.IsIndeterminate="{Binding FreezeButton}"
Command="{Binding StartDownload}">_Download</Button>
materialDesign:ButtonProgressAssist.IsIndeterminate="{Binding DownloadButtonProgressIndeterminate}"
materialDesign:ButtonProgressAssist.Value="{Binding DownloadButtonProgressPercentageValue}"
Command="{Binding StartDownload}"
Content="{Binding DownloadButtonProgressPercentageString}"/>
<Button
x:Name="listFormatsButton"
Margin="8 0 8 0"
materialDesign:ButtonProgressAssist.IsIndicatorVisible="{Binding FreezeButton}"
materialDesign:ButtonProgressAssist.IsIndeterminate="{Binding FreezeButton}"
materialDesign:ButtonProgressAssist.IsIndeterminate="{Binding FormatsButtonProgressIndeterminate}"
Style="{StaticResource MaterialDesignRaisedAccentButton}"
Foreground="White"
Command="{Binding ListFormats}">_List Formats</Button>
Expand Down
1 change: 1 addition & 0 deletions YoutubeDl.Wpf/YoutubeDl.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<RepositoryUrl>https://github.com/database64128/youtube-dl-wpf</RepositoryUrl>
<RepositoryType>Public</RepositoryType>
<Nullable>enable</Nullable>
<AssemblyName>youtube-dl-wpf</AssemblyName>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 68a05a2

Please sign in to comment.