本文实例为大家分享了C#视频转换类的具体代码,供大家参考,具体内容如下
VideoConvert
1
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
using System.Web; using System.Configuration; namespace DotNet.Utilities { public class VideoConvert : System.Web.UI.Page { public VideoConvert() { } string [] strArrMencoder = new string [] { "wmv" , "rmvb" , "rm" }; string [] strArrFfmpeg = new string [] { "asf" , "avi" , "mpg" , "3gp" , "mov" }; #region 配置 public static string ffmpegtool = ConfigurationManager.AppSettings[ "ffmpeg" ]; public static string mencodertool = ConfigurationManager.AppSettings[ "mencoder" ]; public static string savefile = ConfigurationManager.AppSettings[ "savefile" ] + "/" ; public static string sizeOfImg = ConfigurationManager.AppSettings[ "CatchFlvImgSize" ]; public static string widthOfFile = ConfigurationManager.AppSettings[ "widthSize" ]; public static string heightOfFile = ConfigurationManager.AppSettings[ "heightSize" ]; #endregion #region 获取文件的名字 /// <summary> /// 获取文件的名字 /// </summary> public static string GetFileName( string fileName) { int i = fileName.LastIndexOf( "\\" ) + 1; string Name = fileName.Substring(i); return Name; } #endregion #region 获取文件扩展名 /// <summary> /// 获取文件扩展名 /// </summary> public static string GetExtension( string fileName) { int i = fileName.LastIndexOf( "." ) + 1; string Name = fileName.Substring(i); return Name; } #endregion #region 获取文件类型 /// <summary> /// 获取文件类型 /// </summary> public string CheckExtension( string extension) { string m_strReturn = "" ; foreach ( string var in this .strArrFfmpeg) { if (var == extension) { m_strReturn = "ffmpeg" ; break ; } } if (m_strReturn == "" ) { foreach ( string var in strArrMencoder) { if (var == extension) { m_strReturn = "mencoder" ; break ; } } } return m_strReturn; } #endregion #region 视频格式转为Flv /// <summary> /// 视频格式转为Flv /// </summary> /// <param name="vFileName">原视频文件地址</param> /// <param name="ExportName">生成后的Flv文件地址</param> public bool ConvertFlv( string vFileName, string ExportName) { if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName)))) { return false ; } vFileName = HttpContext.Current.Server.MapPath(vFileName); ExportName = HttpContext.Current.Server.MapPath(ExportName); string Command = " -i \"" + vFileName + "\" -y -ab 32 -ar 22050 -b 800000 -s 480*360 \"" + ExportName + "\"" ; //Flv格式 System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = ffmpegtool; p.StartInfo.Arguments = Command; p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath( "~/tools/" ); p.StartInfo.UseShellExecute = false ; p.StartInfo.RedirectStandardInput = true ; p.StartInfo.RedirectStandardOutput = true ; p.StartInfo.RedirectStandardError = true ; p.StartInfo.CreateNoWindow = false ; p.Start(); p.BeginErrorReadLine(); p.WaitForExit(); p.Close(); p.Dispose(); return true ; } #endregion #region 生成Flv视频的缩略图 /// <summary> /// 生成Flv视频的缩略图 /// </summary> /// <param name="vFileName">视频文件地址</param> public string CatchImg( string vFileName) { if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName)))) return "" ; try { string flv_img_p = vFileName.Substring(0, vFileName.Length - 4) + ".jpg" ; string Command = " -i " + HttpContext.Current.Server.MapPath(vFileName) + " -y -f image2 -t 0.1 -s " + sizeOfImg + " " + HttpContext.Current.Server.MapPath(flv_img_p); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = ffmpegtool; p.StartInfo.Arguments = Command; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; try { p.Start(); } catch { return "" ; } finally { p.Close(); p.Dispose(); } System.Threading.Thread.Sleep(4000); //注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长; if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(flv_img_p))) { return flv_img_p; } return "" ; } catch { return "" ; } } #endregion #region 运行FFMpeg的视频解码(绝对路径) /// <summary> /// 转换文件并保存在指定文件夹下 /// </summary> /// <param name="fileName">上传视频文件的路径(原文件)</param> /// <param name="playFile">转换后的文件的路径(网络播放文件)</param> /// <param name="imgFile">从视频文件中抓取的图片路径</param> /// <returns>成功:返回图片虚拟地址;失败:返回空字符串</returns> public string ChangeFilePhy( string fileName, string playFile, string imgFile) { string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool); if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName))) { return "" ; } string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv" ); string FlvImgSize = VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file; try { System.Diagnostics.Process.Start(FilestartInfo); //转换 CatchImg(fileName, imgFile); //截图 } catch { return "" ; } return "" ; } public string CatchImg( string fileName, string imgFile) { string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool); string flv_img = imgFile + ".jpg" ; string FlvImgSize = VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img; try { System.Diagnostics.Process.Start(ImgstartInfo); } catch { return "" ; } if (System.IO.File.Exists(flv_img)) { return flv_img; } return "" ; } #endregion #region 运行FFMpeg的视频解码(相对路径) /// <summary> /// 转换文件并保存在指定文件夹下 /// </summary> /// <param name="fileName">上传视频文件的路径(原文件)</param> /// <param name="playFile">转换后的文件的路径(网络播放文件)</param> /// <param name="imgFile">从视频文件中抓取的图片路径</param> /// <returns>成功:返回图片虚拟地址;失败:返回空字符串</returns> public string ChangeFileVir( string fileName, string playFile, string imgFile) { string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool); if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName))) { return "" ; } string flv_img = System.IO.Path.ChangeExtension(Server.MapPath(imgFile), ".jpg" ); string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), ".flv" ); string FlvImgSize = VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img; System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg); FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file; try { System.Diagnostics.Process.Start(FilestartInfo); System.Diagnostics.Process.Start(ImgstartInfo); } catch { return "" ; } ///注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长; ///这儿需要延时后再检测,我服务器延时8秒,即如果超过8秒图片仍不存在,认为截图失败; if (System.IO.File.Exists(flv_img)) { return flv_img; } return "" ; } #endregion #region 运行mencoder的视频解码器转换(绝对路径) /// <summary> /// 运行mencoder的视频解码器转换 /// </summary> public string MChangeFilePhy( string vFileName, string playFile, string imgFile) { string tool = Server.MapPath(VideoConvert.mencodertool); if ((!System.IO.File.Exists(tool)) || (!System.IO.File.Exists(vFileName))) { return "" ; } string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv" ); string FlvImgSize = VideoConvert.sizeOfImg; System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(tool); FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 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" ; try { System.Diagnostics.Process.Start(FilestartInfo); CatchImg(flv_file, imgFile); } catch { return "" ; } return "" ; } #endregion } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。