zoukankan      html  css  js  c++  java
  • C# ffmpeg工具将视频转为SWF格式

    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:UsersAdministratorDesktopffmpegin";
                    //需要转化的视频目录
                    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:UsersAdministrator"
                    string startCommandContent = @"cd C:UsersAdministrator";
    
                    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:UsersAdministratorDesktopffmpegin"
                    string commandCDFfmpegBin = @"cd {0}";
    
                    commandCDFfmpegBin = string.Format(commandCDFfmpegBin, @ffmpegBinPath);
    
                    process.StandardInput.WriteLine(@commandCDFfmpegBin);
    
                    //如果码特率太多转化出错       
                    //@"ffmpeg -i C:UsersAdministratorDesktopffmpeg	est.avi -to 00:30 c:	tt.avi";
    
                    //规定一定的码特率
                    //ffmpeg -i C:UsersAdministratorDesktopffmpeg	est.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);
                }
            }
        }
    }
  • 相关阅读:
    IIS7中的几种身份鉴别方式(一)Basic身份验证
    IIS7中的几种身份鉴别方式(二)集成身份验证
    java集合
    SharePoint 2010中welcome page的设置细节
    SharePoint中使用Linq出现未将对象引用到实例化的解决方法
    SharePoint 2010中关于An error was encountered while retrieving the user profile的处理方式记录
    The Need for an Architectural Body of Knowledge
    The Softer Side of the Architect
    Event Receivers 学习小结
    使用SmtpClient发送带图片的邮件的代码实现
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/4028248.html
Copyright © 2011-2022 走看看