ASP.NET下調用ffmpeg與mencoder實現視頻轉換截屏

來源:互聯網
上載者:User

最近要做一個視頻播放的系統,用到了ffmpeg和mencoder兩個工具,查了一些資料,發現這方面的資料還挺多的,但是就是亂了一點,我自己從頭整理了一下,和大家分享一下:

1、ffmpeg實現視頻(avi,wmv等格式)轉換為flv格式:

 

/// <summary>    /// 轉換視頻為flv    /// </summary>    /// <param name="fileName">上傳視頻檔案的路徑(原檔案)</param>    /// <param name="playFile">轉換後的檔案的路徑(網路播放檔案)</param>    /// <returns>成功:返回1 失敗:0</returns>    public int FChangeFileVir(string fileName, string playFile)    {        //轉換後視頻高寬        string widthOfFile = "320";        string heightOfFile = "240";        //取得ffmpeg.exe的路徑,路徑配置在Web.Config中,如:<add   key="ffmpeg"   value="E:\aspx1\ffmpeg.exe"   />           string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);        if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))        {            return 0;        }        //獲得(.flv)檔案相對路徑         string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), ".flv");        //建立ffmpeg進程        System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);        //設定後台運行,不顯示視窗        FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;        //運行參數,此處組合成ffmpeg.exe檔案需要的參數即可,此處命令在ffmpeg 0.4.9以上調試通過         //ffmpeg -i F:\01.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv        FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;        try        {            //開始轉換            System.Diagnostics.Process.Start(FilestartInfo);        }        catch        {            return 0;        }        return 1;    }

2、mencoder實現視頻(avi,wmv等格式)轉換為flv格式:

 

 

    /// <summary>    /// 轉換視頻為flv    /// </summary>    /// <param name="fileName">上傳視頻檔案的路徑(原檔案)</param>    /// <param name="playFile">轉換後的檔案的路徑(網路播放檔案)</param>    /// <returns>成功:返回1 失敗:0</returns>    public int MChangeFileVir(string fileName, string playFile)    {        //轉換後視頻高寬        string widthOfFile = "320";        string heightOfFile = "240";        //取得mencoder.exe的路徑,路徑配置在Web.Config中,如:<add   key="mencoder"   value="E:\aspx1\mencoder.exe"   />         string tool = Server.MapPath(PublicMethod.mencodertool);        //判斷mencoder工具與檔案是否存在        if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))        {            return 0;        }        //獲得(.flv)檔案相對路徑         string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), ".flv");        //建立工具進程        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);        }        catch        {            return 0;        }        return 1;    }

3、ffmpeg實現視頻內容:

 

 

    //視頻,fileName視頻地址,imgFile圖片地址    public string CatchImg(string fileName, string imgFile)    {        //使用ffmpeg抓圖        string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);        //圖片名稱        string flv_img = imgFile + ".jpg";        //圖片大小        string FlvImgSize = PublicMethod.sizeOfImg;        //建立ffmpeg進程        System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);        //後台運行        ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;        //進程參數        //ffmpeg -i input.flv -y -f image2 -ss 10.11 -t 0.001 -s 240x180 catchimg.jpg;        ImgstartInfo.Arguments = "   -i   " + fileName + "  -y  -f  image2   -ss 2 -vframes 1  -s   " + FlvImgSize + "   " + flv_img;        try        {            //開始抓圖            System.Diagnostics.Process.Start(ImgstartInfo);            //睡眠一下,等待完成,按需設定            System.Threading.Thread.Sleep(5000);        }        catch        {            return "";        }        //        if (System.IO.File.Exists(flv_img))        {            return flv_img;        }        return "";    }

 

原理很簡單,其實就是使用System.Diagnostics.Process調用ffmpeg和mencoder這兩個現成的工具來進行工作,關鍵是參數傳遞,主要還是看咱對ffmpeg和mencoder使用的熟悉程度。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.