Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
Plugin path parsing fixed,
Plugins arch detection
Basic HDR support
  • Loading branch information
Anime4000 committed May 27, 2021
1 parent a2fa43b commit 2ff5b79
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 39 deletions.
6 changes: 3 additions & 3 deletions IFME/MediaEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal static void Audio(MediaQueue queue, string tempDir)
{
var ac = codec.Audio;
var md = item.Encoder.Mode;
var en = Path.Combine(codec.FilePath, ac.Encoder);
var en = ac.Encoder;

var trim = (queue.Trim.Enable ? $"-ss {queue.Trim.Start} -t {queue.Trim.Duration}" : string.Empty);

Expand All @@ -105,7 +105,7 @@ internal static void Audio(MediaQueue queue, string tempDir)

if (ac.Args.Pipe)
{
ProcessManager.Start(tempDir, $"\"{FFmpeg}\" -hide_banner -v error -i \"{item.File}\" {trim} -map 0:{item.Id} -acodec pcm_s16le {hz} {ch} {af} -f wav {item.Command} - | \"{Path.Combine(codec.FilePath, ac.Encoder)}\" {ac.Args.Input} {ac.Args.Command} {qu} {item.Encoder.Command} {ac.Args.Output} \"{outfmtfile}\"");
ProcessManager.Start(tempDir, $"\"{FFmpeg}\" -hide_banner -v error -i \"{item.File}\" {trim} -map 0:{item.Id} -acodec pcm_s16le {hz} {ch} {af} -f wav {item.Command} - | \"{en}\" {ac.Args.Input} {ac.Args.Command} {qu} {item.Encoder.Command} {ac.Args.Output} \"{outfmtfile}\"");
}
else
{
Expand All @@ -125,7 +125,7 @@ internal static void Video(MediaQueue queue, string tempDir)
{
var vc = codec.Video;

var en = Path.Combine(codec.FilePath, vc.Encoder.Find(b => b.BitDepth == item.Quality.BitDepth).Binary);
var en = vc.Encoder.Find(b => b.BitDepth == item.Quality.BitDepth).Binary;
var outrawfile = $"raw-v{i:D4}_{item.Lang}.{vc.Extension}";
var outfmtfile = $"video{i:D4}_{item.Lang}.{codec.Format[0]}";

Expand Down
1 change: 0 additions & 1 deletion IFME/Plugins/Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class Items
public class PluginsCommon
{
public Guid GUID { get; set; }
public string FilePath { get; set; }
public string Name { get; set; }
public string Version { get; set; }
public bool X64 { get; set; }
Expand Down
89 changes: 58 additions & 31 deletions IFME/Plugins/PluginsLoad.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;

using Newtonsoft.Json;

namespace IFME
Expand All @@ -13,33 +12,43 @@ internal class PluginsLoad
{
internal PluginsLoad()
{
Audio();
Video();
}

private void Audio()
{
var path = Path.Combine("Plugins");
var folder = Path.GetFullPath("Plugins");

if (!Directory.Exists(path))
Directory.CreateDirectory(path);
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);

var folder = Path.Combine(Directory.GetCurrentDirectory(), path);
Audio(folder);
Video(folder);
}

private void Audio(string folder)
{
foreach (var item in Directory.EnumerateFiles(folder, "_plugin.a*.json", SearchOption.AllDirectories).OrderBy(file => file))
{
try
{
var json = File.ReadAllText(item);
var plugin = JsonConvert.DeserializeObject<PluginsAudio>(json);

plugin.FilePath = Path.GetDirectoryName(item);
frmSplashScreen.SetStatus($"Initializing Audio:\n{plugin.Name}");

// Skip wrong cpu arch
if (OSManager.OS.Is64bit != plugin.X64)
{
frmSplashScreen.SetStatusAppend(" (incompatible architecture, skipping...)");
Thread.Sleep(250);
continue;
}

frmSplashScreen.SetStatus($"{plugin.Name}");
// Parse into fully qualified path
if (Path.IsPathRooted(plugin.Audio.Encoder))
plugin.Audio.Encoder = Path.GetFullPath(plugin.Audio.Encoder);
else
plugin.Audio.Encoder = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(item), plugin.Audio.Encoder));

if (!TestAudio(plugin))
{
frmSplashScreen.SetStatusAppend(" (incompatible host, skipping...)");
frmSplashScreen.SetStatusAppend(" (incompatible hardware, skipping...)");
Thread.Sleep(2000);
continue;
}
Expand All @@ -49,34 +58,51 @@ private void Audio()
}
catch (Exception ex)
{
frmMain.PrintLog($"[WARN] {ex.Message}");
frmSplashScreen.SetStatusAppend($" [{ex.Message}]");
Thread.Sleep(5000);
}
}
; }

private void Video()
private void Video(string folder)
{
var path = Path.Combine("Plugins");

if (!Directory.Exists(path))
Directory.CreateDirectory(path);

var folder = Path.Combine(Directory.GetCurrentDirectory(), path);

foreach (var item in Directory.EnumerateFiles(folder, "_plugin.v*.json", SearchOption.AllDirectories).OrderBy(file => file))
{
try
{
var json = File.ReadAllText(item);
var plugin = JsonConvert.DeserializeObject<PluginsVideo>(json);

plugin.FilePath = Path.GetDirectoryName(item);
frmSplashScreen.SetStatus($"Initializing Video:\n{plugin.Name}");

// Skip wrong cpu arch
if (OSManager.OS.Is64bit != plugin.X64)
{
frmSplashScreen.SetStatusAppend(" (incompatible architecture, skipping...)");
Thread.Sleep(250);
continue;
}

frmSplashScreen.SetStatus($"{plugin.Name}");
// Parse into fully qualified path
foreach (var p in plugin.Video.Encoder)
{
if (Path.IsPathRooted(p.Binary))
p.Binary = Path.GetFullPath(p.Binary);
else
p.Binary = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(item), p.Binary));
}

// Check if plugins already in the list
if (Plugins.Items.Video.ContainsKey(plugin.GUID))
{
frmSplashScreen.SetStatusAppend(" (best encoder already loaded, skipping...)");
Thread.Sleep(2000);
continue;
}

if (!TestVideo(plugin))
{
frmSplashScreen.SetStatusAppend(" (incompatible host, skipping...)");
frmSplashScreen.SetStatusAppend(" (incompatible hardware, skipping...)");
Thread.Sleep(2000);
continue;
}
Expand All @@ -85,7 +111,8 @@ private void Video()
}
catch (Exception ex)
{
frmMain.PrintLog($"[WARN] {ex.Message}");
frmSplashScreen.SetStatusAppend($" [{ex.Message}]");
Thread.Sleep(5000);
}
}
}
Expand All @@ -94,7 +121,7 @@ private bool TestAudio(PluginsAudio codec)
{
var ac = codec.Audio;
var ff = MediaEncoding.FFmpeg;
var en = Path.Combine(codec.FilePath, ac.Encoder);
var en = ac.Encoder;
var sampleFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Samples", "wonderland_online.m4a");
var outTempFile = Path.Combine(Path.GetTempPath(), $"test_{DateTime.Now:yyyy-MM-dd_HH-mm-ss_ffff}.{ac.Extension}");
var outTempFolder = Path.Combine(Path.GetTempPath());
Expand All @@ -118,7 +145,7 @@ private bool TestVideo(PluginsVideo codec)
{
var vc = codec.Video;
var ff = MediaEncoding.FFmpeg;
var en = Path.Combine(codec.FilePath, vc.Encoder[0].Binary);
var en = vc.Encoder[0].Binary;
var sampleFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Samples", "wonderland_online.mp4");
var outTempFile = Path.Combine(Path.GetTempPath(), $"test_{DateTime.Now:yyyy-MM-dd_HH-mm-ss_ffff}.{vc.Extension}");
var outTempFolder = Path.Combine(Path.GetTempPath());
Expand Down
14 changes: 13 additions & 1 deletion IFME/ProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,21 @@ private int Run(string Command, string WorkingDirectory)

private void Proc_DataReceived(object sender, DataReceivedEventArgs e)
{
if (frmMain.frmMainStatus == null)
return;

if (!string.IsNullOrEmpty(e.Data))
{
var regexPattern = @"(\d\%\] )|(time=\d)|(\| \(\d+\/\d+\))|(\x08)";
var p = @"(frame[ ]{1,}\d+)|(\d+.\d+[ ]{1,}kbps)|(\d+.\d+[ ]{1,}fps)";
var x = Regex.Matches(e.Data, p, RegexOptions.IgnoreCase);
if (x.Count >= 3)
{
frmMain.PrintProgress($"{x[0]}, Bitrate: {x[1]}, Speed: {x[2]}");
return;
}


var regexPattern = @"(\d\%\] )|(time=\d)|(\| \(\d+\/\d+\))|(\x08)|(frame[ ]{1,}\d+)";
Match m = Regex.Match(e.Data, regexPattern, RegexOptions.IgnoreCase);
if (m.Success)
frmMain.PrintProgress(e.Data);
Expand Down
2 changes: 1 addition & 1 deletion IFME/frmMain.Status.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class VSDesignerBugFixB { }

public partial class frmMain
{
private static frmMain frmMainStatus = null;
internal static frmMain frmMainStatus = null;
private delegate void rtfConsoleAppendText(string value);
private delegate void lstFileProgressText(string value);
private delegate void lstFileStatusText(string value);
Expand Down
4 changes: 2 additions & 2 deletions IFME/frmSplashScreen.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ff5b79

Please sign in to comment.