瀏覽代碼

zhao:1优化麦克风获取

tags/录制修改前
耀 4 年之前
父節點
當前提交
31787bb51d
共有 2 個檔案被更改,包括 217 行新增25 行删除
  1. 216
    25
      Common/system/FFMpeg.cs
  2. 1
    0
      XHWK.WKTool/CreateAMicroLessonWindow.xaml.cs

+ 216
- 25
Common/system/FFMpeg.cs 查看文件

@@ -1,6 +1,8 @@
1
-using System;
1
+using AForge.Video.DirectShow;
2
+using System;
2 3
 using System.Collections.Generic;
3 4
 using System.Diagnostics;
5
+using System.IO;
4 6
 using System.Linq;
5 7
 using System.Text;
6 8
 using System.Threading;
@@ -105,7 +107,7 @@ namespace Common.system
105 107
             }
106 108
             if (IsRecordMicrophone)
107 109
             {
108
-                MicrophoneName = GetInDeviceName();
110
+                MicrophoneName = GetMicrophoneName();
109 111
                 if (!string.IsNullOrWhiteSpace(MicrophoneName))
110 112
                 {
111 113
                     MicrophoneStr = "-f dshow -i audio=\"" + MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 ";
@@ -202,7 +204,7 @@ namespace Common.system
202 204
             if (StartRecordAudio(audioMicrophonePath))
203 205
             {
204 206
                 IsRecordMicrophone = true;
205
-                MicrophoneName = GetInDeviceName();
207
+                MicrophoneName = GetMicrophoneName();
206 208
             }
207 209
             else
208 210
             {
@@ -759,48 +761,195 @@ namespace Common.system
759 761
                 FileToolsCommon.AppendText(LogPath, output.Data);
760 762
             }
761 763
         }
762
-
764
+        #region 麦克风声卡检测
765
+        #region 获取麦克风
766
+        bool IsMicrophone = false;
763 767
         /// <summary>
764
-        /// 获取麦克风
768
+        /// 使用FFmpeg获取麦克风
765 769
         /// </summary>
766
-        private string GetMicrophone()
770
+        string MicrophoneNameInfo = "";
771
+        /// <summary>
772
+        /// 获取麦克风名称
773
+        /// </summary>
774
+        /// <returns></returns>
775
+        public string GetMicrophoneName()
767 776
         {
768
-            List<string> devs = new List<string>();
777
+            return GetAudioInDevices();
778
+            //string FullName = GetInDeviceFull();
779
+            //if(string.IsNullOrWhiteSpace(MicrophoneNameInfo))
780
+            //{
781
+            //    return FullName;
782
+            //}
783
+            ////byte[] bufferASCII = Encoding.Default.GetBytes(MicrophoneNameInfo);
784
+            ////string str = Encoding.UTF8.GetString(bufferASCII);
785
+            ////str = str.Replace("?", " ");
786
+            ////int num = str.Length;
787
+            ////int num1 = FullName.Length;
788
+            //if (FullName.Contains("(") || FullName.Contains("("))
789
+            //{
790
+            //    string EName = FullName.Substring(FullName.IndexOf("(")+1);
791
+            //    if(string.IsNullOrEmpty(EName))
792
+            //    {
793
+            //        EName = FullName.Substring(FullName.IndexOf("(") + 1);
794
+            //        EName = EName.Substring(0, EName.LastIndexOf(")"));
795
+            //    }
796
+            //    else
797
+            //    {
798
+            //        //EName = EName.Substring(0, EName.LastIndexOf(")"));
799
+            //    }
800
+            //    if(MicrophoneNameInfo.Contains(EName))
801
+            //    {
802
+            //        return FullName;
803
+            //    }
804
+            //    else
805
+            //    {
806
+            //        return GetInDeviceName();
807
+            //    }
808
+            //}
809
+            //else
810
+            //{
811
+            //    return FullName;
812
+            //}
813
+        }
814
+        /// <summary>
815
+        /// 使用FFmpeg获取设备名称--待定
816
+        /// </summary>
817
+        public void GetMToFFmpeg()
818
+        {
819
+            return;
820
+            new Thread(new ThreadStart(new Action(() =>
821
+            {
822
+                try
823
+                {
769 824
 
770
-            MMDeviceEnumerator enumberator = new MMDeviceEnumerator();
771
-            MMDeviceCollection deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
772
-            for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
825
+                while (myProcess != null)
826
+                {
827
+                    Thread.Sleep(100);
828
+                }
829
+                myProcess = new Process();
830
+                LogPath = CreateffmpegLog();
831
+                myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe");   //ffmpeg.exe的绝对路径
832
+                myProcess.StartInfo.Arguments = " -list_devices true -f dshow -i dummy";
833
+                if (OutputVideoLog)
834
+                    LogHelper.WriteInfoLog("【音视频合成】:" + myProcess.StartInfo.Arguments);
835
+                myProcess.StartInfo.UseShellExecute = false;           //不使用操作系统外壳程序启动
836
+                myProcess.StartInfo.RedirectStandardError = true;      //重定向标准错误输出
837
+                myProcess.StartInfo.CreateNoWindow = true;             //不显示程序窗口
838
+                myProcess.StartInfo.RedirectStandardInput = true;      //用于模拟该进程控制台的输入
839
+                                                                       //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
840
+                myProcess.ErrorDataReceived += GetDevInfoData;
841
+                myProcess.Start();                 //启动线程
842
+                myProcess.BeginErrorReadLine();    //开始异步读取
843
+                myProcess.WaitForExit();           //阻塞等待进程结束
844
+                myProcess.Close();                 //关闭进程
845
+                myProcess.Dispose();               //释放资源
846
+                myProcess = null;
847
+                }
848
+                catch (Exception ex)
849
+                {
850
+                    LogHelper.WriteErrLog("【获取设备信息】(GetMToFFmpeg)信息获取失败:" + ex.Message, ex);
851
+                }
852
+
853
+            }))).Start();
854
+        }
855
+        /// <summary>
856
+        /// 使用FFmpeg获取设备名称--待定
857
+        /// </summary>
858
+        /// <param name="sender"></param>
859
+        /// <param name="e"></param>
860
+        private void GetDevInfoData(object sender, DataReceivedEventArgs e)
861
+        {
862
+            if (!string.IsNullOrEmpty(e.Data))
773 863
             {
774
-                WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
775
-                foreach (MMDevice device in deviceCollection)
864
+                if (IsMicrophone)
776 865
                 {
777 866
                     try
778 867
                     {
779
-
780
-                        if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
781
-                        {
782
-                            devs.Add(device.FriendlyName);
783
-                            break;
784
-                        }
868
+                        MicrophoneNameInfo = e.Data;
869
+                        MicrophoneNameInfo = MicrophoneNameInfo.Substring(MicrophoneNameInfo.IndexOf("]") + 1);
870
+                        MicrophoneNameInfo = MicrophoneNameInfo.Replace("\"", "");
871
+                        MicrophoneNameInfo = MicrophoneNameInfo.Trim();
872
+                        #region 测试
873
+                        //byte[] bufferASCII = Encoding.Default.GetBytes(MicrophoneNameInfo);
874
+                        //MicrophoneNameInfo = Encoding.UTF8.GetString(bufferASCII); // 统一使用UTF-8
875
+                        ////System.Text.Encoding GB2312 = System.Text.Encoding.GetEncoding("GB2312");
876
+                        ////byte[] gb = GB2312.GetBytes(MicrophoneNameInfo);
877
+                        ////MicrophoneNameInfo = Encoding.UTF8.GetString(gb);
878
+                        #endregion
879
+                        IsMicrophone = false;
880
+                        FileToolsCommon.AppendText(LogPath, e.Data);
881
+                        //FileToolsCommon.AppendText(LogPath, "\r\n***************************************************\r\n");
785 882
                     }
786
-                    catch (Exception)
883
+                    catch (Exception ex)
787 884
                     {
788
-
885
+                        LogHelper.WriteErrLog("【获取设备信息】(GetDevInfoData)信息获取失败:" + ex.Message, ex);
886
+                    }
887
+                }
888
+                else
889
+                {
890
+                    if (e.Data.Contains("DirectShow audio devices"))
891
+                    {
892
+                        //FileToolsCommon.AppendText(LogPath, "\r\n***************************************************\r\n");
893
+                        IsMicrophone = true;
789 894
                     }
895
+                    FileToolsCommon.AppendText(LogPath, e.Data);
896
+                    FileToolsCommon.AppendText(LogPath, "\r\n");
790 897
                 }
791 898
             }
792
-            if (devs.Count > 0)
899
+        }
900
+
901
+        /// <summary>
902
+        /// AForge获取麦克风
903
+        /// </summary>
904
+        /// <returns></returns>
905
+        public static string GetAudioInDevices()
906
+        {
907
+            List<string> devicesList = new List<string>();
908
+            try
793 909
             {
794
-                return devs[0];
910
+                var videoDevices = new FilterInfoCollection(FilterCategory.AudioInputDevice);
911
+                foreach (FilterInfo device in videoDevices)
912
+                {
913
+                    devicesList.Add(device.Name);
914
+                }
915
+            }
916
+            catch (ApplicationException)
917
+            {
918
+                //Console.WriteLine("No local capture devices");
919
+            }
920
+
921
+            if (devicesList.Count > 1)
922
+            {
923
+                return devicesList[1];
795 924
             }
796 925
             else
797 926
             {
798 927
                 return "";
799 928
             }
800 929
         }
801
-
802 930
         /// <summary>
803
-        /// 获取声音输入设备
931
+        /// AForge获取麦克风列表
932
+        /// </summary>
933
+        /// <returns></returns>
934
+        public static List<string> GetAudioInDevicesList()
935
+        {
936
+            List<string> devicesList = new List<string>();
937
+            try
938
+            {
939
+                var videoDevices = new FilterInfoCollection(FilterCategory.AudioInputDevice);
940
+                foreach (FilterInfo device in videoDevices)
941
+                {
942
+                    devicesList.Add(device.Name);
943
+                }
944
+            }
945
+            catch (ApplicationException)
946
+            {
947
+                Console.WriteLine("No local capture devices");
948
+            }
949
+            return devicesList;
950
+        }
951
+        /// <summary>
952
+        /// 获取声音输入设备名称   不完整
804 953
         /// </summary>
805 954
         /// <returns></returns>
806 955
         public static string GetInDeviceName()
@@ -865,7 +1014,7 @@ namespace Common.system
865 1014
             }
