前言:因之前有遇到在項目中將.flv文件視頻轉換為.mp4,故作此記錄,以下是使用ffmpeg.exe作為轉碼工具。
1:接下來需要準備工具;http://ffmpeg.org/ 下載相應版本的ffmpeg.exe(64位+32位)
2:實現代碼,以下只是其中一種實現方式:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
namespace VideoConvert.Models
{
public class FFmpegHelper
{
/// <summary>
/// 32位
/// </summary>
//判斷系統版本: Is32Or64(); p.StartInfo.Arguments = arg; p.StartInfo.FileName = path; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; //表示不顯示轉碼窗口 p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; //設置進程終止時觸發事件; p.EnableRaisingEvents = true; p.Exited += new EventHandler(p_Exited); p.OutputDataReceived +=new DataReceivedEventHandler(p_OutputDataReceived); p.ErrorDataReceived +=new DataReceivedEventHandler(p_ErrorDataReceived); p.Start(); //讀取輸出; p.BeginOutputReadLine(); p.BeginErrorReadLine(); //設置等待進程觸發p_Exited事件后在往下執行; p.WaitForExit(); } } void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) { //記錄輸出日志 } void p_OutputDataReceived(object sender, DataReceivedEventArgs e) { //記錄輸出日志 } void p_Exited(object sender, EventArgs e) { //進程退出觸發該事件,可以利用此執行其它操作或者是判斷 } public void Is32Or64() { if (Environment.Is64BitOperatingSystem) { path = ffmpegPath64; } else { path = ffmpegPath32; } } } }
新聞熱點
疑難解答