• 【C#】【FFmpeg】获取电脑可用音视频设备并输出到下拉列表框


    【重要】不要边看文本边操作,本文有错误纠正,先看完一遍再说。

    要使用的FFmpeg命令

    image

    ffmpeg -list_devices true -f dshow -i dummy

    会输出的信息

    image

    image

    通过正则取出设备名称

    1. List<string> videoList=new List<string>();
    2. foreach (Match item in Regex.Matches(this.info, "] \"(.*?)\" \\(video\\)"))
    3. videoList.Add(item.Value.Replace("]\"","").Replace("\"(video)", ""));
    4. List<string> audioList = new List<string>();
    5. foreach (Match item in Regex.Matches(this.info, "] \"(.*?)\" \\(audio\\)"))
    6. audioList.Add(item.Value.Replace("]\"", "").Replace("\"(audio)", ""));

    1.完全不了解正则表达式的去看看C# 正则表达式 | 菜鸟教程

    2.我本来想的是,加了括号(.*?),匹配出来的应该不包括 ]""(video)

    但是它又确实包括了,于是我只能使用替换的方式将我不要的内容给替换掉

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Text.RegularExpressions;
    7. using System.Threading.Tasks;
    8. namespace why_not_fly
    9. {
    10. internal class MediaInfo
    11. {
    12. public string info = null;
    13. public string error = null;
    14. public bool isFinish = false;
    15. Media media = new Media();
    16. public Liststring>> getDevices() {
    17. List<string> list = new List<string>() { "-list_devices true -f dshow -i dummy" };
    18. media.Start(list);
    19. media.process.ErrorDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);
    20. while (!isFinish)
    21. {
    22. //等待完成
    23. }
    24. MessageBox.Show(info);
    25. List<string> videoList=new List<string>();
    26. foreach (Match item in Regex.Matches(this.info, "] \"(.*?)\" \\(video\\)"))
    27. videoList.Add(item.Value.Replace("] \"","").Replace("\" (video)", ""));
    28. List<string> audioList = new List<string>();
    29. foreach (Match item in Regex.Matches(this.info, "] \"(.*?)\" \\(audio\\)"))
    30. audioList.Add(item.Value.Replace("] \"", "").Replace("\" (audio)", ""));
    31. return new Liststring>>() { videoList, audioList };
    32. }
    33. //设置回调,读取指令的返回值
    34. private void ProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    35. {
    36. try
    37. {
    38. //进程间通信,解决线程中调用控件错误
    39. Control.CheckForIllegalCrossThreadCalls = false;
    40. if (!String.IsNullOrEmpty(outLine.Data))
    41. {
    42. info += outLine.Data + Environment.NewLine;
    43. }
    44. else
    45. {
    46. isFinish = true;
    47. }
    48. }
    49. catch (Exception ex)
    50. {
    51. error = ex.ToString();
    52. }
    53. }
    54. }
    55. }

    3.注意符号之间的空格

    如下图,是有空格的

    image

    你要是空格没写或者写的不对,就取不出来了。

    然后准备两个下拉列表框,载入窗口的时候就读取设备信息

    image

    image

    读取完成!

    难点的话其实也就是正则表达式提取内容,只要内容能提取出来,那一切都比较好办了。

    然后关于读取设备遇到乱码问题,请看【C#】【ffmpeg】外部调用线程执行ffmepg读取返回的信息乱码问题

    我估计可能获取的这些信息还不够,后面再更新。

  • 相关阅读:
    Maven 跳过测试的几种方式
    GitHub Copilot Chat
    【C++杂货铺】探索list的底层实现
    计算机视觉实战项目(图像分类+目标检测+目标跟踪+姿态识别+车道线识别+车牌识别)
    【算法小记】接雨水的不同解法
    Java面向对象进阶7——代码块
    ICDE‘22推荐系统论文梳理之Industry篇
    基于SpringBoot的协同过滤算法商品推荐系统
    vue中引入高德地图Loca数据可视化
    【软件与系统安全笔记】三、基础技术
  • 原文地址:https://blog.csdn.net/ks2686/article/details/127388878