Files
flowframes/Code/Media/GetFrameCountCached.cs

63 lines
1.8 KiB
C#
Raw Normal View History

2021-08-23 16:50:18 +02:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using Flowframes.Data;
using Flowframes.IO;
namespace Flowframes.Media
{
class GetFrameCountCached
{
2021-09-14 18:18:41 +02:00
public static Dictionary<QueryInfo, int> cache = new Dictionary<QueryInfo, int>();
2021-08-23 16:50:18 +02:00
public static async Task<int> GetFrameCountAsync(string path)
{
Logger.Log($"Getting frame count ({path})", true);
long filesize = IoUtils.GetFilesize(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))
frameCount = IoUtils.GetAmountOfFiles(path, false);
else
frameCount = await FfmpegCommands.GetFrameCountAsync(path);
Logger.Log($"Adding hash with value {frameCount} to cache.", true);
cache.Add(hash, frameCount);
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;
}
}
}