C# ffmpeg工具將視頻轉為SWF格式

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

1.下載 ffmpeg 工具

using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Diagnostics;using System.IO;namespace WebThreadTest{    public partial class ffmpegTest : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                //將是視頻轉為SWF格式                //ffmpeg bin目錄                string ffmpegBinPath = @"C:\Users\Administrator\Desktop\ffmpeg\bin\";                //需要轉化的視頻目錄                string convertDirPath = Server.MapPath("/resource/ffmpeg");                //開始轉化                AllVideoConvertToSWF(convertDirPath,ffmpegBinPath);            }        }        /// <summary>        /// 將視頻轉為SWF格式        /// </summary>        /// <param name="ffmpegBinPath">ffmpeg 工具bin 目錄</param>        /// <param name="videoSourceFileName">視頻完全路徑</param>        /// <param name="targetSwfFileName">轉為SWF檔案完全路徑</param>        /// <returns></returns>        private bool VideoConvertToSWF(string ffmpegBinPath, string videoSourceFileName, string targetSwfFileName)        {            try            {                Process process = new Process();                process.StartInfo.FileName = "cmd.exe";//確定程式名                 //"cd C:\Users\Administrator\"                string startCommandContent = @"cd C:\Users\Administrator\";                string sCommand = startCommandContent;                process.StartInfo.Arguments = sCommand;//確定程式命令列                 process.StartInfo.UseShellExecute = false;//Shell的使用                 process.StartInfo.RedirectStandardInput = true;//重新導向輸入                 process.StartInfo.RedirectStandardOutput = true;//重新導向輸出                 process.StartInfo.RedirectStandardError = true;//重新導向輸出錯誤                   process.StartInfo.CreateNoWindow = true;//設定置不顯示示視窗                 process.Start();//00                     process.StandardInput.WriteLine(sCommand);//也可以用這種方式輸入入要行的命令                 //"cd C:\Users\Administrator\Desktop\ffmpeg\bin\"                string commandCDFfmpegBin = @"cd {0}";                commandCDFfmpegBin = string.Format(commandCDFfmpegBin, @ffmpegBinPath);                process.StandardInput.WriteLine(@commandCDFfmpegBin);                //如果碼特率太多轉化出錯                       //@"ffmpeg -i C:\Users\Administrator\Desktop\ffmpeg\test.avi -to 00:30 c:\ttt.avi";                //規定一定的碼特率                //ffmpeg -i C:\Users\Administrator\Desktop\ffmpeg\test.avi -to 00:30 -ar 22050 -y c:\ccc.avi                string commandVideoContent = @"ffmpeg -i {0} -to 00:10 {1}";                commandVideoContent = string.Format(commandVideoContent, @videoSourceFileName, @targetSwfFileName);                process.StandardInput.WriteLine(@commandVideoContent);                process.StandardInput.WriteLine("exit");//要得加上Exit要不然下一行程式                  //string result = process.StandardOutput.ReadToEnd(); //輸出出流取得命令列結果                //Response.Write(result);                  return true;            }            catch (Exception ex)            {                Response.Write(@videoSourceFileName + "轉為WSF檔案失敗<br />");                Response.Write(ex.Message + "<br />");                return false;            }        }        /// <summary>        /// 列出指定目錄下及所其有子目錄及子目錄裡更深層目錄裡的檔案中的視頻檔案進行轉化          /// </summary>        /// <param name="convertDirPath">需要轉化的視頻目錄路徑</param>        /// <param name="ffmpegBinPath">轉化工具ffmpeg bin目錄路徑</param>        public void AllVideoConvertToSWF(string convertDirPath, string ffmpegBinPath)        {            DirectoryInfo dir = new DirectoryInfo(convertDirPath);            string exts = ".avi,.mp3,.mp4,.rmvb,.rm,.wmv,.3gp,.amv,.mov";            //找到該目錄下的檔案              FileInfo[] fi = dir.GetFiles();            foreach (FileInfo f in fi)            {                string extension = Path.GetExtension(f.FullName);                int extenIndex = f.FullName.LastIndexOf(extension);                string beforeName = f.FullName.Remove(extenIndex);                string swfFileName = beforeName + ".swf";                if (System.IO.File.Exists(swfFileName))                {                    continue;                }                string ext = Path.GetExtension(f.FullName).ToLower();                if (exts.Contains(ext))                {                    try                    {                        bool isSuccess = VideoConvertToSWF(ffmpegBinPath, f.FullName, swfFileName);                        Response.Write("完整路徑:" + f.FullName.ToString() + " 產生SWF檔案路徑:" + swfFileName + "  產生狀態:" + isSuccess.ToString() + "<br>");                    }                    catch (Exception ex)                    {                        Response.Write("完整路徑:" + f.FullName.ToString() + " 建置錯誤:" + ex.Message + "<br>");                    }                }            }            //擷取子目錄            DirectoryInfo[] subDir = dir.GetDirectories();            foreach (var d in subDir)            {                AllVideoConvertToSWF(d.FullName, ffmpegBinPath);            }        }    }}

 

C# ffmpeg工具將視頻轉為SWF格式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.