866 1015
         }
867 1016
         /// <summary>
868
-        /// 获取声音输入设备完整名称
1017
+        /// 获取声音输入设备完整名称列表
869 1018
         /// </summary>
870 1019
         /// <returns></returns>
871 1020
         public List<string> GetInDeviceListFull()
@@ -894,7 +1043,47 @@ namespace Common.system
894 1043
             }
895 1044
             return devices;
896 1045
         }
1046
+        /// <summary>
1047
+        /// 获取麦克风---废弃
1048
+        /// </summary>
1049
+        private string GetMicrophone()
1050
+        {
1051
+            List<string> devs = new List<string>();
1052
+
1053
+            MMDeviceEnumerator enumberator = new MMDeviceEnumerator();
1054
+            MMDeviceCollection deviceCollection = enumberator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
1055
+            for (int waveInDevice = 0; waveInDevice < WaveIn.DeviceCount; waveInDevice++)
1056
+            {
1057
+                WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
1058
+                foreach (MMDevice device in deviceCollection)
1059
+                {
1060
+                    try
1061
+                    {
1062
+
1063
+                        if (device.FriendlyName.StartsWith(deviceInfo.ProductName))
1064
+                        {
1065
+                            devs.Add(device.FriendlyName);
1066
+                            break;
1067
+                        }
1068
+                    }
1069
+                    catch (Exception)
1070
+                    {
1071
+
1072
+                    }
1073
+                }
1074
+            }
1075
+            if (devs.Count > 0)
1076
+            {
1077
+                return devs[0];
1078
+            }
1079
+            else
1080
+            {
1081
+                return "";
1082
+            }
1083
+        }
1084
+        #endregion
897 1085
 
1086
+        #region 检测是否可用
898 1087
         /// <summary>
899 1088
         /// 录制麦克风的声音
900 1089
         /// </summary>
@@ -1029,5 +1218,7 @@ namespace Common.system
1029 1218
                 return false;
1030 1219
             }
1031 1220
         }
1221
+        #endregion
1222
+        #endregion
1032 1223
     }
1033 1224
 }

+ 1
- 0
XHWK.WKTool/CreateAMicroLessonWindow.xaml.cs 查看文件

@@ -33,6 +33,7 @@ namespace XHWK.WKTool
33 33
                 APP.InstallScreenCapturerRecorder();
34 34
             }
35 35
             txbStoragePath.Text = FileToolsCommon.GetConfigValue("VideoSavePath");
36
+            APP.FFmpeg.GetMToFFmpeg();
36 37
         }
37 38
 
38 39
         #region 事件

Loading…
取消
儲存