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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. //Dispatcher.Invoke(() =>
  275. //{
  276. //});
  277. //// 允许不同线程间的调用
  278. //Control.CheckForIllegalCrossThreadCalls = false;
  279. }))).Start();
  280. }
  281. /// <summary>
  282. /// 视频音频合成
  283. /// </summary>
  284. /// <param name="FilePath">存储路径</param>
  285. public void MergeVideoOrAudio(object FilePathobj)
  286. {
  287. //new Thread(new ThreadStart(new Action(() =>
  288. //{
  289. //Dispatcher.Invoke(() =>
  290. //{
  291. //});
  292. //}))).Start();
  293. while (myProcess != null)
  294. {
  295. Thread.Sleep(100);
  296. }
  297. //路径
  298. string FilePath = (string)FilePathobj;
  299. string Path = FileToolsCommon.GetDirectoryName(FilePath);
  300. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  301. foreach (var KillProcess in KillProcessArray)
  302. {
  303. KillProcess.Kill();
  304. }
  305. myProcess = new Process();
  306. LogPath = CreateffmpegLog();
  307. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  308. myProcess.StartInfo.Arguments = "-f concat -safe 0 -i " + Path + "temp/filelist.d -c copy " + FilePath;
  309. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  310. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  311. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  312. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  313. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  314. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  315. myProcess.Start(); //启动线程
  316. myProcess.BeginErrorReadLine(); //开始异步读取
  317. myProcess.WaitForExit(); //阻塞等待进程结束
  318. myProcess.Close(); //关闭进程
  319. myProcess.Dispose(); //释放资源
  320. #region 进程是否已经释放
  321. bool IsRunning = true;
  322. while (IsRunning)
  323. {
  324. IsRunning = false;
  325. Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
  326. foreach (var KillProcess in ProcessArray)
  327. {
  328. IsRunning = true;
  329. Thread.Sleep(100);
  330. }
  331. }
  332. #endregion
  333. myProcess = null;
  334. }
  335. /// <summary>
  336. /// 获取文件完整路径
  337. /// </summary>
  338. /// <param name="FilePath">文件路径</param>
  339. /// <returns></returns>
  340. string GetFilePathName(string FilePath)
  341. {
  342. //路径
  343. string Path = FileToolsCommon.GetDirectoryName(FilePath);
  344. //Path.GetFileName(FilePath);//获取文件名
  345. string Extension = FileToolsCommon.GetIOExtension(FilePath);//扩展名
  346. string tempFilePath = Path + "temp/";
  347. //创建临时目录
  348. FileToolsCommon.CreateDirectory(tempFilePath);
  349. //创建记录文件
  350. if (!FileToolsCommon.IsExistFile(tempFilePath + "filelist.d"))
  351. {
  352. FileToolsCommon.CreateFile(tempFilePath + "filelist.d");
  353. }
  354. int num = 1;
  355. string CompleteFilePath = tempFilePath + num + Extension;
  356. while (FileToolsCommon.IsExistFile(CompleteFilePath))
  357. {
  358. num++;
  359. CompleteFilePath = tempFilePath + num + Extension;
  360. }
  361. FileToolsCommon.AppendText(tempFilePath + "filelist.d", "file ./" + num + Extension + "\r\n");
  362. return CompleteFilePath;
  363. }
  364. /// <summary>
  365. /// 生成缩略图
  366. /// </summary>
  367. /// <param name="VideoPath">视频地址</param>
  368. /// <param name="ImagePath">图片地址</param>
  369. public void GenerateThumbnails(string VideoPath, string ImagePath)
  370. {
  371. while (myProcess != null)
  372. {
  373. Thread.Sleep(100);
  374. }
  375. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  376. foreach (var KillProcess in KillProcessArray)
  377. {
  378. KillProcess.Kill();
  379. }
  380. myProcess = new Process();
  381. LogPath = CreateffmpegLog();
  382. this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
  383. //myProcess.StartInfo.Arguments = "-i \"" + VideoPath + "\" -ss 1 -vframes 1 -r 1 -ac 1 -ab 2 -s " + thubWidth + "*" + thubHeight + " -f image2 \"" + ImagePath + "\"";
  384. myProcess.StartInfo.Arguments = "-i \"" + VideoPath + "\" -ss 1 -vframes 1 -r 1 -ac 1 -ab 2 -f image2 \"" + ImagePath + "\"";
  385. myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  386. myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  387. myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  388. myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  389. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  390. myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
  391. myProcess.Start(); //启动线程
  392. myProcess.BeginErrorReadLine(); //开始异步读取
  393. myProcess.WaitForExit(); //阻塞等待进程结束
  394. myProcess.Close(); //关闭进程
  395. myProcess.Dispose(); //释放资源
  396. #region 进程是否已经释放
  397. bool IsRunning = true;
  398. while (IsRunning)
  399. {
  400. IsRunning = false;
  401. Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
  402. foreach (var KillProcess in ProcessArray)
  403. {
  404. IsRunning = true;
  405. Thread.Sleep(100);
  406. }
  407. }
  408. #endregion
  409. myProcess = null;
  410. }
  411. /// <summary>
  412. /// 创建日志文件
  413. /// </summary>
  414. /// <returns></returns>
  415. string CreateffmpegLog()
  416. {
  417. string LogFileName = DateTime.Now.ToString("yyyyMMdd");
  418. string LogFilePath = FileToolsCommon.GetFileAbsolutePath("/Log/FFMpegLog/");
  419. FileToolsCommon.CreateDirectory(LogFilePath);
  420. string LogName = LogFilePath + LogFileName + ".log";
  421. try
  422. {
  423. if (!FileToolsCommon.IsExistFile(LogName))
  424. {
  425. FileToolsCommon.CreateFile(LogName);
  426. FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
  427. "****************************************************************************************************\r\n");
  428. return LogName;
  429. }
  430. else
  431. {
  432. int num = 0;
  433. while (FileToolsCommon.GetFileSizeByMB(LogName) > 2)
  434. {
  435. num++;
  436. LogName = LogFilePath + LogFileName + "_" + num + ".log";
  437. FileToolsCommon.CreateFile(LogName);
  438. }
  439. FileToolsCommon.AppendText(LogName, "\r\n****************************************************************************************************" +
  440. "****************************************************************************************************\r\n");
  441. return LogName;
  442. }
  443. }
  444. catch (Exception)
  445. {
  446. return LogName;
  447. }
  448. }
  449. /// <summary>
  450. /// 输出结果
  451. /// </summary>
  452. /// <param name="sendProcess"></param>
  453. /// <param name="output"></param>
  454. private void Output(object sendProcess, DataReceivedEventArgs output)
  455. {
  456. if (!String.IsNullOrEmpty(output.Data))
  457. {
  458. FileToolsCommon.AppendText(LogPath, output.Data);
  459. }
  460. }
  461. //private void P_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  462. //{
  463. // //+= new DataReceivedEventHandler((s, message) => { Console.WriteLine(message.Data); })
  464. // using (StreamWriter fs = new StreamWriter("E:\\项目\\测试\\Wpf测试\\bin\\Debug\\ffmpeg\\log.txt", true))
  465. // {
  466. // fs.WriteLine(e.Data);
  467. // Debug.WriteLine(output.Data.ToString());
  468. // }
  469. //}
  470. /// <summary>
  471. /// 获取麦克风
  472. /// </summary>
  473. string GetMicrophone()
  474. {
  475. List<string> devs = new List<string>();
  476. MMDeviceEnumerator enumberator = new MMDeviceEnumerator();
  477. MMDeviceCollection deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
  478. for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
  479. {
  480. WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
  481. foreach (MMDevice device in deviceCollection)
  482. {
  483. try
  484. {
  485. if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
  486. {
  487. devs.Add(device.FriendlyName);
  488. break;
  489. }
  490. }
  491. catch (Exception)
  492. {
  493. }
  494. }
  495. }
  496. if (devs.Count > 0)
  497. {
  498. return devs[0];
  499. }
  500. else
  501. {
  502. return "";
  503. }
  504. }
  505. }
  506. }