c# ffmpeg视频转换

c#  ffmpeg视频转换

什么是ffmpeg,它有什么作用呢,怎么可以使用它呢,带着问题去找答案吧!先参考百度百科把,我觉得它很强大无奇不有,为了方便大家我就把链接提供了!

http://baike.baidu.com/link?url=bD4xX59DgppfdclpyGfmC38WTeEkOTM9yc35XGi4OLnQYY7BqmPJSIZHyPKp0imUdI2yujK76Nm047ACZW2hHa

今天工作中遇到这么个问题,这个问题很屌,我通过搜索引擎找了个遍,几乎没有找到理想的答案,于是被逼无奈之下,自己研究参数,于是就有了如下的成就,所以,遇到问题不要害怕,这才是成长最快的时刻。当问题解决了的时候,会有种无比的成就感,把自己的成果分享给他人,供他人参考,这也是种喜悦!

------------没有做不到的,只有想不到的。

转码进度公式=当前时间/总时间*100%;

c#  ffmpeg视频转换

   public class FFmpeg
{
static int timeCount;
public static void Execute()
{
Process p = new Process();
p.StartInfo.FileName = @"E:\张立平资料\Demo\FFmpeg_Demo\ffmpeg\ffmpeg.exe";
p.StartInfo.UseShellExecute = false;
string srcFileName = @"E:\张立平资料\Demo\FFmpeg_Demo\videoold\test.mov";
string destFileName = @"E:\张立平资料\Demo\FFmpeg_Demo\videonew\test.mp4";
p.StartInfo.Arguments = "-i " + srcFileName + " -y -ab 56 -ar 22050 -b 800 -r 29.97 -s 420x340 " + destFileName; //执行参数
//-i D:/vts-collector/test/swap/7a3c9800-2e2f-42fe-ba39-56b25f972e06.mov -y -ab 56 -ar 22050 -b 800 -r 29.97 -s 420x340 D:/vts-collector/test/swap/7a3c9800-2e2f-42fe-ba39-56b25f972e06_mov_stream.mp4 p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); using (p)
{
p.Start();
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
}
} private static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{ var output = e.Data;
if (output != null)
{
if (output.Contains("Duration"))
{
Int32 indexOfDuration = output.IndexOf("Duration");
Int32 indexOfBegin = output.IndexOf(":", indexOfDuration);
Int32 indexOfEnd = output.IndexOf(",", indexOfBegin);
var duration = output.Substring(indexOfBegin + , indexOfEnd - indexOfBegin - );
timeCount =ConvertStringtoSecond(duration);
}
if (output.Contains("time="))
{
Int32 indexOfTime = output.IndexOf("time=");
Int32 indexOfBegin = output.IndexOf("=", indexOfTime);
Int32 indexOfEnd = output.IndexOf("bitrate", indexOfBegin);
string timeStr = output.Substring(indexOfBegin + , indexOfEnd - indexOfBegin - );
var time =Convert.ToDouble(timeStr);
var process = time / timeCount*;
process = Math.Round(process);
Console.Write("{0}% ", process);
}
//Console.WriteLine(e.Data);
}
} private static int ConvertStringtoSecond(string input)
{
int totalSecond = ;
try
{
string[] split = input.Split(new char[] { ':', '.' });
int hour = int.Parse(split[]) * ;
int min = int.Parse(split[]) * ;
int second = int.Parse(split[]);
int millisecond = int.Parse(split[]) / ;
totalSecond = hour + min + second + millisecond;
}
catch (System.Exception ex)
{
Console.Write(ex.Message); }
return totalSecond;
} private static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
}
上一篇:ffplay的快捷键以及选项 FFmpeg 基本用法 FFmpeg常用基本命令 ffmpeg常用转换命令,支持WAV转AMR


下一篇:sfliter__except_handler4