65.9K
CodeProject 正在变化。 阅读更多。
Home

使用ASP.NET的FFMPEG

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.45/5 (14投票s)

2008年4月7日

CPOL

2分钟阅读

viewsIcon

167141

这段代码将向您展示如何将任何视频文件格式转换为高质量的 flv, 并在 Web 服务器上流式传输。

引言

我们很多人发现,像FFMPEG这样的软件包在我们的web应用程序中使用起来相当繁琐。我们很多人已经选择了不同的模块,例如Media Handler等,来将他们的视频文件转换为flv并在服务器上进行流式传输。

这个模块将帮助您将视频文件转换为flv格式,可以轻松地在服务器上进行流式传输。此外,您可以使用此代码获得高质量的flv。

背景

这篇文章背后的基本思想是使用FFMPEG命令行参数来满足需求。互联网上所有其他流行的模块都基于这些命令行,您可以直接使用它们来实现您的目标。

使用代码

这是我尝试开发的网站。它仍在建设中。因此,您可能会发现一些错误。但是转换部分运行良好。

[对不起,我不得不关闭链接,因为我的服务器在这个应用程序上有一些垃圾邮件。如果有兴趣,请通过我的电子邮件与我联系,我会尽我所能帮助他/她]

以下是步骤……

步骤 - 1

只需从互联网上下载FFMPEG模块。最新版本的FFMPEG将是一个zip文件,其中包含所有源代码、FFMPEG.exe、FLVTOOL.exe等等。将这些文件解压缩到您的应用程序目录中。FFMPEG.exe主要将视频文件转换为flv,而FLVTOOL则将元数据插入到flv文件中。这样,视频文件将包含所有视频信息。如果没有元数据,视频文件将无法正常播放。

步骤2

分配可执行文件路径和目录。

        AppPath = Request.PhysicalApplicationPath 'Get the application path
        inputPath = AppPath & "videos\org_video" 'Path of the original file
        outputPath = AppPath & "videos\conv_video" 'Path of the converted file
        imgpath = AppPath & "videos\thumbs" 'Path of the generated thumanails
        prevpath = AppPath & "videos\preview" 'Path of the preview file

现在,主要的是在服务器上执行ffmpeg.exe的命令行……

string cmd as string = " -i """ & newPath & """ """ & outputPath & "\" & out & 
"""" '-i specifies the input file

这里newpath是源视频文件的路径,outputpath是转换后的视频的路径,out是文件名。

上述命令行将视频转换为flv格式。如果您想获得高质量的flv,请使用以下代码……

Dim fileargs As String = "-i " & newPath & " -acodec mp3 -ab 64k -ac 2 
-ar 44100 -f flv -deinterlace -nr 500 -croptop 4 -cropbottom 8 -cropleft 
8 -cropright 8 -s 640x480 -aspect 4:3 -r 25 -b 650k -me_range 25 -i_qfactor 
0.71 -g 500 " & outputPath & "\" & out & ""
上面的代码将生成高质量的flv。现在,要将元数据插入到生成的flv文件中,请使用以下命令行
Dim filebuffer As String = "-U """ & outputPath & "\" & out & """"
要创建缩略图,请使用以下代码……
Dim imgargs As String = " -i """ & newPath & """ -f image2 -ss 1 -vframes 1 -s 
80x60 -an """ & imgpath & "\" & thumbnm & """"
-i = 输入文件 -f = 文件格式 -vframe = 视频帧 -s = 尺寸

现在创建一个可以执行这些命令行的函数

       Dim proc As New System.Diagnostics.Process()
        proc.StartInfo.FileName = exepath 
'Path of exe that will be executed, only for "filebuffer" it will be "flvtool2.exe"
        proc.StartInfo.Arguments = cmd 'The command which will be executed
        proc.StartInfo.UseShellExecute = False
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.RedirectStandardOutput = False
        proc.Start()
步骤 - 3

上面的代码将任何类型的视频转换为FLV,现在您唯一需要做的就是使用任何Flash视频播放器播放视频。

关注点

您可以使用此代码轻松开发任何流式视频应用程序,并轻松构建像youtube或sharkle这样的网站。如有任何帮助,请随时通过arinhere@gmail.com给我发邮件

© . All rights reserved.