ffmpeg.exe 冻结

发布于 2024-11-29 02:11:37 字数 3053 浏览 0 评论 0原文

我正在使用 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();

I'm using Asp.Net C# Framework 4 and currently developing a video conversion application. I'm also using ffmpeg to convert from all uploaded formats to flv. I'm first converting uploaded file to mpg and after to flv due to problems I encountered while trying conversion directly to flv from mp4 sometimes. But ffmpeg freezes as soon as it's done with conversion process to mpg file. When I run task manager and check the processes list, it just stands there using no CPU resource. When I end the ffmpeg process directly from task manager, other process take place which converts from mpg to flv and preview file (jpg) and works smoothly. Due to freezing of first process, the second process cannot start when I try to upload from my web page's file upload form. I appreciate any response from now. Here is my code:

        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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2024-12-06 02:11:38

我知道这是一个非常古老的问题,但是如果有人通过 Google 搜索来到这里,答案如下:

您也必须阅读第一个 ffmpeg 进程的重定向错误输出,即使您不需要它。如果您不读取重定向的错误输出,则会导致死锁,因为您的程序将等待进程完成,但进程会等待读取已填充的错误输出流。您可以查找它

    // 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();

    // Use asynchronous read operations on at least one of the streams.
    // Reading both streams synchronously would generate another deadlock.
    ffmpeg.BeginOutputReadLine();
    string tmpErrorOut = ffmpeg.StandardError.ReadToEnd();

    ffmpeg.WaitForExit();
    ffmpeg.Close();

因此,您必须像第二个 ffmpeg 进程一样读取重定向的错误和输出流。

生成图像预览部分也是如此!

I know this is a very old question, but if someone gets here because of a Google search, the answer is the following:

You would have to read the redirected error output of the first ffmpeg process, too, even if you do not need it. It will result in a deadlock if you do not read the redirected error output because your program will wait for the process to finish, but the process waits for the filled error output stream to be read. You can look it up here.

    // 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();

    // Use asynchronous read operations on at least one of the streams.
    // Reading both streams synchronously would generate another deadlock.
    ffmpeg.BeginOutputReadLine();
    string tmpErrorOut = ffmpeg.StandardError.ReadToEnd();

    ffmpeg.WaitForExit();
    ffmpeg.Close();

So you would have to read the redirected error and output streams like you did with your second ffmpeg process.

The same goes for your generating image preview part!

执妄 2024-12-06 02:11:38
private bool ReturnVideo(string fileName)
{
  string html = string.Empty;
  //rename if file already exists

  int j = 0;
  string AppPath;
  string inputPath;
  string outputPath;
  string imgpath;
  AppPath = Request.PhysicalApplicationPath;
  //Get the application path
  inputPath = AppPath + "Upload\\Videos\\OriginalVideo";
  //Path of the original file
  outputPath = AppPath + "Upload\\Videos\\ConvertVideo";
  //Path of the converted file
  imgpath = AppPath + "Upload\\Videos\\Thumbs";
  //Path of the preview file
  string filepath = Server.MapPath("../Upload/Videos/OriginalVideo/" + fileName);
  while (File.Exists(filepath))
  {
    j = j + 1;
    int dotPos = fileName.LastIndexOf(".");
    string namewithoutext = fileName.Substring(0, dotPos);
    string ext = fileName.Substring(dotPos + 1);
    fileName = namewithoutext + j + "." + ext;
    filepath = Server.MapPath("../Upload/Videos/OriginalVideo/" + fileName);
  }
  try
  {
    this.fileuploadImageVideo.SaveAs(filepath);
  }
  catch
  {
    return false;
  }
  string outPutFile;
  outPutFile = "../Upload/Videos/OriginalVideo/" + fileName;
  int i = this.fileuploadImageVideo.PostedFile.ContentLength;
  System.IO.FileInfo a = new System.IO.FileInfo(Server.MapPath(outPutFile));
  while (a.Exists == false)
  { }
  long b = a.Length;
  while (i != b)
  { }

  string cmd = " -i \"" + inputPath + "\\" + fileName + "\" \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\"";
  ConvertNow(cmd);
  ViewState["fileName"] = fileName.Remove(fileName.IndexOf(".")) + ".wmv";
  string imgargs = " -i \"" + inputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".wmv" + "\" -f image2 -ss 1 -vframes 1 -s 280x200 -an \"" + imgpath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".jpg" + "\"";
  ConvertNow(imgargs);

  return true;
}

