1
<
appSettings
>
2
3 < add key ="ffmpeg" value ="ffmpeg/ffmpeg.exe" />
4 < add key ="mencoder" value ="mencoder/mencoder.exe" />
5 < add key ="mplayer" value ="mencoder/mplayer.exe" />
6
7 < add key ="upfile" value ="UpFiles" />
8
9 < add key ="imgfile" value ="ImgFile" />
10
11 < add key ="CatchFlvImgSize" value ="240x180" />
12 < add key ="widthSize" value ="400" />
13 < add key ="heightSize" value ="350" />
14
15 < add key ="playfile" value ="PlayFiles" />
16 appSettings>
2
3 < add key ="ffmpeg" value ="ffmpeg/ffmpeg.exe" />
4 < add key ="mencoder" value ="mencoder/mencoder.exe" />
5 < add key ="mplayer" value ="mencoder/mplayer.exe" />
6
7 < add key ="upfile" value ="UpFiles" />
8
9 < add key ="imgfile" value ="ImgFile" />
10
11 < add key ="CatchFlvImgSize" value ="240x180" />
12 < add key ="widthSize" value ="400" />
13 < add key ="heightSize" value ="350" />
14
15 < add key ="playfile" value ="PlayFiles" />
16 appSettings>
上传类:
1
namespace
VideoToFLV
2 {
3 public class PublicMethod:System.Web.UI.Page
4 {
5 public PublicMethod()
6 {
7
8 }
9 // 文件路径
10 public static string ffmpegtool = ConfigurationManager.AppSettings[ " ffmpeg " ];
11 public static string mencodertool = ConfigurationManager.AppSettings[ " mencoder " ];
12 public static string mplayertool = ConfigurationManager.AppSettings[ " mplayer " ];
13 public static string upFile = ConfigurationManager.AppSettings[ " upfile " ] + " / " ;
14 public static string imgFile = ConfigurationManager.AppSettings[ " imgfile " ] + " / " ;
15 public static string playFile = ConfigurationManager.AppSettings[ " playfile " ] + " / " ;
16 // 文件图片大小
17 public static string sizeOfImg = ConfigurationManager.AppSettings[ " CatchFlvImgSize " ];
18 // 文件大小
19 public static string widthOfFile = ConfigurationManager.AppSettings[ " widthSize " ];
20 public static string heightOfFile = ConfigurationManager.AppSettings[ " heightSize " ];
21 //
22 //
23 // 获取文件的名字
24 public static string GetFileName( string fileName)
25 {
26 int i = fileName.LastIndexOf( " \\ " ) + 1 ;
27 string Name = fileName.Substring(i);
28 return Name;
29 }
30 // 获取文件扩展名
31 public static string GetExtension( string fileName)
32 {
33 int i = fileName.LastIndexOf( " . " ) + 1 ;
34 string Name = fileName.Substring(i);
35 return Name;
36 }
37 //
38
39 #region
40 // 运行FFMpeg的视频解码,(这里是绝对路径)
41 ///
42
43 /// 转换文件并保存在指定文件夹下面(这里是绝对路径)
44 ///
45
46 /// 上传视频文件的路径(原文件)
47 /// 转换后的文件的路径(网络播放文件)
48 /// 从视频文件中抓取的图片路径
49 /// 成功:返回图片虚拟地址; 失败:返回空字符串
50 public string ChangeFilePhy( string fileName, string playFile, string imgFile)
51 {
52 // 取得ffmpeg.exe的路径,路径配置在Web.Config中,如:
53 string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
54 if (( ! System.IO.File.Exists(ffmpeg)) || ( ! System.IO.File.Exists(fileName)))
55 {
56 return "" ;
57 }
58
59 // 获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/Web/User1/00001.jpg
60
61 string flv_file = System.IO.Path.ChangeExtension(playFile, " .flv " );
62
63
64 // 截图的尺寸大小,配置在Web.Config中,如:
65 string FlvImgSize = PublicMethod.sizeOfImg;
66
67 System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
68
69 FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
70
71 FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + " x " + heightOfFile + " " + flv_file;
72 // ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.05 -s " + FlvImgSize + " " + flv_img;
73
74 try
75 {
76 // 转换
77 System.Diagnostics.Process.Start(FilestartInfo);
78 // 截图
79 CatchImg(fileName, imgFile);
80 // System.Diagnostics.Process.Start(ImgstartInfo);
81 }
82 catch
83 {
84 return "" ;
85 }
86 //
87 return "" ;
88 }
89 //
90 public string CatchImg( string fileName, string imgFile)
91 {
92 //
93 string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
94 //
95 string flv_img = imgFile + " .jpg " ;
96 //
97 string FlvImgSize = PublicMethod.sizeOfImg;
98 //
99 System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
100 ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
101 //
102 ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img;
103 try
104 {
105 System.Diagnostics.Process.Start(ImgstartInfo);
106 }
107 catch
108 {
109 return "" ;
110 }
111 //
112 if (System.IO.File.Exists(flv_img))
113 {
114 return flv_img;
115 }
116
117 return "" ;
118 }
119 #endregion
120 //
121 #region
122 // 运行FFMpeg的视频解码,(这里是(虚拟)相对路径)
123 ///
124
125 /// 转换文件并保存在指定文件夹下面(这里是相对路径)
126 ///
127
128 /// 上传视频文件的路径(原文件)
129 /// 转换后的文件的路径(网络播放文件)
130 /// 从视频文件中抓取的图片路径
131 /// 成功:返回图片虚拟地址; 失败:返回空字符串
132 public string ChangeFileVir( string fileName, string playFile, string imgFile)
133 {
134 // 取得ffmpeg.exe的路径,路径配置在Web.Config中,如:
135 string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
136 if (( ! System.IO.File.Exists(ffmpeg)) || ( ! System.IO.File.Exists(fileName)))
137 {
138 return "" ;
139 }
140
141 // 获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/Web/User1/00001.jpg
142 string flv_img = System.IO.Path.ChangeExtension(Server.MapPath(imgFile), " .jpg " );
143 string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), " .flv " );
144
145
146 // 截图的尺寸大小,配置在Web.Config中,如:
147 string FlvImgSize = PublicMethod.sizeOfImg;
148
149 System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
150 System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
151
152 FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
153 ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
154 // 此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
155 // ffmpeg -i F:\01.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv
156 FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + " x " + heightOfFile + " " + flv_file;
157 ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img;
158
159 try
160 {
161 System.Diagnostics.Process.Start(FilestartInfo);
162 System.Diagnostics.Process.Start(ImgstartInfo);
163 }
164 catch
165 {
166 return "" ;
167 }
168
169 /// 注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
170 /// 这儿需要延时后再检测,我服务器延时8秒,即如果超过8秒图片仍不存在,认为截图失败;
171 /// 此处略去延时代码.如有那位知道如何捕捉ffmpeg.exe截图失败消息,请告知,先谢过!
172 if (System.IO.File.Exists(flv_img))
173 {
174 return flv_img;
175 }
176
177 return "" ;
178 }
179 #endregion
180
181 #region
182 // 运行mencoder的视频解码器转换(这里是(绝对路径))
183 public string MChangeFilePhy( string vFileName, string playFile, string imgFile)
184 {
185 string tool = Server.MapPath(PublicMethod.mencodertool);
186 // string mplaytool = Server.MapPath(PublicMethod.ffmpegtool);
187
188 if (( ! System.IO.File.Exists(tool)) || ( ! System.IO.File.Exists(vFileName)))
189 {
190 return "" ;
191 }
192
193 string flv_file = System.IO.Path.ChangeExtension(playFile, " .flv " );
194
195
196 // 截图的尺寸大小,配置在Web.Config中,如:
197 string FlvImgSize = PublicMethod.sizeOfImg;
198
199 System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(tool);
200
201 FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
202 FilestartInfo.Arguments = " " + vFileName + " -o " + flv_file + " -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=200:mbd=2:mv0:trell:v4mv:cbp:last_pred=1:dia=-1:cmp=0:vb_strategy=1 -vf scale= " + widthOfFile + " : " + heightOfFile + " -ofps 12 -srate 22050 " ;
203 try
204 {
205 System.Diagnostics.Process.Start(FilestartInfo);
206 CatchImg(flv_file, imgFile);
207 }
208 catch
209 {
210 return "" ;
211 }
212 //
213 return "" ;
214 }
215 #endregion
216
217 }
218 }
2 {
3 public class PublicMethod:System.Web.UI.Page
4 {
5 public PublicMethod()
6 {
7
8 }
9 // 文件路径
10 public static string ffmpegtool = ConfigurationManager.AppSettings[ " ffmpeg " ];
11 public static string mencodertool = ConfigurationManager.AppSettings[ " mencoder " ];
12 public static string mplayertool = ConfigurationManager.AppSettings[ " mplayer " ];
13 public static string upFile = ConfigurationManager.AppSettings[ " upfile " ] + " / " ;
14 public static string imgFile = ConfigurationManager.AppSettings[ " imgfile " ] + " / " ;
15 public static string playFile = ConfigurationManager.AppSettings[ " playfile " ] + " / " ;
16 // 文件图片大小
17 public static string sizeOfImg = ConfigurationManager.AppSettings[ " CatchFlvImgSize " ];
18 // 文件大小
19 public static string widthOfFile = ConfigurationManager.AppSettings[ " widthSize " ];
20 public static string heightOfFile = ConfigurationManager.AppSettings[ " heightSize " ];
21 //
22 //
23 // 获取文件的名字
24 public static string GetFileName( string fileName)
25 {
26 int i = fileName.LastIndexOf( " \\ " ) + 1 ;
27 string Name = fileName.Substring(i);
28 return Name;
29 }
30 // 获取文件扩展名
31 public static string GetExtension( string fileName)
32 {
33 int i = fileName.LastIndexOf( " . " ) + 1 ;
34 string Name = fileName.Substring(i);
35 return Name;
36 }
37 //
38
39 #region
40 // 运行FFMpeg的视频解码,(这里是绝对路径)
41 ///
42
43 /// 转换文件并保存在指定文件夹下面(这里是绝对路径)
44 ///
45
46 /// 上传视频文件的路径(原文件)
47 /// 转换后的文件的路径(网络播放文件)
48 /// 从视频文件中抓取的图片路径
49 /// 成功:返回图片虚拟地址; 失败:返回空字符串
50 public string ChangeFilePhy( string fileName, string playFile, string imgFile)
51 {
52 // 取得ffmpeg.exe的路径,路径配置在Web.Config中,如:
53 string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
54 if (( ! System.IO.File.Exists(ffmpeg)) || ( ! System.IO.File.Exists(fileName)))
55 {
56 return "" ;
57 }
58
59 // 获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/Web/User1/00001.jpg
60
61 string flv_file = System.IO.Path.ChangeExtension(playFile, " .flv " );
62
63
64 // 截图的尺寸大小,配置在Web.Config中,如:
65 string FlvImgSize = PublicMethod.sizeOfImg;
66
67 System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
68
69 FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
70
71 FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + " x " + heightOfFile + " " + flv_file;
72 // ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.05 -s " + FlvImgSize + " " + flv_img;
73
74 try
75 {
76 // 转换
77 System.Diagnostics.Process.Start(FilestartInfo);
78 // 截图
79 CatchImg(fileName, imgFile);
80 // System.Diagnostics.Process.Start(ImgstartInfo);
81 }
82 catch
83 {
84 return "" ;
85 }
86 //
87 return "" ;
88 }
89 //
90 public string CatchImg( string fileName, string imgFile)
91 {
92 //
93 string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
94 //
95 string flv_img = imgFile + " .jpg " ;
96 //
97 string FlvImgSize = PublicMethod.sizeOfImg;
98 //
99 System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
100 ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
101 //
102 ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img;
103 try
104 {
105 System.Diagnostics.Process.Start(ImgstartInfo);
106 }
107 catch
108 {
109 return "" ;
110 }
111 //
112 if (System.IO.File.Exists(flv_img))
113 {
114 return flv_img;
115 }
116
117 return "" ;
118 }
119 #endregion
120 //
121 #region
122 // 运行FFMpeg的视频解码,(这里是(虚拟)相对路径)
123 ///
124
125 /// 转换文件并保存在指定文件夹下面(这里是相对路径)
126 ///
127
128 /// 上传视频文件的路径(原文件)
129 /// 转换后的文件的路径(网络播放文件)
130 /// 从视频文件中抓取的图片路径
131 /// 成功:返回图片虚拟地址; 失败:返回空字符串
132 public string ChangeFileVir( string fileName, string playFile, string imgFile)
133 {
134 // 取得ffmpeg.exe的路径,路径配置在Web.Config中,如:
135 string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
136 if (( ! System.IO.File.Exists(ffmpeg)) || ( ! System.IO.File.Exists(fileName)))
137 {
138 return "" ;
139 }
140
141 // 获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/Web/User1/00001.jpg
142 string flv_img = System.IO.Path.ChangeExtension(Server.MapPath(imgFile), " .jpg " );
143 string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), " .flv " );
144
145
146 // 截图的尺寸大小,配置在Web.Config中,如:
147 string FlvImgSize = PublicMethod.sizeOfImg;
148
149 System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
150 System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
151
152 FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
153 ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
154 // 此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
155 // ffmpeg -i F:\01.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv
156 FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + " x " + heightOfFile + " " + flv_file;
157 ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img;
158
159 try
160 {
161 System.Diagnostics.Process.Start(FilestartInfo);
162 System.Diagnostics.Process.Start(ImgstartInfo);
163 }
164 catch
165 {
166 return "" ;
167 }
168
169 /// 注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
170 /// 这儿需要延时后再检测,我服务器延时8秒,即如果超过8秒图片仍不存在,认为截图失败;
171 /// 此处略去延时代码.如有那位知道如何捕捉ffmpeg.exe截图失败消息,请告知,先谢过!
172 if (System.IO.File.Exists(flv_img))
173 {
174 return flv_img;
175 }
176
177 return "" ;
178 }
179 #endregion
180
181 #region
182 // 运行mencoder的视频解码器转换(这里是(绝对路径))
183 public string MChangeFilePhy( string vFileName, string playFile, string imgFile)
184 {
185 string tool = Server.MapPath(PublicMethod.mencodertool);
186 // string mplaytool = Server.MapPath(PublicMethod.ffmpegtool);
187
188 if (( ! System.IO.File.Exists(tool)) || ( ! System.IO.File.Exists(vFileName)))
189 {
190 return "" ;
191 }
192
193 string flv_file = System.IO.Path.ChangeExtension(playFile, " .flv " );
194
195
196 // 截图的尺寸大小,配置在Web.Config中,如:
197 string FlvImgSize = PublicMethod.sizeOfImg;
198
199 System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(tool);
200
201 FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
202 FilestartInfo.Arguments = " " + vFileName + " -o " + flv_file + " -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=200:mbd=2:mv0:trell:v4mv:cbp:last_pred=1:dia=-1:cmp=0:vb_strategy=1 -vf scale= " + widthOfFile + " : " + heightOfFile + " -ofps 12 -srate 22050 " ;
203 try
204 {
205 System.Diagnostics.Process.Start(FilestartInfo);
206 CatchImg(flv_file, imgFile);
207 }
208 catch
209 {
210 return "" ;
211 }
212 //
213 return "" ;
214 }
215 #endregion
216
217 }
218 }