Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model rework #21

Merged
merged 10 commits into from
Apr 21, 2017
Merged
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
17 changes: 9 additions & 8 deletions DemoConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using Tyrrrz.Extensions;
using YoutubeExplode.Models;

namespace YoutubeExplode.DemoConsole
{
Expand Down Expand Up @@ -53,19 +54,19 @@ private static async Task MainAsync()
Console.WriteLine('-'.Repeat(100));

// Print metadata
Console.WriteLine($"Id: {videoInfo.Id} | Title: {videoInfo.Title} | Author: {videoInfo.Author}");
Console.WriteLine($"Id: {videoInfo.Id} | Title: {videoInfo.Title} | Author: {videoInfo.Author.DisplayName}");

// Get the most preferable stream
Console.WriteLine("Looking for the best stream that has both video and audio tracks...");
var streamInfo = videoInfo.Streams
.Where(s => s.ContainsVideo && s.ContainsAudio)
.OrderBy(s => s.Quality)
Console.WriteLine("Looking for the best mixed stream...");
var streamInfo = videoInfo.MixedStreams
.OrderBy(s => s.VideoQuality)
.Last();
string normalizedFileSize = NormalizeFileSize(streamInfo.FileSize);
Console.WriteLine($"Quality: {streamInfo.QualityLabel} | Container: {streamInfo.ContainerType} | Size: {normalizedFileSize}");
string normalizedFileSize = NormalizeFileSize(streamInfo.ContentLength);
Console.WriteLine($"Quality: {streamInfo.VideoQualityLabel} | Container: {streamInfo.Container} | Size: {normalizedFileSize}");

// Compose file name, based on metadata
string fileName = $"{videoInfo.Title}.{streamInfo.QualityLabel}.{streamInfo.FileExtension}";
string fileExtension = streamInfo.Container.GetFileExtension();
string fileName = $"{videoInfo.Title}.{fileExtension}";

// Remove illegal characters from file name
fileName = fileName.Except(Path.GetInvalidFileNameChars());
Expand Down
2 changes: 2 additions & 0 deletions DemoWpf/ViewModels/IMainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using GalaSoft.MvvmLight.CommandWpf;
using YoutubeExplode.Models;
using YoutubeExplode.Models.ClosedCaptions;
using YoutubeExplode.Models.MediaStreams;

namespace YoutubeExplode.DemoWpf.ViewModels
{
Expand Down
11 changes: 7 additions & 4 deletions DemoWpf/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.Win32;
using Tyrrrz.Extensions;
using YoutubeExplode.Models;
using YoutubeExplode.Models.ClosedCaptions;
using YoutubeExplode.Models.MediaStreams;

namespace YoutubeExplode.DemoWpf.ViewModels
{
Expand Down Expand Up @@ -105,15 +107,16 @@ private async void GetVideoInfoAsync()
private async void DownloadMediaStreamAsync(MediaStreamInfo mediaStreamInfo)
{
// Create dialog
string defaultFileName = $"{VideoInfo.Title}.{mediaStreamInfo.QualityLabel}.{mediaStreamInfo.FileExtension}";
string fileExtension = mediaStreamInfo.Container.GetFileExtension();
string defaultFileName = $"{VideoInfo.Title}.{fileExtension}";
defaultFileName = defaultFileName.Except(Path.GetInvalidFileNameChars());
string fileFilter =
$"{mediaStreamInfo.ContainerType} Files|" +
$"*.{mediaStreamInfo.FileExtension}|All files|*.*";
$"{mediaStreamInfo.Container} Files|*.{fileExtension}|" +
"All files|*.*";
var sfd = new SaveFileDialog
{
AddExtension = true,
DefaultExt = mediaStreamInfo.FileExtension,
DefaultExt = fileExtension,
FileName = defaultFileName,
Filter = fileFilter
};
Expand Down
167 changes: 118 additions & 49 deletions DemoWpf/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@
<materialDesign:Card Margin="3,0,3,3">
<Expander Background="{DynamicResource MaterialDesignCardBackground}">
<Expander.Header>
<TextBlock FontSize="15">
<Run Text="Description" />
</TextBlock>
<TextBlock FontSize="15" Text="Description" />
</Expander.Header>
<TextBlock
Margin="10"
Expand All @@ -217,10 +215,7 @@
<materialDesign:Card Margin="3,0,3,3">
<Expander Background="{DynamicResource MaterialDesignCardBackground}">
<Expander.Header>
<TextBlock FontSize="15">
<Run Text="Keywords" />
<Run Text="(" /><Run Text="{Binding VideoInfo.Keywords.Count, Mode=OneWay}" /><Run Text=")" />
</TextBlock>
<TextBlock FontSize="15" Text="Keywords" />
</Expander.Header>
<TextBlock
Margin="10"
Expand All @@ -234,57 +229,131 @@
<materialDesign:Card Margin="3,0,3,3">
<Expander Background="{DynamicResource MaterialDesignCardBackground}">
<Expander.Header>
<TextBlock FontSize="15">
<Run Text="Media streams" />
<Run Text="(" /><Run Text="{Binding VideoInfo.Streams.Count, Mode=OneWay}" /><Run Text=")" />
</TextBlock>
<TextBlock FontSize="15" Text="Media streams" />
</Expander.Header>
<ItemsControl Margin="5" ItemsSource="{Binding VideoInfo.Streams}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- Download button -->
<Button
Padding="4"
VerticalAlignment="Center"
Command="{Binding MainViewModel.DownloadMediaStreamCommand, Source={StaticResource Locator}}"
CommandParameter="{Binding}"
Style="{DynamicResource MaterialDesignFlatButton}">
<materialDesign:PackIcon
Width="24"
Height="24"
Kind="Download" />
</Button>
<StackPanel Orientation="Vertical">
<TextBlock
Margin="10,5,5,5"
FontSize="14"
Text="Mixed streams:" />
<ItemsControl Margin="5,0,5,0" ItemsSource="{Binding VideoInfo.MixedStreams}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- Download button -->
<Button
Padding="4"
VerticalAlignment="Center"
Command="{Binding MainViewModel.DownloadMediaStreamCommand, Source={StaticResource Locator}}"
CommandParameter="{Binding}"
Style="{DynamicResource MaterialDesignFlatButton}">
<materialDesign:PackIcon
Width="24"
Height="24"
Kind="Download" />
</Button>

<!-- Stream info -->
<TextBlock Margin="5,0,0,0" VerticalAlignment="Center">
<Run Text="Quality:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding QualityLabel, Mode=OneWay}" />
<Run />
<Run Text="Content:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding ContentType, Mode=OneWay}" />
<Run />
<Run Text="Container:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding ContainerType, Mode=OneWay}" />
<Run />
<Run Text="Size:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding FileSize, Converter={StaticResource FileSizeToStringConverter}, StringFormat=\{0\}, Mode=OneWay}" />
</TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- Stream info -->
<TextBlock Margin="5,0,0,0" VerticalAlignment="Center">
<Run Text="Quality:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding VideoQualityLabel, Mode=OneWay}" />
<Run />
<Run Text="Container:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding Container, Mode=OneWay}" />
<Run />
<Run Text="Size:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding ContentLength, Converter={StaticResource FileSizeToStringConverter}, StringFormat=\{0\}, Mode=OneWay}" />
</TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

<TextBlock
Margin="10,5,5,5"
FontSize="14"
Text="Video-only streams:" />
<ItemsControl Margin="5,0,5,0" ItemsSource="{Binding VideoInfo.VideoStreams}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- Download button -->
<Button
Padding="4"
VerticalAlignment="Center"
Command="{Binding MainViewModel.DownloadMediaStreamCommand, Source={StaticResource Locator}}"
CommandParameter="{Binding}"
Style="{DynamicResource MaterialDesignFlatButton}">
<materialDesign:PackIcon
Width="24"
Height="24"
Kind="Download" />
</Button>

<!-- Stream info -->
<TextBlock Margin="5,0,0,0" VerticalAlignment="Center">
<Run Text="Quality:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding VideoQualityLabel, Mode=OneWay}" />
<Run />
<Run Text="Container:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding Container, Mode=OneWay}" />
<Run />
<Run Text="Bitrate:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding Bitrate, Converter={StaticResource FileSizeToStringConverter}, StringFormat=\{0\}it/s, Mode=OneWay}" />
<Run />
<Run Text="Size:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding ContentLength, Converter={StaticResource FileSizeToStringConverter}, StringFormat=\{0\}, Mode=OneWay}" />
</TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

<TextBlock
Margin="10,5,5,5"
FontSize="14"
Text="Audio-only streams:" />
<ItemsControl Margin="5,0,5,0" ItemsSource="{Binding VideoInfo.AudioStreams}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- Download button -->
<Button
Padding="4"
VerticalAlignment="Center"
Command="{Binding MainViewModel.DownloadMediaStreamCommand, Source={StaticResource Locator}}"
CommandParameter="{Binding}"
Style="{DynamicResource MaterialDesignFlatButton}">
<materialDesign:PackIcon
Width="24"
Height="24"
Kind="Download" />
</Button>

<!-- Stream info -->
<TextBlock Margin="5,0,0,0" VerticalAlignment="Center">
<Run Text="Container:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding Container, Mode=OneWay}" />
<Run />
<Run Text="Bitrate:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding Bitrate, Converter={StaticResource FileSizeToStringConverter}, StringFormat=\{0\}it/s, Mode=OneWay}" />
<Run />
<Run Text="Size:" />
<Run Foreground="{DynamicResource PrimaryTextBrush}" Text="{Binding ContentLength, Converter={StaticResource FileSizeToStringConverter}, StringFormat=\{0\}, Mode=OneWay}" />
</TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Expander>
</materialDesign:Card>

<!-- Closed captions -->
<materialDesign:Card Margin="3,0,3,3">
<Expander Background="{DynamicResource MaterialDesignCardBackground}">
<Expander.Header>
<TextBlock FontSize="15">
<Run Text="Closed captions" />
<Run Text="(" /><Run Text="{Binding VideoInfo.ClosedCaptionTracks.Count, Mode=OneWay}" /><Run Text=")" />
</TextBlock>
<TextBlock FontSize="15" Text="Closed captions" />
</Expander.Header>
<ItemsControl Margin="5" ItemsSource="{Binding VideoInfo.ClosedCaptionTracks}">
<ItemsControl.ItemTemplate>
Expand Down
Loading