|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using VisioForge.Shared.NAudio.CoreAudioApi;
- using VisioForge.Shared.NAudio.Wave;
-
- namespace Common.system
- {
- public class FFMpeg
- {
- Process myProcess = null;
-
- public void RunFFmpeg(string PathName)
- {
-
- Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
- Debug.WriteLine(KillProcessArray.Length.ToString());
- foreach (var KillProcess in KillProcessArray)
- {
- KillProcess.Kill();
- }
- myProcess = new Process();
- string MicrophoneName = GetMicrophone();
- myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
- if(string.IsNullOrWhiteSpace(MicrophoneName))
- {
- 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的参数
- }
- else
- {
- 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的参数
-
- }
- myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
- myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
- myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
- myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
- myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
- myProcess.Start();
- myProcess.BeginErrorReadLine();
-
- }
-
- public void StopFFmpeg()
- {
- //Thread.Sleep(3000);
- if (myProcess == null)
- return;
- myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
- myProcess.Close();
- myProcess.Dispose();
- myProcess = null;
- //myProcess.Kill();
- }
-
- private void Output(object sendProcess, DataReceivedEventArgs output)
- {
- if (!String.IsNullOrEmpty(output.Data))
- Debug.WriteLine(output.Data.ToString());
- }
-
-
- /// <summary>
- /// 获取麦克风
- /// </summary>
- string GetMicrophone()
- {
- List<string> devs = new List<string>();
-
- MMDeviceEnumerator enumberator = new MMDeviceEnumerator();
- MMDeviceCollection deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
- for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
- {
- WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
- foreach (MMDevice device in deviceCollection)
- {
- try
- {
-
- if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
- {
- devs.Add(device.FriendlyName);
- break;
- }
- }
- catch (Exception)
- {
-
- }
- }
- }
- if (devs.Count > 0)
- {
- return devs[0];
- }
- else
- {
- return "";
- }
- }
- }
- }
|