一、 使用步骤:
第一步、腾讯云开启万象
第二步、安装Tencent.QCloud.Cos.Sdk 包
第三步、修改 腾讯云配置 + 图片存储目录配置
第四步、执行获取图片并保存
二、封装代码
using System.Text;
using System.Threading.Tasks;
using COSXML.Model.CI;
using COSXML.Auth;
using System;
using COSXML;
using UploadHandle;
using QLCommon;
/// <summary>
/// 腾讯云视频获取第一帧
/// 使用步骤:
/// 第一步、腾讯云开启万象
/// 第二步、安装Tencent.QCloud.Cos.Sdk 包
/// 第三步、修改 腾讯云配置 + 图片存储目录配置
/// 第四步、执行获取图片并保存
/// </summary>
public class SnapshotHelper
{
public string Region { get; set; } = "ap-beijing";
public string SecretID { get; set; } = "自己的";
public string SecretKey { get; set; } = "自己的";
public string Bucket { get; set; } = "beijing-1330411582";
public string BasePath { get; set; } = "";//网站根目录wwww+/file/video/
private CosXml cosXml;
/// <summary>
/// 初始化
/// </summary>
public SnapshotHelper()
{
CosXmlConfig config = new CosXmlConfig.Builder()
.SetRegion(this.Region)
.Build();
long durationSecond = 60; //每次请求签名有效时长,单位为秒
QCloudCredentialProvider qCloudCredentialProvider =
new DefaultQCloudCredentialProvider(this.SecretID,
this.SecretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}
/// 视频截帧
public string GetSnapshot(string fileKey)
{
string bucket = this.Bucket;
string key = fileKey; // 媒体文件的对象键,需要替换成桶内存在的媒体文件的对象键
float time = 0.01F; // 截取的具体时间,用浮点数表示
string destPath = BasePath + fileKey.ToLower().Replace(".mp4", ".jpg"); // 截图文件保存路径, 需要替换成本地具体路径, 例如"/usr/local/"
GetSnapshotRequest request = new GetSnapshotRequest(bucket, key, time, destPath);
// 执行请求
GetSnapshotResult result = cosXml.GetSnapshot(request);
//获取图片信息
Console.WriteLine(result.GetResultInfo());
if (result.IsSuccessful())
{
return destPath;
}
else
throw new Exception(result.httpMessage);
}
/// <summary>
/// 获取视频帧,并剪切固定大小
/// </summary>
public void GetCut(string fileKey, int width, int height)
{
string fileName = this.GetSnapshot(fileKey);
//图片剪切处理
ImageOperate.Cut(fileName, width, height);
}
}
三、使用案例
SnapshotHelper _snap = new SnapshotHelper();
//_snap.GetSnapshot("1684292777834-3.mp4");
_snap.GetSnapshot("1731393344259-45.mp4");
更多: