Skip to content

Commit

Permalink
Add MKVToolNix to Updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkl58 committed Jan 28, 2024
1 parent 0537963 commit 4176fe3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 44 deletions.
1 change: 1 addition & 0 deletions NotEnoughAV1Encodes/NotEnoughAV1Encodes.csproj
Expand Up @@ -33,6 +33,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.57" />
<PackageReference Include="MahApps.Metro" Version="3.0.0-alpha0476" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
3 changes: 2 additions & 1 deletion NotEnoughAV1Encodes/Views/Updater.xaml
Expand Up @@ -45,7 +45,7 @@
<Label x:Name="LabelUpdateSVTAV1Version" Content="{Binding SVTAV1UpdateVersion}" HorizontalAlignment="Left" Margin="439,195,0,0" VerticalAlignment="Top" Width="88" Height="34" VerticalContentAlignment="Center" />

<!-- MKVToolnix -->
<Label Content="MKVToolNix" HorizontalAlignment="Left" Margin="12,273,0,0" VerticalAlignment="Top" Width="113" Height="34" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
<Button x:Name="ButtonUpdateMKVToolNix" Content="Update MKVToolNix" HorizontalAlignment="Left" Margin="10,273,0,0" VerticalAlignment="Top" Width="115" Background="{x:Null}" Height="34" Click="ButtonUpdateMKVToolnix_Click" mah:ControlsHelper.ContentCharacterCasing="Normal" />
<Label Content="Current Version:" HorizontalAlignment="Left" Margin="130,273,0,0" VerticalAlignment="Top" Width="111" Height="34" VerticalContentAlignment="Center" />
<Label Content="Update Version:" HorizontalAlignment="Left" Margin="334,273,0,0" VerticalAlignment="Top" Width="112" Height="34" VerticalContentAlignment="Center" />
<Label x:Name="LabelCurrentMKVToolnixVersion" Content="{Binding MKVToolnixCurrentVersion}" HorizontalAlignment="Left" Margin="241,273,0,0" VerticalAlignment="Top" Width="88" Height="34" VerticalContentAlignment="Center" />
Expand All @@ -62,5 +62,6 @@
<Label x:Name="LabelBuildsProvidedBy" Content="FFmpeg provided by gyan.dev under GPLv3 &#xD;&#xA;Other Builds provided by jeremylee.sh under GPLv3" HorizontalAlignment="Left" Margin="10,-5,0,0" VerticalAlignment="Top" Width="403" Height="39" VerticalContentAlignment="Center" FontStyle="Italic" />
<ProgressBar x:Name="ProgressBar" Margin="0,326,0,0" Height="30" VerticalAlignment="Top" />
<Label x:Name="LabelProgressBar" Content="" Margin="0,326,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Height="30" VerticalAlignment="Top"/>

</Grid>
</mah:MetroWindow>
128 changes: 85 additions & 43 deletions NotEnoughAV1Encodes/Views/Updater.xaml.cs
Expand Up @@ -18,13 +18,16 @@
using Newtonsoft.Json.Linq;
using NotEnoughAV1Encodes.resources.lang;
using Octokit;
using HtmlAgilityPack;

