Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions WpfDemoApp/App.config

This file was deleted.

4 changes: 2 additions & 2 deletions WpfDemoApp/App.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Application x:Class="WpfDemoApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
</Application>
StartupUri="Views/MainWindow.xaml">
</Application>
9 changes: 0 additions & 9 deletions WpfDemoApp/InformationWindow.xaml

This file was deleted.

53 changes: 0 additions & 53 deletions WpfDemoApp/MainWindow.xaml

This file was deleted.

107 changes: 0 additions & 107 deletions WpfDemoApp/MainWindow.xaml.cs

This file was deleted.

110 changes: 110 additions & 0 deletions WpfDemoApp/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows;
using WpfDemoApp.Views;
using YoutubeDLSharp;
using YoutubeDLSharp.Options;

namespace WpfDemoApp.ViewModels
{
public partial class MainWindowViewModel : ObservableObject
{
private YoutubeDL _youtubeDL = new();
private IProgress<DownloadProgress> _progress;
private IProgress<string> _output;

[ObservableProperty]
private bool _audioOnly = false;

[ObservableProperty]
private string _url = "";

[ObservableProperty]
private string _options = "";

[ObservableProperty]
private string _state = "";

[ObservableProperty]
private float _progressValue = 0;

[ObservableProperty]
private string _progressText = "";

[ObservableProperty]
private string _outputText = "";

public string Version => _youtubeDL.Version;

public MainWindowViewModel()
{
_progress = new Progress<DownloadProgress>(ShowDownloadProgress);
_output = new Progress<string>(ShowDownloadOutput);
}

[RelayCommand]
private async Task Download()
{
await YoutubeDLSharp.Utils.DownloadBinaries();

RunResult<string> result;

var options = Options.Split('\n');
var custom = OptionSet.FromString(options);
// it seems like yt-dlp doesn't output progress bar by default
custom.Progress = true;

OutputText = "";
if (AudioOnly)
{
result = await _youtubeDL.RunAudioDownload(Url, AudioConversionFormat.Mp3, progress: _progress, output: _output, overrideOptions: custom);
}
else
{
result = await _youtubeDL.RunVideoDownload(Url, progress: _progress, output: _output, overrideOptions: custom);
}

if (result.Success)
{
MessageBox.Show($"Successfully downloaded \"{Url}\" to:\n\"{result.Data}\".", "YoutubeDLSharp");
}
else
{
ShowErrorMessage(Url, string.Join("\n", result.ErrorOutput));
}
}

[RelayCommand]
private async Task FetchInfo()
{
await YoutubeDLSharp.Utils.DownloadBinaries();

var result = await _youtubeDL.RunVideoDataFetch(Url);
if (result.Success)
{
// doesn't follow MVVM pattern but project simple enough to use this trick
var informationWindow = new InformationWindow(result.Data);
informationWindow.ShowDialog();
}
else ShowErrorMessage(Url, string.Join("\n", result.ErrorOutput));
}

private void ShowDownloadProgress(DownloadProgress progress)
{
State = progress.State.ToString();
ProgressValue = progress.Progress;
ProgressText = $"speed: {progress.DownloadSpeed} | left: {progress.ETA}";
}

private void ShowDownloadOutput(string str)
{
if (str.Contains("[download]")) return;

OutputText += (str + Environment.NewLine);
}

private void ShowErrorMessage(string url, string error)
=> MessageBox.Show($"Failed to process '{url}'. Output:\n\n{error}", "YoutubeDLSharp - ERROR",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
15 changes: 15 additions & 0 deletions WpfDemoApp/Views/InformationWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Window
x:Class="WpfDemoApp.Views.InformationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:metadata="clr-namespace:YoutubeDLSharp.Metadata;assembly=YoutubeDLSharp"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=metadata:VideoData}"
Title="{Binding Title}" Height="400" Width="400"
WindowStartupLocation="CenterOwner">
<Grid>
<TextBox IsReadOnly="True" VerticalScrollBarVisibility="Auto" Text="{Binding Mode=OneWay}" />
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Windows;
using System.Windows;
using YoutubeDLSharp.Metadata;

namespace WpfDemoApp
namespace WpfDemoApp.Views
{
public partial class InformationWindow : Window
{
public InformationWindow(VideoData videoData)
{
this.DataContext = videoData;
InitializeComponent();
DataContext = videoData;
}
}
}
}
59 changes: 59 additions & 0 deletions WpfDemoApp/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<Window
x:Class="WpfDemoApp.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vms="clr-namespace:WpfDemoApp.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vms:MainWindowViewModel}"
Title="YoutubeDLSharp" SizeToContent="Height" Width="400" ResizeMode="CanMinimize">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Padding" Value="4" />
</Style>
</Window.Resources>
<DockPanel>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem ContentStringFormat="yt-dlp Version: {0}"
Content="{Binding Version, Mode=OneWay}" />
</StatusBar>
<Grid Margin="4">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Text="{Binding Url}" Margin="0,12" FontSize="14" />
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center"
Content="Audio Only"
IsChecked="{Binding AudioOnly}" />
<Button Grid.Column="1" Command="{Binding DownloadCommand}"
Content="Download" />
<Button Grid.Column="2" Command="{Binding FetchInfoCommand}"
Content="Information" />
</Grid>
<Expander Grid.Row="2" Header="Custom Arguments">
<TextBox AcceptsReturn="True" Text="{Binding Options}" VerticalScrollBarVisibility="Auto" Height="60" />
</Expander>
<StackPanel Grid.Row="3" Margin="0,12">
<ProgressBar Value="{Binding ProgressValue}" Height="20" Margin="0"
Maximum="1" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding State}" Margin="0,0,8,0" />
<TextBlock Text="{Binding ProgressText}" />
</StackPanel>
</StackPanel>
<TextBox Text="{Binding OutputText, Mode=OneWay}" Grid.Row="4" IsReadOnly="True"
Height="100" VerticalScrollBarVisibility="Visible" />
</Grid>
</DockPanel>
</Window>
Loading