隐藏

C# 调用FFmpeg 根据图片合成视频

发布:2021/3/22 14:12:50作者:管理员 来源:本站 浏览次数:977

1.项目结构:


2.代码:

		
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebFFmpeg
  9. {
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14.  
  15. }
  16.  
  17. protected void Button1_Click(object sender, EventArgs e)
  18. {
  19. string pathString = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
  20. Process p = new Process();
  21. p.StartInfo.FileName = pathString + "\\FFmpeg\\ffmpeg.exe";
  22. p.StartInfo.Arguments = @"-y -r 1 -i " +
  23. pathString + @"FFmpeg\pic\img%2d.jpg -i " +
  24. pathString + @"FFmpeg\music\02.mp3 -s 800x800 -vcodec mpeg4" +
  25. pathString + @"FFmpeg\vedio\out.mp4";
  26. p.StartInfo.UseShellExecute = false;
  27. p.StartInfo.RedirectStandardError = true;
  28. p.StartInfo.Crea teNoWindow = true;
  29. p.ErrorDataReceived += new DataReceivedEventHandler((s, message) => { Response.Write(message.Data); });//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  30. p.Start();//启动线程
  31. p.BeginErrorReadLine();//开始异步读取
  32. p.WaitForExit();//阻塞等待进程结束
  33. p.Close();//关闭进程
  34. p.Dispose();//释放资源
  35.  
  36. Response.Write("<a href='FFmpeg/vedio/out.mp4'>下载</a>");
  37. }
  38. }
  39. }