winfrom的一个参考demo
private void button1_Click(object sender, EventArgs e)
{
string FromName = "E:\\录像\\原始视频.mp4";//原始视频
string ExportName = "E:\\录像\\转换完的视频.mp4";//转换完的视频
VideoConvertFlvFast(FromName, ExportName);
}
public bool VideoConvertFlvFast(string FromName, string ExportName)
{
if (!System.IO.File.Exists(FromName))
{
return false;
}
string ffmpeg = "";
try
{
ffmpeg = System.AppDomain.CurrentDomain.BaseDirectory + "converter\\avcodec.exe";
}
catch (Exception ex)
{
return false;
}
//FromName = HttpContext.Current.Server.MapPath(FromName);
//ExportName = HttpContext.Current.Server.MapPath(ExportName);
//string Command = " -i \"" + FromName + "\" -y -vcodec copy -acodec copy \"" + ExportName + "\"";
//string Command = string.Format("-i {0} -vcodec copy -an {1} -y", FromName, ExportName);//->Flv格式 (抛去音频)
//string Command = " ffmpeg.exe -i F1_02.264 -vcodec copy -an F1_02.flv -y ";
//string Command = string.Format("-i {0} -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -qscale 11 {1} -y", FromName, ExportName);//->Flv格式 (抛去音频)
// string Command = string.Format("-i {0} -r 24 -b 400k -vcodec libx264 -preset ultrafast -qp 35 -y {1}", FromName, ExportName);//
string Command = string.Format("-i {0} -r 24 -b 400k -vcodec libx264 -preset ultrafast -qp 35 -y {1}", FromName, ExportName);//
//string Command = string.Format("-i {0} -c:v libx264 -preset medium -b:v 500k -maxrate 500k -bufsize 1000k -cpu-used 5 {1}", FromName, ExportName);//
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = ffmpeg;
p.StartInfo.Arguments = Command;
p.StartInfo.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory + "converter\\";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;//不创建进程窗口
//开始执行
p.Start();
//设置CPU数 这里使用了CPU0和CPU1和CPU3和CPU4
p.ProcessorAffinity = (IntPtr)(0x0001 | 0x0002 );
//string str = p.StandardOutput.ReadToEnd();
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
return true;
}
注意:在转换的时候很消耗CPU 导致电脑卡顿,这时候我们可以使用
p.ProcessorAffinity = (IntPtr)(0x0001 | 0x0002 );限制CPU的数,防止CPU占用率100%的情况。