Skip to content

Commit

Permalink
Merge pull request #5 from CodesDoWork/MoreMetadataFlexibility
Browse files Browse the repository at this point in the history
More metadata flexibility
  • Loading branch information
CodesDoWork authored Jan 10, 2022
2 parents ece61a1 + b592398 commit b6cf444
Show file tree
Hide file tree
Showing 21 changed files with 383 additions and 104 deletions.
4 changes: 2 additions & 2 deletions Installer/Product.wxs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<?define releaseFolder="..\Releases\1.1.2\MediaDownloader\bin" ?>
<?define releaseFolder="..\Releases\1.2.0\MediaDownloader\bin" ?>
<?define sourceFolder="..\MediaDownloader" ?>
<Product Id="*" Name="Media Downloader" Language="1033" Version="1.1.2" Manufacturer="CodesDoWork" UpgradeCode="DF9618DD-729E-427F-BA34-D99EAA20D02C">
<Product Id="*" Name="Media Downloader" Language="1033" Version="1.2.0" Manufacturer="CodesDoWork" UpgradeCode="DF9618DD-729E-427F-BA34-D99EAA20D02C">
<Package Languages="1033" Manufacturer="CodesDoWork" InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
<Media Id="1" Cabinet="md_installer.cab" EmbedCab="yes" />

Expand Down
54 changes: 35 additions & 19 deletions MediaDownloader/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
x:Class="MediaDownloader.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:MediaDownloader.Converters"
xmlns:views="clr-namespace:MediaDownloader.Views"
StartupUri="/Windows/MainWindow.xaml">
<Application.Resources>
Expand All @@ -15,6 +16,8 @@
M13.5 2c-5.288 0-9.649 3.914-10.377 9h-3.123l4 5.917 4-5.917h-2.847c.711-3.972 4.174-7 8.347-7 4.687 0 8.5 3.813 8.5 8.5s-3.813 8.5-8.5 8.5c-3.015 0-5.662-1.583-7.171-3.957l-1.2 1.775c1.916 2.536 4.948 4.182 8.371 4.182 5.797 0 10.5-4.702 10.5-10.5s-4.703-10.5-10.5-10.5z
</Geometry>

<converters:BooleanToggleConverter x:Key="BooleanToggleConverter" />

<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="FontSize" Value="14" />
Expand Down Expand Up @@ -189,29 +192,36 @@
</Style>

<Style TargetType="DataGrid">
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="VerticalGridLinesBrush" Value="Transparent" />
<Setter Property="HorizontalGridLinesBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="AutoGenerateColumns" Value="False" />
<Setter Property="CanUserResizeRows" Value="False" />
<Setter Property="CanUserReorderColumns" Value="False" />
<Setter Property="CanUserResizeColumns" Value="False" />
<Setter Property="HorizontalGridLinesBrush" Value="Transparent" />
<Setter Property="AlternationCount" Value="2" />
<Setter Property="AlternatingRowBackground" Value="WhiteSmoke" />
<Setter Property="RowHeaderWidth" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="AutoGenerateColumns" Value="False" />
</Style>

<Style TargetType="DataGridColumnHeader">
<Setter Property="Padding" Value="0,10" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="FontWeight" Value="DemiBold" />
</Style>

<Style TargetType="DataGridCell">
<Setter Property="Margin" Value="0,10,0,0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="FontSize" Value="16" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="IsHitTestVisible" Value="False" />
<Setter Property="Background" Value="Transparent" />
<Style.Resources>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Padding" Value="10,5" />
<Setter Property="FontWeight" Value="DemiBold" />
</Style>
<Style TargetType="DataGridCell">
<Setter Property="Margin" Value="0,2" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Background="{TemplateBinding Background}" BorderThickness="0">
<ContentPresenter VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>

<Style TargetType="ContextMenu">
Expand All @@ -223,6 +233,12 @@
</Style.Resources>
</Style>

<Style TargetType="CheckBox">
<Setter Property="FontSize" Value="14" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Margin" Value="10" />
</Style>

<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Gainsboro" />
</Style>
Expand Down
19 changes: 19 additions & 0 deletions MediaDownloader/Converters/BooleanToggleConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Globalization;
using System.Windows.Data;

namespace MediaDownloader.Converters
{
internal class BooleanToggleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool) value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool) value;
}
}
}
2 changes: 2 additions & 0 deletions MediaDownloader/Data/DatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public DatabaseContext() : base("name=MediaDownloaderDb") { }
public static DatabaseContext DBConnection { get; } = new();

