后台自动截取视频图片 for .net

通过后台代码调用cmd命令与ffmpeg程序实现后台自动截取视频图片

代码:

 1         /// <summary>
 2         /// 
 3         /// </summary>
 4         /// <param name="path">视频路径</param>
 5         /// <param name="imgFile">图片保存路径</param>
 6         /// <returns></returns>
 7         public static string CatchImg(string path, string imgFile)
 8         {
 9             string flv_img = imgFile + ".jpg";
10             //path = @"D:VideoUploadvideos2582938-a586-4814-9f58-af0db57f5d6d.flv";
11             //flv_img = @"D:VideoUploadTopImgx.jpg";
12             String folder = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
13             var ffmpegPath = folder + @"Contentffmpeg";
14             System.Diagnostics.Process p = new System.Diagnostics.Process();
15             p.StartInfo.FileName = "cmd.exe";//要执行的程序名称 
16             p.StartInfo.UseShellExecute = false;
17             p.StartInfo.RedirectStandardInput = true;//可能接受来自调用程序的输入信息
18             p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息 
19             p.StartInfo.CreateNoWindow = true;//不显示程序窗口 
20             if (p.Start()) //开始进程
21             {
22                 //向CMD窗口发送输入信息: 
23                 //p.StandardInput.WriteLine(" ffmpeg -ss 00:00:02 -i flv.flv -f image2 -y mm.jpg ");
24                 p.StandardInput.WriteLine(ffmpegPath+@"ffmpeg -ss 00:00:02 -i "+path+@" -f image2 -y "+flv_img +" &exit");
25                 p.StandardInput.AutoFlush = true;
26                 p.WaitForExit();
27                 p.Close();
28             }
29             
30             //p.StandardInput.WriteLine("ffmpeg -i " + path + " -y -f image2 -ss 2 -t 0.001 -s 350x240 test4.jpg");
31             //-ss表示搜索到指定的时间 -i表示输入的文件 -y表示覆盖输出 -f表示强制使用的格式
32             //string x = p.StandardOutput.ReadToEnd();
33             //p.Kill();
34             if (System.IO.File.Exists(flv_img))
35             {
36                 return flv_img;
37             }
38 
39             return "";
40         }

ffmpeg下载地址:http://ffmpeg.org/download.html

原文地址:https://www.cnblogs.com/viruscih/p/10251167.html