experimenting with detecting credits

This commit is contained in:
John Andrews
2022-10-15 21:45:57 +13:00
parent 526380f3dd
commit df79dba18f
4 changed files with 107 additions and 2 deletions

View 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

View File

@@ -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.

View 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;
}
}