ffmpeg使用心得-多张图片处理视频,免安装,快速嵌入到客户端程序事例。
公司突然要求让自动根据图片生成视频,测试了opencvSharp以及ffmpeg,最后发现还是ffmpeg更方便简洁,走了不少弯路 代码和思路记下来,方便自己和大家。
第一步:去官方下载ffmpeg,把ffmpeg.exe放在指定位置。
第二步 编辑cmd命令执行
1 //打开ffmpeg.exe所在目录文件夹 2 string opendic = "cd "+ Application.StartupPath; 3 str.Add(opendic); 4 //第二部启动该exe 5 string startexe = "start ffmpeg.exe"; 6 str.Add(startexe); 7 //string str2 = CMDHelper.ExeCommand(str12); 8 string vediopath = localdicpath + "\\" + ExpandMethods.DateTimeToLongTimeStamp(DateTime.Now) + ".mp4"; 9 //第三步 执行命令 本条命令就是循环图片所在文件夹,生成10s视频 10 string executecommon = string.Format("ffmpeg -loop 1 -f image2 -i {0} -vcodec libx264 -r 2 -t 10 {1}", localdicpath+"\\image%d.jpg", vediopath); 11 str.Add(executecommon); 12 //开始执行命令 13 string status = CMDHelper.ExeCommand(str.ToArray());
第三步,开始执行cmd命令
1 public static string ExeCommand(string[] commandTexts) 2 { 3 Process p = new Process(); 4 p.StartInfo.FileName = "cmd.exe"; 5 p.StartInfo.UseShellExecute = false; 6 p.StartInfo.RedirectStandardInput = true; 7 p.StartInfo.RedirectStandardOutput = true; 8 p.StartInfo.RedirectStandardError = true; 9 p.StartInfo.CreateNoWindow = true; 10 string strOutput = null; 11 string eOut = null; 12 try 13 { 14 15 p.Start(); 16 17 foreach (string item in commandTexts) 18 { 19 p.StandardInput.WriteLine(item); 20 } 21 p.StandardInput.WriteLine("exit"); 22 p.StandardInput.Close(); 23 p.BeginErrorReadLine(); 24 p.ErrorDataReceived += new DataReceivedEventHandler( 25 (sender, e) => { eOut += e.Data; }); 26 p.OutputDataReceived += new DataReceivedEventHandler( 27 (sender, e1) => { strOutput += e1.Data; }); 28 strOutput = p.StandardOutput.ReadToEnd(); 29 strOutput = p.StandardOutput.ReadToEnd(); 30 //var output = new List<string>(); 31 //while (p.StandardOutput.Peek() > -1) 32 //{ 33 // output.Add(p.StandardOutput.ReadLine()); 34 //} 35 36 //while (p.StandardError.Peek() > -1) 37 //{ 38 // output.Add(p.StandardError.ReadLine()); 39 //} 40 p.WaitForExit(); 41 42 43 p.Close(); 44 if (string.IsNullOrEmpty(strOutput)) 45 { 46 strOutput = "ok"; 47 } 48 49 } 50 catch (Exception e) 51 { 52 strOutput = e.Message; 53 } 54 return strOutput; 55 }
好了 到此视频就生成成功了,我感觉这是最方便的视频图片处理方法,我们是客户端程序,省去用户安装,只要大家在学习一些cmd命令就可以方便快速便捷的处理视频。希望对大家有帮助
浙公网安备 33010602011771号