前段时间写过一篇抓取视频文件中图片的帖子,如下痛并努力着-在asp.net中如何从视频文件中抓取一桢并生成图像文件
另外一种方法是采用ffmpeg解码器,来实现如功能。
代码如下:
另外一种方法是采用ffmpeg解码器,来实现如功能。
代码如下:
1
public class CatchFlv
2
{
3
public CatchFlv()
4
{
5
}
6
/// <summary>
7
/// @从视频文件截图,生成在视频文件所在文件夹
8
/// 支持文件格式:asf,avi,mpg,flv,3gp,mov,wmv
9
/// 在Web.Config 中需要两个前置配置项:
10
/// 1.ffmpeg.exe文件的路径
11
/// <add key="ffmpeg" value="\thread\ffmpeg\ffmpeg.exe" />
12
/// 2.截图的尺寸大小
13
/// <add key="CatchFlvImgSize" value="140x110" />
14
/// 3.视频处理程序ffmpeg.exe
15
/// </summary>
16
/// <param name="vFileName">视频文件绝对或相对地址,如:(..)/Web/FlvFile/User1/001.avi</param>
17
/// <returns>成功:返回图片绝对/相对地址; 失败:返回空字符串</returns>
18
public static string CatchImg(string vFileName)
19
{
20
try
21
{
22
string ffmpeg = System.Configuration.ConfigurationSettings.AppSettings["ffmpeg"];
23
ffmpeg = HttpContext.Current.Server.MapPath(ffmpeg);
24
25
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(vFileName))))
26
{
27
return "";
28
}
29
30
//获得图片相对路径/最后存储到数据库的路径,如:/Web/FlvFile/User1/00001.jpg
31
string flv_img = System.IO.Path.ChangeExtension(vFileName, ".jpg");
32
33
//图片绝对路径,如:D:\Video\Web\FlvFile\User1\0001.jpg
34
string flv_img_p = HttpContext.Current.Server.MapPath(flv_img);
35
36
//截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="140x110" />
37
string FlvImgSize = System.Configuration.ConfigurationSettings.AppSettings["CatchFlvImgSize"];
38
39
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
40
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
41
42
//此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
43
startInfo.Arguments = " -i " + System.Web.HttpContext.Current.Server.MapPath(vFileName) + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img_p;
44
try
45
{
46
System.Diagnostics.Process.Start(startInfo);
47
}
48
catch
49
{
50
return "";
51
}
52
System.Threading.Thread.Sleep(4000);
53
///注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
54
if (System.IO.File.Exists(flv_img_p))
55
{
56
return flv_img;
57
}
58
return "";
59
}
60
catch
61
{
62
return "";
63
}
64
65
}
66
67
/// <summary>
68
/// @视频文件格式转换,生成在视频文件所在文件夹
69
/// 支持文件格式:asf,avi,mpg,flv,3gp,mov,wmv
70
/// 在Web.Config 中需要两个前置配置项:
71
/// 1.ffmpeg.exe文件的路径
72
/// <add key="ffmpeg" value="\thread\ffmpeg\ffmpeg.exe" />
73
/// 2.截图的尺寸大小
74
/// <add key="CatchFlvSize" value="240x180" />
75
/// 3.视频处理程序ffmpeg.exe
76
/// </summary>
77
/// <param name="vFileName">视频文件绝对或相对地址,如:(..)/Web/FlvFile/User1/001.avi</param>
78
/// <returns>成功:返回flv文件绝对/相对地址; 失败:返回空字符串</returns>
79
public static string ChangeMediaFormat(string vFileName)
80
{
81
try
82
{
83
string ffmpeg = System.Configuration.ConfigurationSettings.AppSettings["ffmpeg"];
84
string wks_url = System.Configuration.ConfigurationSettings.AppSettings["wks_local"];
85
ffmpeg = wks_url+ffmpeg;
86
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(wks_url+vFileName)))
87
{
88
return "";
89
}
90
91
string flv_fot = System.IO.Path.ChangeExtension(vFileName, ".flv");
92
string flv_fot_p = wks_url+flv_fot;
93
94
//尺寸大小,配置在Web.Config中,如:<add key="CatchFlvSize" value="240x180" />
95
string FlvImgSize = System.Configuration.ConfigurationSettings.AppSettings["CatchFlvSize"];
96
97
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
98
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
99
100
//此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
101
startInfo.Arguments = " -i " + wks_url+vFileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + FlvImgSize + " " + flv_fot_p;
102
103
try
104
{
105
System.Diagnostics.Process.Start(startInfo);
106
}
107
catch
108
{
109
return "";
110
}
111
112
///注意:文件格式转换成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
113
System.Threading.Thread.Sleep(6000);
114
if (System.IO.File.Exists(flv_fot_p))
115
{
116
WKS.Common.ImagesSet.DeleteOleImg(wks_url+vFileName);
117
return flv_fot;
118
}
119
120
return "";
121
}
122
catch
123
{
124
return "";
125
}
126
}
127
}
128
但是,这种方法不知道ffmpeg什么时候生成结果,生成什么样的结果,因此也存在一定局限性.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128