ffmpeg 视频截图

(1)https://bbs.huaweicloud.com/blogs/243409  可以根据此地址来下载ffmpeg  

(2)代码实现

     /// <summary>
        /// 借助ffmpeg生成缩略图
        /// </summary>
        /// <param name="originalFilePath">源文件</param>
        public string GenerateThumbnail(string originalFilePath)
        {
            try
            {
                //判断系统类型
                //如果是windows,直接使用ffmpeg.exe
                //如果是linux,则使用安装的ffmpeg(需要提前安装)
                /*
                  Linux工具调用:ffmpeg -i 333.jpg -q:v 31 -frames:v 1 -y image.jpg
                  windows:  ffmpeg.exe -i 333.jpg -q:v 31 -frames:v 1 -y image.jpg 
                      -i 333.jpg 是输入文件
                      -q:v 31 是质量,值区间是2-31
                      -frames:v 1 是提取帧必要参数
                      -y 是遇到同名文件则覆盖 
                      image.jpg 输出文件名
                      还可以加 -s 160*100 表示输出宽高比为160*100
                 */
                string imgName = DateTime.Now.Ticks.ToString("x");
                string flv_img_p = System.Web.HttpContext.Current.Server.MapPath(imgName + ".jpg"); 
                string cmdPath = "ffmpeg.exe";//ffmpeg工具对象
                //截图的尺寸大小
                string FlvImgSize = ConfigurationManager.AppSettings["CatchFlvImgSize"];
                string cmdParams = $" -i {originalFilePath} -q:v 31 -frames:v 1 -y {flv_img_p} ";//命令参数
                //if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                //{
                //    cmdPath = "ffmpeg.exe";//根据实际的ffmpeg.exe文件路径来
                //}
                //else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                //{
                //    cmdPath = "ffmpeg";//安装ffmpeg工具
                //}
                //else
                //{
                //    throw new Exception("当前操作系统不支持!");
                //} 
                using (System.Diagnostics.Process ffmpegProcess = new System.Diagnostics.Process())
                {
                    StreamReader errorReader;  
                    ffmpegProcess.StartInfo.UseShellExecute = false; 
                    ffmpegProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
                    ffmpegProcess.StartInfo.RedirectStandardError = true; 
                    ffmpegProcess.StartInfo.FileName = cmdPath;  
                    ffmpegProcess.StartInfo.Arguments = cmdParams; 
                    ffmpegProcess.Start();  
                    errorReader = ffmpegProcess.StandardError;  
                    ffmpegProcess.WaitForExit();   
                }

                return flv_img_p;
            }
            catch (Exception ex)
            {
                throw new Exception("生成视频截图出错!", ex);
            }
        }
原文地址:https://www.cnblogs.com/anjingdian/p/14986489.html