added crude nvidia detection

This commit is contained in:
reven
2021-12-22 09:57:47 +13:00
parent bae469619c
commit e9a65eec98
12 changed files with 127 additions and 4 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -29,7 +29,7 @@ namespace FileFlows.VideoNodes
if (crop == string.Empty)
return 2;
args.Logger.ILog("Black bars detected, crop: " + crop);
args.Logger?.ILog("Black bars detected, crop: " + crop);
args.Parameters.Add(CROP_KEY, crop);
return 1;

View File

@@ -20,6 +20,7 @@ namespace VideoNodes.Tests
vi.Read(@"D:\videos\unprocessed\Hellboy 2019 Bluray-1080p.mp4");
}
[TestMethod]
public void VideoInfoTest_SubtitleRemover()
{
@@ -43,6 +44,37 @@ namespace VideoNodes.Tests
Assert.AreEqual(1, output);
}
[TestMethod]
public void VideoInfoTest_DetectBlackBars()
{
const string file = @"D:\videos\unprocessed\Bourne.mkv";
var vi = new VideoInfoHelper(@"C:\utils\ffmpeg\ffmpeg.exe", new TestLogger());
vi.Read(@"D:\videos\unprocessed\Bourne.mkv");
var args = new FileFlows.Plugin.NodeParameters(file, new TestLogger());
args.GetToolPath = (string tool) => @"C:\utils\ffmpeg\ffmpeg.exe";
args.TempPath = @"D:\videos\temp";
int result = new DetectBlackBars().Execute(args);
Assert.IsTrue(result > 0);
}
[TestMethod]
public void VideoInfoTest_NvidiaCard()
{
const string file = @"D:\videos\unprocessed\Bourne.mkv";
const string ffmpeg = @"C:\utils\ffmpeg\ffmpeg.exe";
var args = new FileFlows.Plugin.NodeParameters(file, new TestLogger());
//args.Process = new FileFlows.Plugin.ProcessHelper(args.Logger);
var node = new VideoEncode();
node.SetArgs(args);
bool result = node.HasNvidiaCard(ffmpeg);
Assert.IsTrue(result);
}
}
}

Binary file not shown.

View File

@@ -1,6 +1,7 @@
namespace FileFlows.VideoNodes
{
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
@@ -116,7 +117,10 @@ namespace FileFlows.VideoNodes
List<string> ffArgs = new List<string>();
if (videoIsRightCodec == null || crop != string.Empty)
{
string codecParameters = CheckVideoCodec(ffmpegExe, VideoCodecParameters);
ffArgs.Add($"-map 0:v:0 -c:v {VideoCodecParameters} {crop}");
}
else
ffArgs.Add($"-map 0:v:0 -c:v copy");
@@ -156,6 +160,93 @@ namespace FileFlows.VideoNodes
}
}
#if(DEBUG)
/// <summary>
/// Used for unit tests
/// </summary>
/// <param name="args">the args</param>
public void SetArgs(NodeParameters args)
{
this.args = args;
}
#endif
public string CheckVideoCodec(string ffmpeg, string vidparams)
{
if (string.IsNullOrEmpty(vidparams))
return string.Empty;
if (vidparams.ToLower().Contains("hevc_nvenc"))
{
// nvidia h265 encoding, check can
bool nvidia = HasNvidiaCard(ffmpeg);
if(nvidia == false)
{
// change to cpu encoding
args.Logger?.ILog("No NVIDIA card detected, falling back to CPU encoding H265 (libx265)");
return "libx265";
}
return vidparams;
}
else if (vidparams.ToLower().Contains("h264_nvenc"))
{
// nvidia h265 encoding, check can
bool nvidia = HasNvidiaCard(ffmpeg);
if (nvidia == false)
{
// change to cpu encoding
args.Logger?.ILog("No NVIDIA card detected, falling back to CPU encoding H264 (libx264)");
return "libx264";
}
return vidparams;
}
return vidparams;
}
public bool HasNvidiaCard(string ffmpeg)
{
try
{
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
var cmd = args.Process.ExecuteShellCommand(new ExecuteArgs
{
Command = "wmic",
Arguments = "path win32_VideoController get name"
}).Result;
if(cmd.ExitCode == 0)
{
// it worked
if (cmd.Output?.ToLower()?.Contains("nvidia") == false)
return false;
}
}
else
{
// linux, crude method, look for nvidia in the /dev dir
var dir = new DirectoryInfo("/dev");
if (dir.Exists == false)
return false;
bool dev = dir.GetDirectories().Any(x => x.Name.ToLower().Contains("nvidia"));
if (dev == false)
return false;
}
// check cuda in ffmpeg itself
var result = args.Process.ExecuteShellCommand(new ExecuteArgs
{
Command = ffmpeg,
Arguments = "-hide_banner -init_hw_device list"
}).Result;
return result.Output?.Contains("cuda") == true;
}
catch (Exception ex)
{
args.Logger?.ELog("Failed to detect NVIDIA card: " + ex.Message + Environment.NewLine + ex.StackTrace);
return false;
}
}
protected bool IsSameVideoCodec(string current, string wanted)
{
wanted = ReplaceCommon(wanted);

View File

@@ -1,17 +1,17 @@
[
{
"Name": "BasicNodes",
"Version": "0.0.1.33",
"Version": "0.0.1.34",
"Package": "https://github.com/revenz/FileFlowsPlugins/blob/master/Builds/BasicNodes.zip?raw=true"
},
{
"Name": "MetaNodes",
"Version": "0.0.1.33",
"Version": "0.0.1.34",
"Package": "https://github.com/revenz/FileFlowsPlugins/blob/master/Builds/MetaNodes.zip?raw=true"
},
{
"Name": "VideoNodes",
"Version": "0.0.1.33",
"Version": "0.0.1.34",
"Package": "https://github.com/revenz/FileFlowsPlugins/blob/master/Builds/VideoNodes.zip?raw=true"
}
]