首页 > 代码库 > C# ffmpeg工具将视频转为SWF格式
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:\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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。