星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FFMpeg.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using VisioForge.Shared.NAudio.CoreAudioApi;
  7. using VisioForge.Shared.NAudio.Wave;
  8. namespace Common.system
  9. {
  10. public class FFMpeg
  11. {
  12. Process myProcess = null;
  13. public void RunFFmpeg(string PathName)
  14. {
  15. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  16. Debug.WriteLine(KillProcessArray.Length.ToString());
  17. foreach (var KillProcess in KillProcessArray)
  18. {
  19. KillProcess.Kill();
  20. }
  21. myProcess = new Process();
  22. string MicrophoneName = GetMicrophone();
  23. myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  24. if(string.IsNullOrWhiteSpace(MicrophoneName))
  25. {
  26. myProcess.StartInfo.Arguments = "-f dshow -i video=\"screen-capture-recorder\" -f dshow -i audio=\"virtual-audio-capturer\" -vcodec libx264 -acodec libmp3lame -r 15 -crf 22 -f avi " + PathName; //ffmpeg的参数
  27. }
  28. else
  29. {
  30. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" + MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f dshow -i video=\"screen-capture-recorder\" -pix_fmt yuv420p -vcodec h264 -preset:v ultrafast -tune:v zerolatency -acodec aac -ar 44100 -ac 2 -f avi " + PathName; //ffmpeg的参数
  31. }
  32. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  33. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  34. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  35. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  36. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  37. myProcess.Start();
  38. myProcess.BeginErrorReadLine();
  39. }
  40. public void StopFFmpeg()
  41. {
  42. //Thread.Sleep(3000);
  43. if (myProcess == null)
  44. return;
  45. myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
  46. myProcess.Close();
  47. myProcess.Dispose();
  48. myProcess = null;
  49. //myProcess.Kill();
  50. }
  51. private void Output(object sendProcess, DataReceivedEventArgs output)
  52. {
  53. if (!String.IsNullOrEmpty(output.Data))
  54. Debug.WriteLine(output.Data.ToString());
  55. }
  56. /// <summary>
  57. /// 获取麦克风
  58. /// </summary>
  59. string GetMicrophone()
  60. {
  61. List<string> devs = new List<string>();
  62. MMDeviceEnumerator enumberator = new MMDeviceEnumerator();
  63. MMDeviceCollection deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
  64. for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
  65. {
  66. WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
  67. foreach (MMDevice device in deviceCollection)
  68. {
  69. try
  70. {
  71. if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
  72. {
  73. devs.Add(device.FriendlyName);
  74. break;
  75. }
  76. }
  77. catch (Exception)
  78. {
  79. }
  80. }
  81. }
  82. if (devs.Count > 0)
  83. {
  84. return devs[0];
  85. }
  86. else
  87. {
  88. return "";
  89. }
  90. }
  91. }
  92. }