要使用的FFmpeg命令
ffmpeg -list_devices true -f dshow -i dummy
会输出的信息
通过正则取出设备名称
List<string> videoList=new List<string>();
foreach (Match item in Regex.Matches(this.info, "]\"(.*?)\"\\(video\\)"))
videoList.Add(item.Value.Replace("]\"","").Replace("\"(video)", ""));
List<string> audioList = new List<string>();
foreach (Match item in Regex.Matches(this.info, "]\"(.*?)\"\\(audio\\)"))
audioList.Add(item.Value.Replace("]\"", "").Replace("\"(audio)", ""));
1.完全不了解正则表达式的去看看https://www.runoob.com/csharp/csharp-regular-expressions.html
2.我本来想的是,加了括号(.*?),匹配出来的应该不包括 ]"
和 "(video)
但是它又确实包括了,于是我只能使用替换的方式将我不要的内容给替换掉
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace why_not_fly
{
internal class MediaInfo
{
public string info = null;
public string error = null;
public bool isFinish = false;
Media media = new Media();
public List<List<string>> getDevices() {
List<string> list = new List<string>() { "-list_devices true -f dshow -i dummy" };
media.Start(list);
media.process.ErrorDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);
while (!isFinish)
{
//等待完成
}
//处理文本
info = info.Replace(" ", "");
MessageBox.Show(info);
List<string> videoList=new List<string>();
foreach (Match item in Regex.Matches(this.info, "]\"(.*?)\"\\(video\\)"))
videoList.Add(item.Value.Replace("]\"","").Replace("\"(video)", ""));
List<string> audioList = new List<string>();
foreach (Match item in Regex.Matches(this.info, "]\"(.*?)\"\\(audio\\)"))
audioList.Add(item.Value.Replace("]\"", "").Replace("\"(audio)", ""));
return new List<List<string>>() { videoList, audioList };
}
//设置回调,读取指令的返回值
private void ProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
try
{
//进程间通信,解决线程中调用控件错误
Control.CheckForIllegalCrossThreadCalls = false;
if (!String.IsNullOrEmpty(outLine.Data))
{
info += outLine.Data + Environment.NewLine;
}
else
{
isFinish = true;
}
}
catch (Exception ex)
{
error = ex.ToString();
}
}
}
}
然后准备两个下拉列表框,载入窗口的时候就读取设备信息
读取完成!
难点的话其实也就是正则表达式提取内容,只要内容能提取出来,那一切都比较好办了。
然后关于读取设备遇到乱码问题,请看【C#】【ffmpeg】外部调用线程执行ffmepg读取返回的信息乱码问题
我估计可能获取的这些信息还不够,后面再更新。
标签:info,列表框,FFmpeg,List,Replace,item,using,new,音视频 From: https://www.cnblogs.com/mllt/p/CSHAPE_FFMPEG_DEVICES.html