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;
            }
        }
posted @ 2022-08-17 16:13  cv玲玲  阅读(746)  评论(0编辑  收藏  举报