星火微课系统客户端
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 26KB

4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
4 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Threading;
  5. using VisioForge.Shared.NAudio.CoreAudioApi;
  6. using VisioForge.Shared.NAudio.Wave;
  7. namespace Common.system
  8. {
  9. /// <summary>
  10. /// ffmpeg帮助类
  11. /// 创建人:赵耀
  12. /// 创建时间::2020年8月22日
  13. /// 需要安装\ffmpeg\bin\Setup Screen Capturer Recorder v0.12.10.exe
  14. /// 本地调试需要配置环境变量 将ffmpeg.exe位置配置到环境变量的path中
  15. /// </summary>
  16. public class FFMpeg
  17. {
  18. public Process myProcess = null;
  19. /// <summary>
  20. /// ffmpeg输出日志文件地址 每个文件2MB左右
  21. /// </summary>
  22. string LogPath = "";
  23. /// <summary>
  24. /// 录制屏幕
  25. /// </summary>
  26. /// <param name="FilePath">文件存储路径</param>
  27. public void StartRecordingVideo(string FilePath)
  28. {
  29. while (myProcess != null)
  30. {
  31. Thread.Sleep(100);
  32. }
  33. //路径
  34. string Path = FileToolsCommon.GetDirectoryName(FilePath);
  35. string Extension = FileToolsCommon.GetIOExtension(FilePath);//扩展名
  36. //文件保存路径
  37. string PathName = GetFilePathName(FilePath);
  38. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  39. foreach (var KillProcess in KillProcessArray)
  40. {
  41. KillProcess.Kill();
  42. }
  43. myProcess = new Process();
  44. LogPath = CreateffmpegLog();
  45. string MicrophoneName = GetMicrophone();
  46. myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  47. switch (Extension.ToUpper())
  48. {
  49. case "MP4":
  50. if (string.IsNullOrWhiteSpace(MicrophoneName))
  51. {
  52. 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的参数
  53. }
  54. else
  55. {
  56. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
  57. 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 " + PathName; //ffmpeg的参数
  58. }
  59. break;
  60. case "AVI":
  61. case "FLV":
  62. if (string.IsNullOrWhiteSpace(MicrophoneName))
  63. {
  64. 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 " + Extension.ToLower() + " " + PathName; //ffmpeg的参数
  65. }
  66. else
  67. {
  68. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
  69. 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 " + Extension.ToLower() + " " + PathName; //ffmpeg的参数
  70. }
  71. break;
  72. default:
  73. if (string.IsNullOrWhiteSpace(MicrophoneName))
  74. {
  75. 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的参数
  76. }
  77. else
  78. {
  79. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
  80. 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 " + PathName; //ffmpeg的参数
  81. }
  82. break;
  83. }
  84. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  85. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  86. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  87. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  88. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  89. myProcess.Start();
  90. myProcess.BeginErrorReadLine();
  91. }
  92. /// <summary>
  93. /// 录制音频
  94. /// </summary>
  95. /// <param name="Mp3Path">MP3音频位置</param>
  96. public void StartRecordingAudio(string Mp3Path)
  97. {
  98. while (myProcess != null)
  99. {
  100. Thread.Sleep(100);
  101. }
  102. //文件保存路径
  103. string PathName = GetFilePathName(Mp3Path);
  104. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  105. foreach (var KillProcess in KillProcessArray)
  106. {
  107. KillProcess.Kill();
  108. }
  109. myProcess = new Process();
  110. LogPath = CreateffmpegLog();
  111. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  112. string MicrophoneName = GetMicrophone();
  113. if (string.IsNullOrWhiteSpace(MicrophoneName))
  114. {
  115. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" " + PathName;
  116. }
  117. else
  118. {
  119. myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
  120. MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 " + PathName;
  121. }
  122. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  123. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  124. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  125. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  126. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  127. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  128. myProcess.Start(); //启动线程
  129. myProcess.BeginErrorReadLine(); //开始异步读取
  130. }
  131. /// <summary>
  132. /// 暂停录制
  133. /// </summary>
  134. public bool SuspendFFmpeg()
  135. {
  136. if (myProcess != null)
  137. {
  138. myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
  139. myProcess.Close();//关闭进程
  140. myProcess.Dispose();//释放资源
  141. }
  142. #region 进程是否已经释放
  143. bool IsRunning = true;
  144. while (IsRunning)
  145. {
  146. IsRunning = false;
  147. Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
  148. foreach (var KillProcess in ProcessArray)
  149. {
  150. IsRunning = true;
  151. Thread.Sleep(100);
  152. }
  153. }
  154. #endregion
  155. myProcess = null;
  156. return true;
  157. //myProcess.Kill();
  158. }
  159. /// <summary>
  160. /// 停止录制并合成文件
  161. /// </summary>
  162. /// <param name="FilePath">文件存储路径 必须与开始时的路径完全一致</param>
  163. public bool StopFFmpeg(string FilePath)
  164. {
  165. //文件完整路径
  166. if (myProcess != null)
  167. {
  168. myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
  169. myProcess.Close();//关闭进程
  170. myProcess.Dispose();//释放资源
  171. }
  172. #region 进程是否已经释放
  173. bool IsRunning = true;
  174. while (IsRunning)
  175. {
  176. IsRunning = false;
  177. Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
  178. foreach (var KillProcess in ProcessArray)
  179. {
  180. IsRunning = true;
  181. Thread.Sleep(100);
  182. }
  183. }
  184. #endregion
  185. myProcess = null;
  186. Thread t1 = new Thread(MergeVideoOrAudio);
  187. t1.Start(FilePath);
  188. return true;
  189. //文件合成
  190. }
  191. /// <summary>
  192. /// 图片音频合成视频
  193. /// </summary>
  194. /// <param name="ImageListPath">图片列表位置 命名为1.png 2.png</param>
  195. /// <param name="Mp3Path">MP3音频位置</param>
  196. /// <param name="VideoSavePath">视频保存位置</param>
  197. /// <param name="Frequency">每秒帧率</param>
  198. /// <param name="VideoWidth">视频宽度</param>
  199. /// <param name="VideoHeight">视频高度</param>
  200. public void SynthesisVideo(string ImageListPath, string Mp3Path, string VideoSavePath, int Frequency, int VideoWidth, int VideoHeight)
  201. {
  202. new Thread(new ThreadStart(new Action(() =>
  203. {
  204. Thread.Sleep(500);
  205. while (myProcess != null)
  206. {
  207. Thread.Sleep(100);
  208. }
  209. string Extension = FileToolsCommon.GetIOExtension(VideoSavePath);//扩展名
  210. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  211. //Debug.WriteLine(KillProcessArray.Length.ToString());
  212. foreach (var KillProcess in KillProcessArray)
  213. {
  214. KillProcess.Kill();
  215. }
  216. myProcess = new Process();
  217. LogPath = CreateffmpegLog();
  218. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  219. switch (Extension.ToUpper())
  220. {
  221. case "MP4":
  222. myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
  223. ImageListPath + @"%d.png -i " +
  224. Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
  225. VideoSavePath;
  226. break;
  227. case "AVI":
  228. myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
  229. ImageListPath + @"%d.png -i " +
  230. Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f AVI " +
  231. VideoSavePath;
  232. break;
  233. case "FLV":
  234. myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
  235. ImageListPath + @"%d.png -i " +
  236. Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f FLV " +
  237. VideoSavePath;
  238. break;
  239. default:
  240. myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
  241. ImageListPath + @"%d.png -i " +
  242. Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
  243. VideoSavePath;
  244. break;
  245. }
  246. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  247. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  248. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  249. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  250. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  251. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  252. myProcess.Start(); //启动线程
  253. myProcess.BeginErrorReadLine(); //开始异步读取
  254. myProcess.WaitForExit(); //阻塞等待进程结束
  255. myProcess.Close(); //关闭进程
  256. myProcess.Dispose(); //释放资源
  257. #region 进程是否已经释放
  258. bool IsRunning = true;
  259. while (IsRunning)
  260. {
  261. IsRunning = false;
  262. Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
  263. foreach (var KillProcess in ProcessArray)
  264. {
  265. IsRunning = true;
  266. Thread.Sleep(100);
  267. }
  268. }
  269. #endregion
  270. myProcess = null;
  271. //生成视频后删除图片和音频保存列表
  272. //FileToolsCommon.DeleteDirectory(ImageListPath);
  273. //FileToolsCommon.DeleteDirectory(FileToolsCommon.GetDirectoryName(Mp3Path));
  274. FileToolsCommon.DeleteDirectory(FileToolsCommon.GetDirectoryName(VideoSavePath) + "temp/");
  275. //Dispatcher.Invoke(() =>
  276. //{
  277. //});
  278. //// 允许不同线程间的调用
  279. //Control.CheckForIllegalCrossThreadCalls = false;
  280. }))).Start();
  281. }
  282. /// <summary>
  283. /// 视频音频合成
  284. /// </summary>
  285. /// <param name="FilePath">存储路径</param>
  286. public void MergeVideoOrAudio(object FilePathobj)
  287. {
  288. //new Thread(new ThreadStart(new Action(() =>
  289. //{
  290. //Dispatcher.Invoke(() =>
  291. //{
  292. //});
  293. //}))).Start();
  294. while (myProcess != null)
  295. {
  296. Thread.Sleep(100);
  297. }
  298. //路径
  299. string FilePath = (string)FilePathobj;
  300. string Path = FileToolsCommon.GetDirectoryName(FilePath);
  301. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  302. foreach (var KillProcess in KillProcessArray)
  303. {
  304. KillProcess.Kill();
  305. }
  306. myProcess = new Process();
  307. LogPath = CreateffmpegLog();
  308. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  309. myProcess.StartInfo.Arguments = "-f concat -safe 0 -i " + Path + "temp/filelist.d -c copy " + FilePath;
  310. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  311. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  312. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  313. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  314. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  315. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  316. myProcess.Start(); //启动线程
  317. myProcess.BeginErrorReadLine(); //开始异步读取
  318. myProcess.WaitForExit(); //阻塞等待进程结束
  319. myProcess.Close(); //关闭进程
  320. myProcess.Dispose(); //释放资源
  321. #region 进程是否已经释放
  322. bool IsRunning = true;
  323. while (IsRunning)
  324. {
  325. IsRunning = false;
  326. Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
  327. foreach (var KillProcess in ProcessArray)
  328. {
  329. IsRunning = true;
  330. Thread.Sleep(100);
  331. }
  332. }
  333. #endregion
  334. myProcess = null;
  335. FileToolsCommon.DeleteDirectory(Path + "temp/");
  336. }
  337. /// <summary>
  338. /// 获取文件完整路径
  339. /// </summary>
  340. /// <param name="FilePath">文件路径</param>
  341. /// <returns></returns>
  342. string GetFilePathName(string FilePath)
  343. {
  344. //路径
  345. string Path = FileToolsCommon.GetDirectoryName(FilePath);
  346. //Path.GetFileName(FilePath);//获取文件名
  347. string Extension = FileToolsCommon.GetIOExtension(FilePath);//扩展名
  348. string tempFilePath = Path + "temp/";
  349. //创建临时目录
  350. FileToolsCommon.CreateDirectory(tempFilePath);
  351. //创建记录文件
  352. if (!FileToolsCommon.IsExistFile(tempFilePath + "filelist.d"))
  353. {
  354. FileToolsCommon.CreateFile(tempFilePath + "filelist.d");
  355. }
  356. int num = 1;
  357. string CompleteFilePath = tempFilePath + num + Extension;
  358. while (FileToolsCommon.IsExistFile(CompleteFilePath))
  359. {
  360. num++;
  361. CompleteFilePath = tempFilePath + num + Extension;
  362. }
  363. FileToolsCommon.AppendText(tempFilePath + "filelist.d", "file ./" + num + Extension + "\r\n");
  364. return CompleteFilePath;
  365. }
  366. /// <summary>
  367. /// 生成缩略图
  368. /// </summary>
  369. /// <param name="VideoPath">视频地址</param>
  370. /// <param name="ImagePath">图片地址</param>
  371. public void GenerateThumbnails(string VideoPath, string ImagePath)
  372. {
  373. while (myProcess != null)
  374. {
  375. Thread.Sleep(100);
  376. }
  377. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  378. foreach (var KillProcess in KillProcessArray)
  379. {
  380. KillProcess.Kill();
  381. }
  382. myProcess = new Process();
  383. LogPath = CreateffmpegLog();
  384. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  385. //myProcess.StartInfo.Arguments = "-i \"" + VideoPath + "\" -ss 1 -vframes 1 -r 1 -ac 1 -ab 2 -s " + thubWidth + "*" + thubHeight + " -f image2 \"" + ImagePath + "\"";
  386. myProcess.StartInfo.Arguments = "-i \"" + VideoPath + "\" -ss 1 -vframes 1 -r 1 -ac 1 -ab 2 -f image2 \"" + ImagePath + "\"";
  387. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  388. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  389. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  390. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  391. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  392. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  393. myProcess.Start(); //启动线程
  394. myProcess.BeginErrorReadLine(); //开始异步读取
  395. myProcess.WaitForExit(); //阻塞等待进程结束
  396. myProcess.Close(); //关闭进程
  397. myProcess.Dispose(); //释放资源
  398. #region 进程是否已经释放
  399. bool IsRunning = true;
  400. while (IsRunning)
  401. {
  402. IsRunning = false;
  403. Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
  404. foreach (var KillProcess in ProcessArray)
  405. {
  406. IsRunning = true;
  407. Thread.Sleep(100);
  408. }
  409. }
  410. #endregion
  411. myProcess = null;
  412. }
  413. /// <summary>
  414. /// 创建日志文件
  415. /// </summary>
  416. /// <returns></returns>
  417. string CreateffmpegLog()
  418. {
  419. string LogFileName = DateTime.Now.ToString("yyyyMMdd");
  420. string LogFilePath = FileToolsCommon.GetFileAbsolutePath("/Log/FFMpegLog/");
  421. FileToolsCommon.CreateDirectory(LogFilePath);
  422. string LogName = LogFilePath + LogFileName + ".log";
  423. try
  424. {
  425. if (!FileToolsCommon.IsExistFile(LogName))
  426. {
  427. FileToolsCommon.CreateFile(LogName);
  428. FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
  429. "****************************************************************************************************\r\n");
  430. return LogName;
  431. }
  432. else
  433. {
  434. int num = 0;
  435. while (FileToolsCommon.GetFileSizeByMB(LogName) > 2)
  436. {
  437. num++;
  438. LogName = LogFilePath + LogFileName + "_" + num + ".log";
  439. FileToolsCommon.CreateFile(LogName);
  440. }
  441. FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
  442. "****************************************************************************************************\r\n");
  443. return LogName;
  444. }
  445. }
  446. catch (Exception)
  447. {
  448. return LogName;
  449. }
  450. }
  451. /// <summary>
  452. /// 输出结果
  453. /// </summary>
  454. /// <param name="sendProcess"></param>
  455. /// <param name="output"></param>
  456. private void Output(object sendProcess, DataReceivedEventArgs output)
  457. {
  458. if (!String.IsNullOrEmpty(output.Data))
  459. {
  460. FileToolsCommon.AppendText(LogPath, output.Data);
  461. }
  462. }
  463. //private void P_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  464. //{
  465. // //+= new DataReceivedEventHandler((s, message) => { Console.WriteLine(message.Data); })
  466. // using (StreamWriter fs = new StreamWriter("E:\\项目\\测试\\Wpf测试\\bin\\Debug\\ffmpeg\\log.txt", true))
  467. // {
  468. // fs.WriteLine(e.Data);
  469. // Debug.WriteLine(output.Data.ToString());
  470. // }
  471. //}
  472. /// <summary>
  473. /// 获取麦克风
  474. /// </summary>
  475. string GetMicrophone()
  476. {
  477. List<string> devs = new List<string>();
  478. MMDeviceEnumerator enumberator = new MMDeviceEnumerator();
  479. MMDeviceCollection deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
  480. for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
  481. {
  482. WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
  483. foreach (MMDevice device in deviceCollection)
  484. {
  485. try
  486. {
  487. if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
  488. {
  489. devs.Add(device.FriendlyName);
  490. break;
  491. }
  492. }
  493. catch (Exception)
  494. {
  495. }
  496. }
  497. }
  498. if (devs.Count > 0)
  499. {
  500. return devs[0];
  501. }
  502. else
  503. {
  504. return "";
  505. }
  506. }
  507. /// <summary>
  508. /// 获取麦克风列表
  509. /// </summary>
  510. public List<string> GetMicrophoneList()
  511. {
  512. List<string> devs = new List<string>();
  513. MMDeviceEnumerator enumberator = new MMDeviceEnumerator();
  514. MMDeviceCollection deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
  515. for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
  516. {
  517. WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
  518. foreach (MMDevice device in deviceCollection)
  519. {
  520. try
  521. {
  522. if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
  523. {
  524. devs.Add(device.FriendlyName);
  525. break;
  526. }
  527. }
  528. catch (Exception)
  529. {
  530. }
  531. }
  532. }
  533. return devs;
  534. }
  535. }
  536. }