您的位置:首页 > 其它

视频转码在实际项目中的应用

2015-08-11 21:50 204 查看

视频转码在实际项目中的应用

前言:因之前有遇到在项目中将.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>

private static string ffmpegPath32 = AppDomain.CurrentDomain.BaseDirectory + @"ffmpeg\32\ffmpeg.exe";

/// <summary>

/// 64位

/// </summary>

private static string ffmpegPath64 = AppDomain.CurrentDomain.BaseDirectory + @"ffmpeg\64\ffmpeg.exe";

/// <summary>

/// 实际版本

/// </summary>

private static string path;

private static Process p = null;

/// <summary>

/// 将flv转码为 mp4

/// </summary>

/// <param name="oldPath">....flv</param>

/// <param name="newPath">.....mp4</param>

public void VideoConvert(string oldPath,string newPath)

{

using (p = new Process())

{

//更多转码方式可以查看官方文档,以下只是其中一种:

string arg = " -i " + oldPath + " -c:v libx264 -crf 23 -c:a libfaac -q:a 100 " + newPath;

//判断系统版本:

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;

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: