视频文件信息读取器
最近想要整理mtv,特意写了这个读取多种视频文件信息的辅助工具,包括mpg,avi,wmv,flv,rm,rmvb等。
原理:我只是做了个壳,实质是用ffmpeg.exe读取视频信息。
public class cVideoInfo
{
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;

public cVideoInfo(string FileName)
{
_FileName = FileName;
_Info = "";
_Name = FileName.Substring(FileName.LastIndexOf("\\") + 1, FileName.LastIndexOf(".") - FileName.LastIndexOf("\\") - 1);
_Ext = FileName.Substring(FileName.LastIndexOf(".") + 1);
_Duration = "";
_BitRate = "";
_Size = "";
_FileSize = "";
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();//释放资源
}
public string FileName
{
get
{
return _FileName;
}
}
public string Info
{
get
{
return _Info;
}
}
public string Name
{
get
{
return _Name;
}
}
public string Ext
{
get
{
return _Ext;
}
}
public string Duration
{
get
{
return _Duration;
}
}
public string BitRate
{
get
{
return _BitRate;
}
}
public string Size
{
get
{
return _Size;
}
}
public string FileSize
{
get
{
return _FileSize;
}
}
private void CatchOutputInfo(object sendProcess, DataReceivedEventArgs OutputInfo)
{
string sOutputInfo = OutputInfo.Data;
int iStart = 0, iEnd = 0;
if (!String.IsNullOrEmpty(sOutputInfo))
{
_Info = _Info + "\r\n" + sOutputInfo;

//Duration ,BitRate
if (sOutputInfo.IndexOf("Duration") >= 0)
{
iStart = sOutputInfo.IndexOf(":") + 1;
iEnd = sOutputInfo.IndexOf(".", iStart);
_Duration = sOutputInfo.Substring(iStart, iEnd - iStart);
iStart = sOutputInfo.IndexOf("bitrate") + 8;
_BitRate = sOutputInfo.Substring(iStart);
}
//Size
if (sOutputInfo.IndexOf("Video") >= 0)
{
if (_Ext.Substring(0,2).ToLower() == "rm")
{
iStart = sOutputInfo.IndexOf(",") + 1;
iEnd = sOutputInfo.IndexOf(",", iStart);
}
else
{
iStart = sOutputInfo.IndexOf(",") + 1;
iStart = sOutputInfo.IndexOf(",", iStart) + 1;
iEnd = sOutputInfo.IndexOf(",", iStart);
}
_Size = sOutputInfo.Substring(iStart, iEnd - iStart);
}
}
}
}
浙公网安备 33010602011771号