大道之行也,天下为公
Asp.net Ajax,Web Service ,PHP ,SQLServer,MySQL
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 65 文章- 1 评论- 211
视频格式转化为FLV (C#)
在上次随笔
Asp.net FMS 开发视频网站
中,好多朋友提到其他视频格式转化成FLV格式的问题,经过网上搜索资料研习整理,现经我的一点思路分享给大家:
我添加了一个转换FLV工程 VideoConvert:
1。配置文件里添加
web.config
1
<
appSettings
>
2
3
<!--
convert tools path
-->
4
<
add
key
="FfmpegPath"
value
="D:\tools\"
/>
5
6
<!--
setting
-->
7
<
add
key
="ThreadCount"
value
="5"
/>
8
<
add
key
="BatchSize"
value
="10"
/>
9
<
add
key
="QueueTimeout"
value
="20"
/>
10
<
add
key
="TransactionTimeout"
value
="30"
/>
11
</
appSettings
>
2。添加一个接口
IConvert
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
namespace
VideoConvert
6
{
7
public
interface
IConvert
8
{
9
10
/**/
///
<summary>
11
///
将视频文件转换为Flv格式
12
///
</summary>
13
///
<param name="sourceFile">
要转换的文件
</param>
14
///
<returns></returns>
15
bool
Convert(
string
sourceFile);
16
17
18
19
/**/
///
<summary>
20
///
获取缩略图
21
///
</summary>
22
///
<param name="sourceFile"></param>
23
///
<returns></returns>
24
bool
GetSmallImage(
string
sourceFile);
25
26
27
}
28
}
29
3。转换工具设定继承 IConvert
FfmpegConvert
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.Configuration;
5
6
7
namespace
VideoConvert
8
{
9
public
class
FfmpegConvert : IConvert
10
{
11
12
13
/**/
///
<summary>
14
///
转换软件所在的路径
15
///
</summary>
16
private
string
ConvertTool
=
ConfigurationManager.AppSettings[
"
FfmpegPath
"
]
+
"
ffmpeg.exe
"
;
17
18
19
/**/
///
<summary>
20
///
构造函数
21
///
</summary>
22
public
FfmpegConvert()
23
{
24
25
}
26
27
28
/**/
///
<summary>
29
///
将视频文件转换为Flv格式
30
///
</summary>
31
///
<param name="sourceFile">
要转换的文件
</param>
32
///
<returns></returns>
33
public
bool
Convert(
string
sourceFile)
34
{
35
try
36
{
37
//
文件名是否为空
38
if
(
string
.IsNullOrEmpty(sourceFile))
return
false
;
39
//
检测文件是否存在
40
41
42
string
TargetFile
=
sourceFile.Substring(
0
, sourceFile.Length
-
4
)
+
"
.flv
"
;
43
string
Argu
=
@"
-i
"
+
sourceFile
+
"
-ab 56 -ar 22050 -b 500 -r 15 -s 480x360
"
+
TargetFile;
44
45
System.Diagnostics.ProcessStartInfo startInfo
=
new
System.Diagnostics.ProcessStartInfo(ConvertTool, Argu);
46
System.Diagnostics.Process.Start(startInfo);
47
System.Threading.Thread.Sleep(
6000
);
48
return
true
;
49
50
}
51
catch
(Exception exp)
52
{
53
throw
exp;
54
}
55
56
}
57
58
59
/**/
///
<summary>
60
///
获取缩略图
61
///
</summary>
62
///
<param name="sourceFile"></param>
63
///
<returns></returns>
64
public
bool
GetSmallImage(
string
sourceFile)
65
{
66
67
//
文件名是否为空
68
if
(
string
.IsNullOrEmpty(sourceFile))
return
false
;
69
//
检测文件是否存在
70
71
try
72
{
73
string
TargetFile
=
sourceFile.Substring(
0
, sourceFile.Length
-
4
)
+
"
.jpg
"
;
74
string
Argu
=
@"
-i
"
+
sourceFile
+
"
-y -f image2 -ss 08.010 -t 0.001 -s 352x240
"
+
TargetFile;
75
System.Diagnostics.ProcessStartInfo startInfo
=
new
System.Diagnostics.ProcessStartInfo(ConvertTool, Argu);
76
System.Diagnostics.Process.Start(startInfo);
77
System.Threading.Thread.Sleep(
6000
);
78
79
80
//
必须等待进行完成后才能返回结果
81
82
83
return
true
;
84
85
}
86
catch
(Exception exp)
87
{
88
throw
exp;
89
}
90
91
}
92
93
}
94
}
95
4.
program
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Configuration;
4
using
System.Text;
5
using
System.Threading;
6
using
System.Transactions;
7
using
VideoConvert;
8
9
namespace
VideoConvert
10
{
11
class
Program
12
{
13
14
//
threadCount
15
private
static
int
threadCount
=
int
.Parse(ConfigurationManager.AppSettings[
"
ThreadCount
"
]);
16
17
private
static
IConvert tool
=
new
FfmpegConvert();
18
19
//
finished count
20
private
static
int
completeCount
=
0
;
21
22
static
void
Main(
string
[] args)
23
{
24
25
26
Thread workTicketThread;
27
Thread[] workerThreads
=
new
Thread[threadCount];
28
29
for
(
int
i
=
0
; i
<
threadCount; i
++
)
30
{
31
32
workTicketThread
=
new
Thread(
new
ThreadStart(ProcessVideo));
33
34
//
Make this a background thread, so it will terminate when the main thread/process is de-activated
35
workTicketThread.IsBackground
=
true
;
36
workTicketThread.SetApartmentState(ApartmentState.STA);
37
38
//
Start the Work
39
workTicketThread.Start();
40
workerThreads[i]
=
workTicketThread;
41
}
42
43
Console.WriteLine(
"
Converting begin. press Enter stop
"
);
44
Console.ReadLine();
45
Console.WriteLine(
"
cancel
"
);
46
47
//
abort all threads
48
for
(
int
i
=
0
; i
<
workerThreads.Length; i
++
)
49
{
50
51
workerThreads[i].Abort();
52
}
53
54
Console.WriteLine();
55
Console.WriteLine(
"
Processed
"
+
completeCount
+
"
video files
"
);
56
Console.WriteLine(
"
Process compeleted. press Enter to exit
"
);
57
Console.ReadLine();
58
59
60
}
61
62
63
/**/
///
<summary>
64
///
Convert
65
///
</summary>
66
///
<returns></returns>
67
private
static
void
ProcessVideo()
68
{
69
70
71
while
(
true
)
72
{
73
74
75
76
if
(
!
string
.IsNullOrEmpty(waitConvertFile))
77
{
78
//
Convert
79
Console.WriteLine(
"
start to convert file:
"
+
waitConvertFile
+
""
);
80
try
81
{
82
if
(tool.Convert(waitConvertFile.PhysicalPath)
&&
tool.GetSmallImage(waitConvertFile.PhysicalPath))
83
{
84
completeCount
++
;
85
86
//
Change waitConvertFile status if need
87
88
}
89
}
90
catch
(Exception exp)
91
{
92
//
setting Convert failure
93
Console.WriteLine(
"
文件
"
+
waitConvertFile.VideoID
+
"
Convert failure
"
);
94
}
95
Console.WriteLine(
"
文件
"
+
waitConvertFile.VideoID
+
"
Convert ending
"
);
96
Thread.Sleep(
1000
);
97
}
98
Thread.Sleep(
1000
*
60
);
99
100
101
102
103
104
}
105
106
107
}
108
}
109
}
通过四步,我们视频转换工程就创建完了,这里的主要思路是服务器端调用视频转换工具 ffmpeg.exe,设置参数,通过Main来实现转换。如果有问题请和我联系。
posted @ 2007-06-19 10:12
秀才
阅读(5037)
评论(28)
编辑
收藏
所属分类:
.net开发心得
发表评论
回复
引用
查看
#1楼
2007-06-19 10:25 |
overred
你这个貌似能把avi的转成flv,至于其他的格式你还得二次转换,这个才是最重要的,也是大家最关心的,我这有把大部分格式转为flv的demo以及组件,有兴趣可以交流!
回复
引用
查看
#2楼
2007-06-19 10:42 |
申健
@overred
如果有好东东写出来大家共享一下吧。
回复
引用
查看
#3楼
2007-06-19 10:44 |
若寒
先谢谢分享。
如果转换很长的视频文件,Sleep(6000);就 return true;可能视频还没转换完。期待更好的解决方法。
目前我正伤脑筋。。
回复
引用
查看
#4楼
2007-06-19 10:49 |
overred
@申健
等我有时间拉吧
基本上覆盖世面上的一些视频格式到flv的转换.采用比较落后的msmq队列(也是若寒问题的解决方法之一)
如果你有钱可以使用on2的流媒体服务器,直接解码,貌似3000多$每年
有兴趣的可以给我交流
回复
引用
#5楼
2007-06-19 10:52 |
kerry [未注册用户]
能否边传边转?
回复
引用
查看
#6楼
2007-06-19 10:54 |
YAO.NET℡
while (true)
{
怎感觉很别扭呢.
另外,有一种转换失败的情况,这种情况C#的try不能捕获,但是文件就是没有生成,这种错误情况的处理.
在文件较大,文件较多的情形下,还得考虑队列转换,也即多线程+队列,否则若干个视频同时转服务器当掉.
你的 while (true)之所以感觉别扭,是因为ffmpeg会出一种莫名的错误,将整个线程阻死,而问题是C#并捕获不到,所以一直就卡那儿了.
还有就是overred 所说的对于wmv,rm的一些格式的处理,可能要二次转换.
回复
引用
#7楼
2007-06-19 11:06 |
quickcn [未注册用户]
比较有用。
回复
引用
查看
#8楼
2007-06-19 11:07 |
若寒
我现在用mencoder.exe转换成flv,多种格式的转换都没问题。
在文件较大,文件较多的情形下,还得考虑队列转换,也即多线程+队列?这个不是很清楚,得先了解一下,谢谢YAO.NET.
回复
引用
查看
#9楼
2007-06-19 11:08 |
YAO.NET℡
@若寒
通过Sleep不是好办法,不准确.
我想你头痛的和我当年遇到的一样.
回复
引用
查看
#10楼
2007-06-19 11:09 |
YAO.NET℡
不要使用Process.Start()直接ffmpeg.exe,改用C#执行bat文件,bat文件我们可以自定义,比如转换命令后,写一句exit,在转换命名执行完后,窗口会关闭,C#会捕获窗口关闭,再发出自定义事件消息,在自定义事件里检查文件是否生成即可知,但仍不是100%准确.
最好的办法是使用VC++,直接研究ffmpeg源码,最准确.
回复
引用
查看
#11楼
2007-06-19 11:14 |
overred
建议有人创建个flv视频转换的qq群,呵呵 只是建议(本人创建不了)