ffmpeg.exe 4.31下载地址
注意资源的释放!!
/// <summary>
/// 生成视频缩略图
/// </summary>
/// <param name="ffmpeg">ffmpeg插件精确路径</param>
/// <param name="vFileName">视频精确路径</param>
/// <param name="flv_img_p">生成的图片路径</param>
/// <returns></returns>
public static bool ExecuteThumbnail(string ffmpeg, string vFileName,string flv_img_p)
{
string flvImgSize= ExecuteCommand(ffmpeg, vFileName);//大小可自行配置或根据视频大小来设置,这里是根据视频大小来的
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
//此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
startInfo.Arguments = " -i " + vFileName + " -y -f image2 -t 0.1 -s " + flvImgSize + " " + flv_img_p;
try
{
System.Diagnostics.Process.Start(startInfo);//生成可能需要一点时间,若要即时建议增加等待,视频越大处理越慢
//System.Threading.Thread.Sleep(2000); //2秒
return true;
}
catch (Exception e)
{
return false;
}
}
/// <summary>
/// 执行ffmpeg代码获取视频信息
/// </summary>
/// <param name="ffmpeg">ffmpeg插件精确路径</param>
/// <param name="vFileName">视频绝对路径</param>
public static string ExecuteCommand(string ffmpeg, string vFileName)
{
try
{
//创建一个进程
Process pc = new Process();
pc.StartInfo.FileName = "\"" + ffmpeg + "\"" + " -i " + "\"" + vFileName + "\"";
pc.StartInfo.UseShellExecute = false;
pc.StartInfo.RedirectStandardOutput = true;
pc.StartInfo.RedirectStandardError = true;
pc.StartInfo.CreateNoWindow = true;
//启动进程
pc.Start();
//准备读出输出流和错误流
string outputData = string.Empty;
string errorData = string.Empty;
pc.BeginOutputReadLine();
pc.BeginErrorReadLine();
pc.OutputDataReceived += (ss, ee) =>
{
outputData += ee.Data;
};
pc.ErrorDataReceived += (ss, ee) =>
{
errorData += ee.Data;
};
//等待退出
pc.WaitForExit();
//关闭进程
pc.Close();
//通过正则表达式获取信息里面的宽度信息
Regex regex = new Regex("(\\d{2,4})x(\\d{2,4})", RegexOptions.Compiled);
Match m = regex.Match(errorData);
if (m.Success)
{
return m.Groups[1].Value + "x" + m.Groups[2].Value;
}
else
{
return "544x960";
}
}
catch (Exception)
{
return "544x960";
}
}
借鉴:
https://www.cnblogs.com/wiseblog/articles/12236914.html
更好的方法:
https://www.cnblogs.com/weisenz/archive/2012/04/17/2453421.html