mirror of
https://github.com/revenz/FileFlowsPlugins.git
synced 2026-01-09 00:49:32 -06:00
experimenting with detecting credits
This commit is contained in:
33
VideoNodes/Tests/EndCreditsTests.cs
Normal file
33
VideoNodes/Tests/EndCreditsTests.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
#if(DEBUG)
|
||||
|
||||
namespace VideoNodes.Tests;
|
||||
|
||||
using FileFlows.VideoNodes;
|
||||
using FileFlows.VideoNodes.FfmpegBuilderNodes;
|
||||
using FileFlows.VideoNodes.VideoNodes;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
[TestClass]
|
||||
public class EndCreditsTests : TestBase
|
||||
{
|
||||
[TestMethod]
|
||||
public void EndCredits_Base()
|
||||
{
|
||||
var logger = new TestLogger();
|
||||
string file = @"D:\videos\testfiles\pgs.mkv";
|
||||
var vi = new VideoInfoHelper(FfmpegPath, logger);
|
||||
var vii = vi.Read(file);
|
||||
|
||||
var args = new NodeParameters(file, logger, false, string.Empty);
|
||||
args.GetToolPathActual = (string tool) => FfmpegPath;
|
||||
args.TempPath = TempPath;
|
||||
|
||||
var node = new DetectEndCredits();
|
||||
node.PreExecute(args);
|
||||
var result = node.Execute(args);
|
||||
var log = logger.ToString();
|
||||
Assert.AreEqual(1, result);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -24,8 +24,8 @@ public abstract class TestBase
|
||||
{
|
||||
LoadSettings("../../../test.settings.json");
|
||||
}
|
||||
this.TestPath = this.TestPath?.EmptyAsNull() ?? @"C:\videos\testfiles";
|
||||
this.TempPath = this.TempPath?.EmptyAsNull() ?? @"C:\videos\temp";
|
||||
this.TestPath = this.TestPath?.EmptyAsNull() ?? @"d:\videos\testfiles";
|
||||
this.TempPath = this.TempPath?.EmptyAsNull() ?? @"d:\videos\temp";
|
||||
this.FfmpegPath = this.FfmpegPath?.EmptyAsNull() ?? @"C:\utils\ffmpeg\ffmpeg.exe";
|
||||
if (Directory.Exists(this.TempPath) == false)
|
||||
Directory.CreateDirectory(this.TempPath);
|
||||
|
||||
Binary file not shown.
72
VideoNodes/VideoNodes/DetectEndCredits.cs
Normal file
72
VideoNodes/VideoNodes/DetectEndCredits.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Tesseract;
|
||||
|
||||
namespace FileFlows.VideoNodes.VideoNodes;
|
||||
|
||||
internal class DetectEndCredits: VideoNode
|
||||
{
|
||||
public override int Execute(NodeParameters args)
|
||||
{
|
||||
var imageDir = ExportImages(args, args.WorkingFile);
|
||||
var time = ScanImages(args, imageDir);
|
||||
return 1;
|
||||
}
|
||||
|
||||
private string ExportImages(NodeParameters args, string file)
|
||||
{
|
||||
string dir = Path.Combine(args.TempPath, Guid.NewGuid().ToString());
|
||||
Directory.CreateDirectory(dir);
|
||||
var result = args.Execute(new()
|
||||
{
|
||||
Command = FFMPEG,
|
||||
ArgumentList = new[]
|
||||
{
|
||||
"-sseof",
|
||||
"-600",
|
||||
"-i",
|
||||
file,
|
||||
"-vf",
|
||||
"fps=1",
|
||||
Path.Combine(dir, "out%04d.png")
|
||||
}
|
||||
});
|
||||
return dir;
|
||||
}
|
||||
|
||||
private object ScanImages(NodeParameters args, string imageDir)
|
||||
{
|
||||
var images = Directory.GetFiles(imageDir, "*.png");
|
||||
DateTime dt = DateTime.Now;
|
||||
|
||||
using var engine = new TesseractEngine(@"D:\videos\temp\tesseract", "eng", EngineMode.Default);
|
||||
Dictionary<string, int> imagesWithText = new();
|
||||
bool last2 = false, last1 = false;
|
||||
for(int i=0;i<images.Length;i += 5) // every 5th image just to speed things up
|
||||
{
|
||||
var imageFile = images[i];
|
||||
using var img = Pix.LoadFromFile(imageFile);
|
||||
using var page = engine.Process(img);
|
||||
var text = page.GetText();
|
||||
bool hasText = false;
|
||||
if (string.IsNullOrWhiteSpace(text) == false)
|
||||
{
|
||||
text = Regex.Replace(text, "[^\\w]", string.Empty);
|
||||
if (text.Length > 10)
|
||||
{
|
||||
hasText = true;
|
||||
imagesWithText.Add(imageFile, 1 + (last2 ? 1 : 0) + (last1 ? 1: 0));
|
||||
args.Logger.DLog(imageFile + " , text = " + text);
|
||||
}
|
||||
}
|
||||
last2 = last1;
|
||||
last1 = hasText;
|
||||
}
|
||||
args.Logger.ILog("Time taken to scan images: "+ (DateTime.Now.Subtract(dt)));
|
||||
return imagesWithText;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user