namespace NotEnoughAV1Encodes.Views
{
public partial class Updater : MetroWindow, INotifyPropertyChanged
{
private static readonly string FFMPEG_LAST_BUILD_VERSION_URL = "https://www.gyan.dev/ffmpeg/builds/last-build-update";
private static readonly string FFMPEG_LAST_BUILD_DOWNLOAD_URL = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-git-full.7z";
private static readonly string MKVTOOLNIX_DOWNLOADS_WEBPAGE = "https://mkvtoolnix.download/downloads.html";
private static readonly string MKVTOOLNIX_ROOT_URL = "https://mkvtoolnix.download";
private static readonly string ASSEMBLY_VERSION = Assembly.GetExecutingAssembly().GetName().Version.ToString();
private static readonly string CURRENT_DIR = Directory.GetCurrentDirectory();

Expand All @@ -44,6 +47,7 @@ public partial class Updater : MetroWindow, INotifyPropertyChanged
private string mkvtoolnixCurrentVersion;

private string QSVReleaseAPIGithub;
private string MKVToolnixDownloadURL;

private string UpdateVersion = "0";
private readonly string CurrentVersion = ASSEMBLY_VERSION.Remove(ASSEMBLY_VERSION.Length - 2);
Expand Down Expand Up @@ -138,7 +142,7 @@ private async void ParseEverything()
ParseQSVEncCGithub();
await ParseFFMPEGVersion();
await ParseJeremyleeJSONAsync();
await GetOnlineMKVToolnixVersion();
ParseMKVToolnixWebsite();
GetLocalMKVToolnixVersion();
GetLocalQSVEncCVersion();
CompareLocalVersion();
Expand Down Expand Up @@ -248,6 +252,39 @@ private async Task ParseFFMPEGVersion()
catch { }
}

private void ParseMKVToolnixWebsite()
{
try
{
HttpClient client = new();
string html = client.GetStringAsync(MKVTOOLNIX_DOWNLOADS_WEBPAGE).Result;

HtmlDocument doc = new();
doc.LoadHtml(html);

// XPath to select the <tr> with "Portable (64-bit)"
string xpath = "//tr[td='Portable (64-bit)']";

HtmlNode portableRow = doc.DocumentNode.SelectSingleNode(xpath);

if (portableRow != null)
{
// Get the href value from the <a> tag within the selected <tr>
HtmlNode hrefNode = portableRow.SelectSingleNode("td/a[@href]");

if (hrefNode != null)
{
string hrefValue = hrefNode.GetAttributeValue("href", "");
MKVToolnixDownloadURL = MKVTOOLNIX_ROOT_URL + "/" + hrefValue;

var split = hrefValue.Split('/');
MKVToolnixUpdateVersion = split[2];
}
}
}
catch { }
}

private void GetLocalMKVToolnixVersion()
{
try
Expand All @@ -274,38 +311,6 @@ private void GetLocalQSVEncCVersion()
catch { }
}

private async Task GetOnlineMKVToolnixVersion()
{
try
{
HttpClient client = new();
// Download Release Notes
HttpResponseMessage response = await client.GetAsync("https://mkvtoolnix.download/doc/NEWS.md");

if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
// Split by Line
string[] lines = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.None);
string version = "";
// Iterate over Lines
foreach (string line in lines)
{
if (line.Contains("# Version"))
{
version = line;
break;
}
}
//Remove First 10 Characters
version = version[10..];
version = version.Split()[0];
MKVToolnixUpdateVersion = version;
}
}
catch { }
}

private void CompareLocalVersion()
{
// ffmpeg
Expand Down Expand Up @@ -679,16 +684,6 @@ private async void ButtonUpdateSVTAV1_Click(object sender, RoutedEventArgs e)
LabelProgressBar.Dispatcher.Invoke(() => LabelProgressBar.Content = "Finished updating SVT-AV1");
}

private void ButtonUpdateMKVToolnix_Click(object sender, RoutedEventArgs e)
{
ProcessStartInfo psi = new()
{
FileName = "https://www.fosshub.com/MKVToolNix.html",
UseShellExecute = true
};
Process.Start(psi);
}

private async void ButtonUpdateQSVEnc_Click(object sender, RoutedEventArgs e)
{
ToggleAllButtons(false);
Expand Down Expand Up @@ -751,6 +746,53 @@ private async void ButtonUpdateQSVEnc_Click(object sender, RoutedEventArgs e)
catch { }

ToggleAllButtons(true);

LabelProgressBar.Dispatcher.Invoke(() => LabelProgressBar.Content = "Finished updating QSVEncC");
}

private async void ButtonUpdateMKVToolnix_Click(object sender, RoutedEventArgs e)
{
ToggleAllButtons(false);

if (string.IsNullOrEmpty(MKVToolnixDownloadURL))
{
LabelProgressBar.Content = "Error: Download URL was Null or Empty";
return;
}

try
{
// Downloads qsvenc
await Task.Run(() => DownloadBin(MKVToolnixDownloadURL, Path.Combine(CURRENT_DIR, "Apps", "mkvtoolnix.7z")));

// Should never happen
if (!File.Exists(Path.Combine(CURRENT_DIR, "Apps", "mkvtoolnix.7z")))
{
LabelProgressBar.Content = "Downloaded file not found!";
return;
}

if (Directory.Exists(Path.Combine(CURRENT_DIR, "Apps", "mkvtoolnix")))
{
Directory.Delete(Path.Combine(CURRENT_DIR, "Apps", "mkvtoolnix"), true);
}

Directory.CreateDirectory(Path.Combine(CURRENT_DIR, "Apps", "mkvtoolnix"));

// Extract downloaded 7z file
ExtractFile(Path.Combine(CURRENT_DIR, "Apps", "mkvtoolnix.7z"), Path.Combine(CURRENT_DIR, "Apps"));

// Cleanup
File.Delete(Path.Combine(CURRENT_DIR, "Apps", "mkvtoolnix.7z"));

GetLocalMKVToolnixVersion();
CompareLocalVersion();
}
catch { }

ToggleAllButtons(true);

LabelProgressBar.Dispatcher.Invoke(() => LabelProgressBar.Content = "Finished updating MKVToolNix");
}

private static string FindQSVDownloadUrl(JArray releasesArray)
Expand Down

0 comments on commit 4176fe3

Please sign in to comment.