private void ConvertNow(string cmd)
{
  string exepath;
  string AppPath = Request.PhysicalApplicationPath;
  //Get the application path
  exepath = AppPath + "ffmpeg.exe";
  System.Diagnostics.Process proc = new System.Diagnostics.Process();
  proc.StartInfo.FileName = exepath;
  //Path of exe that will be executed, only for "filebuffer" it will be "wmvtool2.exe"
  proc.StartInfo.Arguments = cmd;
  //The command which will be executed
  proc.StartInfo.UseShellExecute = false;
  proc.StartInfo.CreateNoWindow = true;
  proc.StartInfo.RedirectStandardOutput = false;
  proc.Start();

  while (proc.HasExited == false)
  { }
}

if (fileuploadImageVideo.HasFile)
{
  ReturnVideo(this.fileuploadImageVideo.FileName.ToString());

  string filename = fileuploadImageVideo.PostedFile.FileName;
  fileuploadImageVideo.SaveAs(Server.MapPath("../upload/Video/"+filename));
  objfun.Video = filename ;
}
private bool ReturnVideo(string fileName)
{
  string html = string.Empty;
  //rename if file already exists

  int j = 0;
  string AppPath;
  string inputPath;
  string outputPath;
  string imgpath;
  AppPath = Request.PhysicalApplicationPath;
  //Get the application path
  inputPath = AppPath + "Upload\\Videos\\OriginalVideo";
  //Path of the original file
  outputPath = AppPath + "Upload\\Videos\\ConvertVideo";
  //Path of the converted file
  imgpath = AppPath + "Upload\\Videos\\Thumbs";
  //Path of the preview file
  string filepath = Server.MapPath("../Upload/Videos/OriginalVideo/" + fileName);
  while (File.Exists(filepath))
  {
    j = j + 1;
    int dotPos = fileName.LastIndexOf(".");
    string namewithoutext = fileName.Substring(0, dotPos);
    string ext = fileName.Substring(dotPos + 1);
    fileName = namewithoutext + j + "." + ext;
    filepath = Server.MapPath("../Upload/Videos/OriginalVideo/" + fileName);
  }
  try
  {
    this.fileuploadImageVideo.SaveAs(filepath);
  }
  catch
  {
    return false;
  }
  string outPutFile;
  outPutFile = "../Upload/Videos/OriginalVideo/" + fileName;
  int i = this.fileuploadImageVideo.PostedFile.ContentLength;
  System.IO.FileInfo a = new System.IO.FileInfo(Server.MapPath(outPutFile));
  while (a.Exists == false)
  { }
  long b = a.Length;
  while (i != b)
  { }

  string cmd = " -i \"" + inputPath + "\\" + fileName + "\" \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\"";
  ConvertNow(cmd);
  ViewState["fileName"] = fileName.Remove(fileName.IndexOf(".")) + ".wmv";
  string imgargs = " -i \"" + inputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".wmv" + "\" -f image2 -ss 1 -vframes 1 -s 280x200 -an \"" + imgpath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".jpg" + "\"";
  ConvertNow(imgargs);

  return true;
}

private void ConvertNow(string cmd)
{
  string exepath;
  string AppPath = Request.PhysicalApplicationPath;
  //Get the application path
  exepath = AppPath + "ffmpeg.exe";
  System.Diagnostics.Process proc = new System.Diagnostics.Process();
  proc.StartInfo.FileName = exepath;
  //Path of exe that will be executed, only for "filebuffer" it will be "wmvtool2.exe"
  proc.StartInfo.Arguments = cmd;
  //The command which will be executed
  proc.StartInfo.UseShellExecute = false;
  proc.StartInfo.CreateNoWindow = true;
  proc.StartInfo.RedirectStandardOutput = false;
  proc.Start();

  while (proc.HasExited == false)
  { }
}

if (fileuploadImageVideo.HasFile)
{
  ReturnVideo(this.fileuploadImageVideo.FileName.ToString());

  string filename = fileuploadImageVideo.PostedFile.FileName;
  fileuploadImageVideo.SaveAs(Server.MapPath("../upload/Video/"+filename));
  objfun.Video = filename ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文