C#从视频截图的方法

来源:互联网 发布:stm8s软件复位代码 编辑:程序博客网 时间:2024/05/18 17:43

下边是截图CatchImg方法,可从大多数的视频文件中截图成功,大家可测试;
如果截图不成功,大多是因为视频本身的问题,如编码标准或加了密.
但从在线录制的视频Flv文件中截图,还未发现截图失败;

/// <summary>
/// @从视频文件截图,生成在视频文件所在文件夹
/// 在Web.Config 中需要两个前置配置项:
/// 1.ffmpeg.exe文件的路径
/// <add key="ffmpeg" value="E:\ffmpeg\ffmpeg.exe" />
/// 2.截图的尺寸大小
/// <add key="CatchFlvImgSize" value="240x180" />
/// 3.视频处理程序ffmpeg.exe
/// </summary>
/// <param name="vFileName">视频文件地址,如:/Web/FlvFile/User1/00001.Flv</param>
/// <returns>成功:返回图片虚拟地址; 失败:返回空字符串</returns>
public string CatchImg(string vFileName)
{
//取得ffmpeg.exe的路径,路径配置在Web.Config中,如:<add key="ffmpeg" value="E:\ffmpeg\ffmpeg.exe" />
string ffmpeg=System.Configuration.ConfigurationSettings.AppSettings["ffmpeg"];

if ( (!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(vFileName)) )
{
return "";
}

//获得图片相对路径/最后存储到数据库的路径,如:/Web/FlvFile/User1/00001.jpg
string flv_img = System.IO.Path.ChangeExtension(vFileName,".jpg") ;

//图片绝对路径,如:D:\Video\Web\FlvFile\User1\0001.jpg
string flv_img_p = HttpContext.Current.Server.MapPath(flv_img);

//截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="240x180" />
string FlvImgSize=System.Configuration.ConfigurationSettings.AppSettings["CatchFlvImgSize"];

System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

//此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
startInfo.Arguments = " -i " + vFileName + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img_p ;

try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{
return "";
}

///注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
///这儿需要延时后再检测,我服务器延时8秒,即如果超过8秒图片仍不存在,认为截图失败;
///此处略去延时代码.如有那位知道如何捕捉ffmpeg.exe截图失败消息,请告知,先谢过!
if ( System.IO.File.Exists(flv_img_p))
{
return flv_img;
}

return "";
}

待解决问题:
就是我无法从ffmpeg.exe捕捉截图失败消息~
不知能看到这篇日志的行家可否有办法取得,我目前只能通过检测图片是否生成来判断成功与否,但时间较慢,因为这个检测程序就让用户要多等大概4,5秒时间. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuartzTypeLib;

private void button1_Click(object sender, EventArgs e)
{
FilgraphManager m_FilGraphManager = null;
IBasicAudio m_BasicAudio = null;
IVideoWindow m_VideoWindow = null;
IMediaEvent m_MediaEvent = null;
IMediaEventEx m_MediaEventEx = null;
IMediaPosition m_MediaPosition = null;
IMediaControl m_MediaControl = null;
OpenFileDialog OpenDialog = new OpenFileDialog();
OpenDialog.Filter = "Media Files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|All Files|*.*";//本例用对话框读入要显示的影片文件名
if (DialogResult.OK == OpenDialog.ShowDialog())
{
m_FilGraphManager = new FilgraphManager();
m_FilGraphManager.RenderFile(OpenDialog.FileName);
m_BasicAudio = m_FilGraphManager as IBasicAudio;
try
{
m_VideoWindow = m_FilGraphManager as IVideoWindow;
m_VideoWindow.Owner = (int)panel1.Handle;
//m_VideoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
m_VideoWindow.WindowStyle = 1;
//此设置可以不显示播放器的title,使播放器像嵌在窗体中。
//可设置 private const int WS_CHILD = 0x40000000;
// private const int WS_CLIPCHILDREN = 0x2000000;
m_VideoWindow.SetWindowPosition(0,0,
panel1.ClientRectangle.Width,
panel1.ClientRectangle.Height);
// 在panel1中显示,要求影片可随panel1大小而变化。
}
catch (Exception)
{
m_VideoWindow = null;
}

m_MediaEvent = m_FilGraphManager as IMediaEvent;
m_MediaEventEx = m_FilGraphManager as IMediaEventEx;
//m_MediaEventEx.SetNotifyWindow((int)this.Handle, WM_GRAPHNOTIFY, 0);
m_MediaEventEx.SetNotifyWindow((int)this.Handle, 1, 0);
m_MediaPosition = m_FilGraphManager as IMediaPosition;
m_MediaControl = m_FilGraphManager as IMediaControl;
this.Text = "DirectShow - [" + OpenDialog.FileName + "]";
m_MediaControl.Run();
}
}  

转自:http://www.cnblogs.com/yao/articles/409228.html

原创粉丝点击