2021-08-23 16:50:18 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Drawing;
|
2023-02-15 12:27:36 +01:00
|
|
|
|
using System.Linq;
|
2021-08-23 16:50:18 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Flowframes.Data;
|
|
|
|
|
|
using Flowframes.IO;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Flowframes.Media
|
|
|
|
|
|
{
|
|
|
|
|
|
class GetFrameCountCached
|
|
|
|
|
|
{
|
2022-04-05 18:07:05 +02:00
|
|
|
|
private static Dictionary<QueryInfo, int> cache = new Dictionary<QueryInfo, int>();
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-07-27 14:10:29 +02:00
|
|
|
|
public static async Task<int> GetFrameCountAsync(MediaFile mf, int retryCount = 3)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await GetFrameCountAsync(mf.SourcePath, retryCount);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-05 18:07:05 +02:00
|
|
|
|
public static async Task<int> GetFrameCountAsync(string path, int retryCount = 3)
|
2021-08-23 16:50:18 +02:00
|
|
|
|
{
|
|
|
|
|
|
Logger.Log($"Getting frame count ({path})", true);
|
|
|
|
|
|
|
2023-02-15 12:27:36 +01:00
|
|
|
|
long filesize = IoUtils.GetPathSize(path);
|
2021-09-14 18:18:41 +02:00
|
|
|
|
QueryInfo hash = new QueryInfo(path, filesize);
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
|
|
|
|
|
if (filesize > 0 && CacheContains(hash))
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Log($"Cache contains this hash, using cached value.", true);
|
|
|
|
|
|
return GetFromCache(hash);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Log($"Hash not cached, reading frame count.", true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int frameCount;
|
|
|
|
|
|
|
|
|
|
|
|
if (IoUtils.IsPathDirectory(path))
|
2023-02-15 12:27:36 +01:00
|
|
|
|
{
|
|
|
|
|
|
frameCount = IoUtils.GetAmountOfFiles(path, false); // Count frames based on image file amount
|
|
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
else
|
2023-02-15 12:27:36 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (path.IsConcatFile())
|
|
|
|
|
|
{
|
|
|
|
|
|
var lines = IoUtils.ReadFileLines(path);
|
|
|
|
|
|
var filtered = lines.Where(l => l.StartsWith("file '"));
|
|
|
|
|
|
frameCount = filtered.Count(); // Count frames from concat file
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
frameCount = await FfmpegCommands.GetFrameCountAsync(path); // Count frames from video stream
|
|
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2023-02-15 12:27:36 +01:00
|
|
|
|
if (frameCount > 0)
|
2022-04-05 18:07:05 +02:00
|
|
|
|
{
|
|
|
|
|
|
Logger.Log($"Adding hash with value {frameCount} to cache.", true);
|
|
|
|
|
|
cache.Add(hash, frameCount);
|
|
|
|
|
|
}
|
2022-04-05 23:13:48 +02:00
|
|
|
|
else
|
2022-04-05 18:07:05 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (retryCount > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Log($"Got {frameCount} frames, retrying ({retryCount} left)", true);
|
2022-04-05 18:08:02 +02:00
|
|
|
|
Clear();
|
2022-04-05 18:07:05 +02:00
|
|
|
|
frameCount = await GetFrameCountAsync(path, retryCount - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Log($"Failed to get frames and out of retries ({frameCount} frames for {path})", true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
|
|
|
|
|
return frameCount;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-14 18:18:41 +02:00
|
|
|
|
private static bool CacheContains(QueryInfo hash)
|
2021-08-23 16:50:18 +02:00
|
|
|
|
{
|
2021-09-14 18:18:41 +02:00
|
|
|
|
foreach (KeyValuePair<QueryInfo, int> entry in cache)
|
2021-08-23 16:50:18 +02:00
|
|
|
|
if (entry.Key.path == hash.path && entry.Key.filesize == hash.filesize)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-14 18:18:41 +02:00
|
|
|
|
private static int GetFromCache(QueryInfo hash)
|
2021-08-23 16:50:18 +02:00
|
|
|
|
{
|
2021-09-14 18:18:41 +02:00
|
|
|
|
foreach (KeyValuePair<QueryInfo, int> entry in cache)
|
2021-08-23 16:50:18 +02:00
|
|
|
|
if (entry.Key.path == hash.path && entry.Key.filesize == hash.filesize)
|
|
|
|
|
|
return entry.Value;
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2022-04-05 18:07:05 +02:00
|
|
|
|
|
2023-02-15 12:27:36 +01:00
|
|
|
|
public static void Clear()
|
2022-04-05 18:07:05 +02:00
|
|
|
|
{
|
|
|
|
|
|
cache.Clear();
|
|
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|