using System; using System.IO; using System.Security.Cryptography; using System.Text; using UnityEngine; namespace Kill.Video { /// /// 视频缓存管理:按 url 哈希缓存到 persistentDataPath/video_cache /// key=mp4(转码后),key=tmp.h264(下载中临时) /// public static class VideoCacheManager { private const string CacheSubDir = "video_cache"; private static string CacheDir { get { string dir = Path.Combine(Application.persistentDataPath, CacheSubDir); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); return dir; } } /// 已转码好的 mp4 缓存路径 public static string GetCachedMp4Path(string url) { return Path.Combine(CacheDir, Hash(url) + ".mp4"); } /// 下载中的 .h264 临时路径 public static string GetTempH264Path(string url) { return Path.Combine(CacheDir, Hash(url) + ".h264"); } public static bool IsCached(string url) { var p = GetCachedMp4Path(url); return File.Exists(p) && new FileInfo(p).Length > 0; } public static void Clear() { string dir = Path.Combine(Application.persistentDataPath, CacheSubDir); if (Directory.Exists(dir)) { try { Directory.Delete(dir, true); } catch (Exception e) { Debug.LogWarning("[VideoCacheManager] clear fail: " + e.Message); } } } private static string Hash(string s) { using var sha = SHA1.Create(); var bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(s)); var sb = new StringBuilder(bytes.Length * 2); foreach (var b in bytes) sb.Append(b.ToString("x2")); return sb.ToString(); } } }