killapp/Assets/Scripts/Video/VideoCacheManager.cs
“虞渠成” 4726107e19 feat(video,android): 视频下载转码播放 + 列表下载/播放按钮
- 集成 ffmpeg-kit-full-gpl 6.0.3(arm64),用 libx264 重编码修复 .h264 缺 SPS/PPS 导致的播放卡死

- 新增 VideoDownloadPlayService/VideoCacheManager,.mp4 直下直播、.h264 下载后转 mp4 缓存,转码完成后删除临时 .h264

- FFmpegKitBridge 走主线程 JNI 调用规避 IL2CPP 后台线程 FindClass 不稳定

- 视频列表按缓存状态显示 下载/播放 按钮,下载中亮色条显示百分比;播放面板原下载按钮改为保存到相册
2026-09-08 13:31:31 +08:00

65 lines
2.0 KiB
C#

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
namespace Kill.Video
{
/// <summary>
/// 视频缓存管理:按 url 哈希缓存到 persistentDataPath/video_cache
/// key=mp4(转码后),key=tmp.h264(下载中临时)
/// </summary>
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;
}
}
/// <summary>已转码好的 mp4 缓存路径</summary>
public static string GetCachedMp4Path(string url)
{
return Path.Combine(CacheDir, Hash(url) + ".mp4");
}
/// <summary>下载中的 .h264 临时路径</summary>
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();
}
}
}