最近想要整理mtv,特意写了这个读取多种视频文件信息的辅助工具,包括mpg,avi,wmv,flv,rm,rmvb等。
原理:我只是做了个壳,实质是用ffmpeg.exe读取视频信息。
public class cVideoInfo
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string _FileName; //video file fullname
private string _Info; //all info
private string _Name; // vide name
private string _Ext; // file ext name
private string _Duration;
private string _BitRate;
private string _Size;
private string _FileSize;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public cVideoInfo(string FileName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_FileName = FileName;
_Info = "";
_Name = FileName.Substring(FileName.LastIndexOf("\\") + 1, FileName.LastIndexOf(".") - FileName.LastIndexOf("\\") - 1);
_Ext = FileName.Substring(FileName.LastIndexOf(".") + 1);
_Duration = "";
_BitRate = "";
_Size = "";
_FileSize = "";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FileInfo fi = new FileInfo(_FileName);
if (!fi.Exists)
return;
else
_FileSize = fi.Length.ToString();
//这段代码的参考网上的
Process p = new Process();//建立外部调用线程
p.StartInfo.FileName = "ffmpeg.exe";//要调用外部程序的绝对路径
p.StartInfo.Arguments = "-i \"" + _FileName + "\"";//参数(这里就是FFMPEG的参数了)
p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的
这是我耗费了2个多月得出来的经验
mencoder就是用standardOutput来捕获的)
p.StartInfo.CreateNoWindow = true;//不创建进程窗口
p.ErrorDataReceived += new DataReceivedEventHandler(CatchOutputInfo);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
p.Start();//启动线程
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string FileName
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _FileName;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Info
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _Info;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Name
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _Name;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Ext
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _Ext;
}
}
public string Duration
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _Duration;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string BitRate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _BitRate;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Size
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _Size;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string FileSize
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _FileSize;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void CatchOutputInfo(object sendProcess, DataReceivedEventArgs OutputInfo)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sOutputInfo = OutputInfo.Data;
int iStart = 0, iEnd = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!String.IsNullOrEmpty(sOutputInfo))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_Info = _Info + "\r\n" + sOutputInfo;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Duration ,BitRate
if (sOutputInfo.IndexOf("Duration") >= 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iStart = sOutputInfo.IndexOf(":") + 1;
iEnd = sOutputInfo.IndexOf(".", iStart);
_Duration = sOutputInfo.Substring(iStart, iEnd - iStart);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
iStart = sOutputInfo.IndexOf("bitrate") + 8;
_BitRate = sOutputInfo.Substring(iStart);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Size
if (sOutputInfo.IndexOf("Video") >= 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_Ext.Substring(0,2).ToLower() == "rm")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iStart = sOutputInfo.IndexOf(",") + 1;
iEnd = sOutputInfo.IndexOf(",", iStart);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iStart = sOutputInfo.IndexOf(",") + 1;
iStart = sOutputInfo.IndexOf(",", iStart) + 1;
iEnd = sOutputInfo.IndexOf(",", iStart);
}
_Size = sOutputInfo.Substring(iStart, iEnd - iStart);
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)