【发布时间】:2011-10-24 21:36:54
【问题描述】:
我正在使用 Asp.Net C# Framework 4,目前正在开发一个视频转换应用程序。我还使用 ffmpeg 将所有上传的格式转换为 flv。由于有时在尝试从 mp4 直接转换为 flv 时遇到的问题,我首先将上传的文件转换为 mpg,然后再转换为 flv。但是 ffmpeg 一旦完成转换为 mpg 文件的过程就会冻结。当我运行任务管理器并检查进程列表时,它只是站在那里不使用 CPU 资源。当我直接从任务管理器结束 ffmpeg 进程时,会发生其他进程,从 mpg 转换为 flv 和预览文件(jpg)并且工作顺利。由于第一个进程冻结,当我尝试从网页的文件上传表单上传时,第二个进程无法启动。我很感激从现在开始的任何回应。这是我的代码:
string duration = "00:00:00";
//converting video
Process ffmpeg;
ffmpeg = new Process();
// convert to mpg 1st
ffmpeg.StartInfo.Arguments = " -i \"" + Server.MapPath("static/user/vid/") + videolink + "\" -f mpeg -b 300k -ac 2 -ab 128k -ar 44K \"" + Server.MapPath("static/user/vid/") + mpglink + "\"";
ffmpeg.StartInfo.FileName = Page.MapPath("bin/ffmpeg.exe");
ffmpeg.StartInfo.CreateNoWindow = true;
ffmpeg.StartInfo.UseShellExecute = false;
ffmpeg.StartInfo.RedirectStandardOutput = true;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.Start();
ffmpeg.WaitForExit();
ffmpeg.Close();
// mpg 2 flv
ffmpeg = new Process();
ffmpeg.StartInfo.Arguments = " -i \"" + Server.MapPath("static/user/vid/") + mpglink + "\" -f flv -s 624x352 \"" + Server.MapPath("static/user/vid/") + flvlink + "\"";
ffmpeg.StartInfo.FileName = Page.MapPath("bin/ffmpeg.exe");
ffmpeg.StartInfo.CreateNoWindow = true;
ffmpeg.StartInfo.UseShellExecute = false;
ffmpeg.StartInfo.RedirectStandardOutput = true;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.Start();
ffmpeg.BeginOutputReadLine();
string error = ffmpeg.StandardError.ReadToEnd();
ffmpeg.WaitForExit();
try
{
duration = error.Substring(error.IndexOf("Duration: ") + 10, 8);
}
catch
{
}
if (ffmpeg.ExitCode != 0)
{
ltrUpload.Text = "<div class=\"resultbox-negative\" id=\"divResult\">Problem occured during upload process. Error code: " + error + "<br>" + "</div>";
return;
}
ffmpeg.Close();
// generate preview image
ffmpeg.StartInfo.Arguments = " -i \"" + Server.MapPath("static/user/vid/") + flvlink + "\" -s 624x352 -ss 00:00:03 -an -vframes 1 -f image2 -vcodec mjpeg \"" + Server.MapPath("static/user/vid/") + flvlink.Replace(".flv", ".jpg") + "\"";
ffmpeg.StartInfo.FileName = Page.MapPath("bin/ffmpeg.exe");
ffmpeg.StartInfo.CreateNoWindow = true;
ffmpeg.StartInfo.UseShellExecute = false;
ffmpeg.StartInfo.RedirectStandardOutput = true;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.Start();
ffmpeg.WaitForExit();
ffmpeg.Close();
// deleting original file and mpg
FileInfo fi = new FileInfo(Server.MapPath("static/user/vid/") + videolink);
if (fi.Exists) fi.Delete();
fi = new FileInfo(Server.MapPath("static/user/vid/") + mpglink);
if (fi.Exists) fi.Delete();
【问题讨论】:
-
与您的问题并不真正相关(但它可能会有所帮助) - 您确实应该将这样一个长时间运行的进程移至一个单独的应用程序,如 Windows 服务。它可以监视文件夹并进行多线程转换等。Web 应用程序并不适合这种事情。
标签: c# asp.net visual-studio ffmpeg