你现在的位置:首页>>WEB编程>>ASP.NET >>ffmpeg.exe在.NET中上传视频并转换成FLV格式

ffmpeg.exe在.NET中上传视频并转换成FLV格式

更新时间:2010-7-11 23:29:50   浏览次数:327
关键词:ffmpeg.exe转换视频         

首页提供给大家一个ffmpeg.exe官方下载地址。你可以下载最新版本
地址:http://ffdshow.faireal.net/mirror/ffmpeg/

功能:在.NET2.0里,实现上传视频,然后把图片转换成FLV格式,网上有很多这样的例子,可找了很多,不是这里出问题,就是代码很乱,根本看不懂。

这里提供给大家测试通过的代码。且代码不是很多。你很容易看懂。

代码:

ASPX里的代码
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传视频" /></div>
    </form>
</body>

 

CS里代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;

public partial class VodUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
   //按钮事件
    protected void Button1_Click(object sender, EventArgs e)
    {
        string imgfileExp = this.FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf(".") + 1);
        Response.Write(imgfileExp);
        string VodName = Server.MapPath("uploadfile") + "\\" + this.getVodName() + "." + imgfileExp;
        string NVodname = Server.MapPath("uploadfile") + "\\" + this.getVodName() + ".flv";
        this.FileUpload1.PostedFile.SaveAs(VodName);
        string strCmd1 = VodName + " -ab 56 -ar 22050 -b 800 -r 29.97 -s 420x340 " + NVodname; //转换成flv格式

        RunFFMpeg(strCmd1);
    }

   //以日期的方式生成文件名
    public string getVodName()
    {
        System.DateTime currentTime = new System.DateTime();
        currentTime = System.DateTime.Now;
        Random rnd = new Random();
        string rndStr = "";
        for (int i = 0; i <= 4; i++)
        {
            rndStr += rnd.Next(10).ToString();
        }

        return currentTime.ToString("yyyyMMddhhmmssffffff") + rndStr;
    }

   //调用ffmpeg.exe转换格式
    public void RunFFMpeg(string strCmd)
    {
        //创建并启动一个新进程
        Process p = new Process();

        //设置进程启动信息属性StartInfo,这是ProcessStartInfo类,包括了一些属性和方法:
        p.StartInfo.FileName = Server.MapPath("ffmpeg.exe");           //程序名
        p.StartInfo.Arguments = " -i " + strCmd;    //执行参数

        p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程

        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;

        p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中

        p.StartInfo.CreateNoWindow = false;//不创建进程窗口
        //p.ErrorDataReceived = new DataReceivedEventHandler(Output);//FFMPEG输出流时候把流的处理过程转移到Output


        p.Start();

        p.BeginErrorReadLine();//开始异步读取

        p.WaitForExit();//阻塞等待进程结束
        p.Close();//关闭进程
        p.Dispose();//释放资源

    }
}
 

最后给大家说明一下,在使用这个代码的时候,要在WEB.CONFIG里配置一下,如果不配置,以上代码是没有办法使用的,我也是这个原因,使得我花了很多的冤枉时间。

需要在<system.web></system.web>之间加上:<httpRuntime maxRequestLength="512000"   useFullyQualifiedRedirectUrl="true" executionTimeout="9000"/>

这一点很先要。

随机推荐文章
CopyRight © 2008-2010  糊涂蛋技术网   苏ICP备05013073号
站长:xlxcn QQ:54960248  三郎 QQ:120554323   E-mail:xlxcn#126.com