ffmpeg截取视频第一帧作为封面
/// <summary>
/// 获取动态缩略图
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string GetThumbnail11(string path)
{
try
{
string root = _iHostEnvironment.WebRootPath;
string ext = Path.GetExtension(path)?.ToLower();
string dir = Path.GetDirectoryName(path);
string name = Path.GetFileNameWithoutExtension(path);
string fileHtmlDir = dir + "\\" + name + "\\Thumbnail";
if (!Directory.Exists(root + fileHtmlDir))
{
Directory.CreateDirectory(root + fileHtmlDir);
}
if (File.Exists(root + fileHtmlDir + "\\Thumbnail" + ext))
{
return fileHtmlDir + "\\Thumbnail" + ext;
}
string type = Util.Common.GetFileType(ext);
if (type == "3")
{
return CutOutLocalvideoImages111(path, root + fileHtmlDir + "\\Thumbnail.jpg", fileHtmlDir + "\\Thumbnail.jpg");
}
return null;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 视频获取动态封面
/// </summary>
/// <param name="filePath"></param>
/// <param name="savePath"></param>
/// <returns></returns>
private string CutOutLocalvideoImages111(string filePath, string savePath, string rePath)
{
string result;
try
{
string root = _iHostEnvironment.WebRootPath;
string ffmpeg = _iHostEnvironment.WebRootPath + "\\ffmpeg.exe";
string text = root + filePath;
string text2 = ffmpeg;
if (!File.Exists(text2) || !File.Exists(text))
{
result = "";
}
else
{
string output;
string error;
int? width=0, height=0;
//宽度和高度
ExecuteCommand("\""+text2 + "\"" + " -i " + "\"" + text + "\"", out error);
if (string.IsNullOrEmpty(error))
{
width = null;
height = null;
}
//通过正则表达式获取信息里面的宽度信息
Regex regex = new Regex(@"(\d{2,4})x(\d{2,4})", RegexOptions.Compiled);
Match m = regex.Match(error);
if (m.Success)
{
width = int.Parse(m.Groups[1].Value);
height = int.Parse(m.Groups[2].Value);
}
string text4 = width.ToString()+ "*"+ height.ToString();
ProcessStartInfo processStartInfo = new ProcessStartInfo(text2);
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.Arguments = string.Concat(new string[]
{
" -i ",
text,
" -y -f image2 -t 20 -s ",
text4,
" ",
savePath
});
try
{
Process.Start(processStartInfo);
}
catch
{
result = "";
return result;
}
Thread.Sleep(4000);
if (File.Exists(savePath))
{
result = Path.ChangeExtension(rePath, ".jpg");
}
else
{
result = "";
}
}
}
catch
{
result = "";
}
return result;
}
// <summary>
/// 执行一条command命令
/// </summary>
/// <param name="command">需要执行的Command</param>
/// <param name="output">输出</param>
/// <param name="error">错误</param>
public static void ExecuteCommand(string command, out string error)
{
try
{
//创建一个进程
Process pc = new Process();
pc.StartInfo.FileName = command;
pc.StartInfo.UseShellExecute = false;
pc.StartInfo.RedirectStandardInput = true;
pc.StartInfo.RedirectStandardOutput = false;
pc.StartInfo.RedirectStandardError = true;
pc.StartInfo.CreateNoWindow = true;
//启动进程
pc.Start();
//准备读出输出流和错误流
string outputData = string.Empty;
string errorData = string.Empty;
pc.BeginErrorReadLine();
pc.ErrorDataReceived += (ss, ee) =>
{
errorData += ee.Data;
};
//等待退出
pc.WaitForExit();
//关闭进程
pc.Close();
//返回流结果
error = errorData;
}
catch (Exception)
{
error = null;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!