public virtual DbSet<SavedDownload> SavedDownloads { get; set; }

public virtual DbSet<TitleModifier> TitleModifiers { get; set; }
}
}
41 changes: 28 additions & 13 deletions MediaDownloader/Data/Download.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using MediaDownloader.Download;
using MediaDownloader.Properties;
using MediaDownloader.Utils;
Expand All @@ -14,9 +15,13 @@ public class Download

public static NotifyProperty<decimal> OverallPercentage = new();

private Downloader _downloader;

public Download() : this(null) { }

public Download(string url = null, string storagePath = null)
{
Url = url;
Url.Value = url;
StoragePath = storagePath ?? new KnownFolder(KnownFolderType.Downloads).Path;
Percentage.Listeners.Add(
_ =>
Expand All @@ -38,15 +43,11 @@ public Download(string url = null, string storagePath = null)
);
}

public string Url { get; set; }
public NotifyProperty<string> Url { get; } = new();

public string StoragePath { get; }

private Downloader Downloader { get; set; }

public NotifyProperty<DownloadType> DownloadType { get; } = new() {Value = Data.DownloadType.Audio};

public NotifyProperty<VideoQuality> VideoQuality { get; } = new() {Value = Data.VideoQuality.Best};
public DownloadSettings DownloadSettings { get; } = new();

public NotifyProperty<string> Destination { get; } = new();

Expand All @@ -62,16 +63,18 @@ public Download(string url = null, string storagePath = null)

public NotifyProperty<string> TimeRemaining { get; } = new();

public NotifyProperty<DownloadStatus> CurrentStatus { get; } = new() {Value = DownloadStatus.Preparing};
public NotifyProperty<DownloadStatus> CurrentStatus { get; } = new(DownloadStatus.Preparing);

public MetadataSettings MetadataSettings { get; } = new();

public bool Start()
{
if (string.IsNullOrWhiteSpace(Url))
if (string.IsNullOrWhiteSpace(Url.Value))
{
return false;
}

if (!IsValidUrl(Url))
if (!IsValidUrl(Url.Value))
{
ShowInfo(Resources.EnterValidURL);
return false;
Expand All @@ -84,14 +87,26 @@ public bool Start()


Percentage.Value = 0;
Downloader = Downloader.GetDownloaderFor(this);
Downloader.Start();
_downloader = Downloader.GetDownloaderFor(this);

BackgroundWorker worker = new();
worker.DoWork += (_, _) => _downloader.Start();
worker.RunWorkerAsync();
CurrentDownloads.Add(this);

return true;
}

public void Abort() { Downloader.Abort(); }
public void Abort() { _downloader.Abort(); }

public Download New()
{
Download download = new();
download.DownloadSettings.CopyFrom(DownloadSettings);
download.MetadataSettings.CopyFrom(MetadataSettings);

return download;
}

public void OnDownloadAction()
{
Expand Down
24 changes: 24 additions & 0 deletions MediaDownloader/Data/DownloadSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using MediaDownloader.Utils;

namespace MediaDownloader.Data
{
public class DownloadSettings
{
public DownloadSettings()
{
DownloadType.Listeners.Add(value => IsVideoDownload.Value = value == Data.DownloadType.Video);
}

public NotifyProperty<DownloadType> DownloadType { get; } = new(Data.DownloadType.Audio);

public NotifyProperty<bool> IsVideoDownload { get; } = new();

public NotifyProperty<VideoQuality> VideoQuality { get; } = new(Data.VideoQuality.Best);

public void CopyFrom(DownloadSettings downloadSettings)
{
DownloadType.Value = downloadSettings.DownloadType.Value;
VideoQuality.Value = downloadSettings.VideoQuality.Value;
}
}
}
17 changes: 17 additions & 0 deletions MediaDownloader/Data/MetadataSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using MediaDownloader.Utils;

namespace MediaDownloader.Data
{
public class MetadataSettings
{
public NotifyProperty<bool> SaveThumbnail { get; } = new(true);

public NotifyProperty<bool> SavePlaylistAsAlbum { get; } = new(true);

public void CopyFrom(MetadataSettings metadataSettings)
{
SaveThumbnail.Value = metadataSettings.SaveThumbnail.Value;
SavePlaylistAsAlbum.Value = metadataSettings.SavePlaylistAsAlbum.Value;
}
}
}
22 changes: 22 additions & 0 deletions MediaDownloader/Data/TitleModifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using static MediaDownloader.Data.DatabaseContext;

namespace MediaDownloader.Data
{
public class TitleModifier
{
[Key] public int Id { get; set; }

public string Target { get; set; }

public bool IsActivated { get; set; } = true;

public static ObservableCollection<TitleModifier> GetLocalSavedDownloads()
{
DBConnection.TitleModifiers.Load();
return DBConnection.TitleModifiers.Local;
}
}
}
77 changes: 58 additions & 19 deletions MediaDownloader/Download/YtDlp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace MediaDownloader.Download
{
// TODO replacements for "[Lyric Video]", "(Original Video)", etc.

public class YtDlp : Downloader
{
private const string ExePath = "dl\\yt-dlp_x86.exe";
Expand All @@ -13,7 +15,9 @@ public class YtDlp : Downloader

private const string DownloadArgs = " -r 10G --throttled-rate 100K";

private const string EmbedArgs = " --embed-thumbnail --embed-metadata --embed-chapters";
private const string EmbedArgs = " --embed-metadata --embed-chapters";

private const string EmbedThumbnailArg = " --embed-thumbnail";

private const string QualityArgs = " --audio-quality 320k";

Expand Down Expand Up @@ -43,15 +47,36 @@ public YtDlp(Data.Download download) : base(download)

protected override string MakeProcessArguments()
{
var formatArguments = Download.DownloadType.Value == DownloadType.Audio ? MakeAudioArgs() : MakeVideoArgs();
var storageArguments
= $" -o \"{Download.StoragePath}\\%(artist)s - %(title)s.%(ext)s\" -i \"{Download.Url}\"";

return new StringBuilder(DownloadArgs).Append(EmbedArgs)
.Append(QualityArgs)
.Append(formatArguments)
.Append(storageArguments)
.ToString();
StringBuilder builder = new(DownloadArgs);
foreach (var titleModifier in TitleModifier.GetLocalSavedDownloads())
{
if (titleModifier.IsActivated)
{
builder.Append(ReplaceInMetadata("title", titleModifier.Target, ""));
}
}

if (Download.DownloadSettings.IsVideoDownload.Value)
{
AppendVideoArgs(builder);
}
else
{
AppendAudioArgs(builder);
}

if (Download.MetadataSettings.SaveThumbnail.Value)
{
builder.Append(EmbedThumbnailArg);
}

return builder.Append(EmbedArgs)
.Append(QualityArgs)
.Append(ReplaceInMetadata("title", " $", ""))
.Append(
$" -o \"{Download.StoragePath}\\%(artist)s - %(title)s.%(ext)s\" -i \"{Download.Url.Value}\""
)
.ToString();
}

protected override void ReceiveOutputData(string data)
Expand Down Expand Up @@ -105,19 +130,33 @@ private static string ParseMetadata(string from, string to)
return $" --parse-metadata \"{from}:%({to})s\" --replace-in-metadata \"{to}\" \"^NA$\" \"\"";
}

private static string MakeAudioArgs()
private static string ReplaceInMetadata(string where, string what, string with)
{
return $" --replace-in-metadata \"{where}\" \"{what}\" \"{with}\"";
}

private void AppendAudioArgs(StringBuilder builder)
{
return new StringBuilder(AudioArgs).Append(ParseMetadata("playlist_title", "album"))
.Append(ParseMetadata("playlist_index", "track_number"))
.ToString();
builder.Append(AudioArgs);
if (Download.MetadataSettings.SavePlaylistAsAlbum.Value)
{
builder.Append(ParseMetadata("playlist_title", "album"))
.Append(ParseMetadata("playlist_index", "track_number"));
}
else
{
builder.Append(ReplaceInMetadata("album", ".*", ""));
}
}

private string MakeVideoArgs()
private void AppendVideoArgs(StringBuilder builder)
{
return VideoArgs +
(Download.VideoQuality.Value == VideoQuality.Best
? ""
: $" -S \"res:{Download.VideoQuality.Value.VideoHeight}\"");
builder.Append(VideoArgs);

if (Download.DownloadSettings.VideoQuality.Value != VideoQuality.Best)
{
builder.Append($" -S \"res:{Download.DownloadSettings.VideoQuality.Value.VideoHeight}\"");
}
}
}
}
Loading

0 comments on commit b6cf444

Please sign in to comment.