【问题标题】:Getting the length of video using ffmpeg/ffprobe is not working使用 ffmpeg/ffprobe 获取视频长度不起作用
【发布时间】:2016-11-23 07:06:57
【问题描述】:

我正在努力使用 FFMPEG 获取视频的长度/持续时间。下面是我从 Google 获得的代码,但是当我运行该方法时,它返回空字符串。做了我能做的任何调整,但没有成功。谁能指导我这里出了什么问题?

 private static void GetVideoDuration()
    {
        string basePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
        string filePath = basePath + @"1.mp4";            
        string cmd = string.Format("-v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1  {0}", filePath);
        Process proc = new Process();
        proc.StartInfo.FileName = Path.Combine(basePath, @"ffprobe.exe");
        proc.StartInfo.Arguments = cmd;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.UseShellExecute = false;            

        if (!proc.Start())
        {
            Console.WriteLine("Error starting");
            return;
        }
        StreamReader reader = proc.StandardError;
        string line;
        while ((line = reader.ReadToEnd()) != null)
        {
            Console.WriteLine(line);
        }
        proc.Close();
    }

【问题讨论】:

  • 您必须 RedirectStandardOutput 并从 FFMPEG 读取所有输出。 FFMPEG 将“大量”文本放在那里。启动FFMPEG后,先WaitForExit,让FFMPEG完成工作。
  • 如果您手动运行ffprobe 命令,通过命令行界面未编写脚本,它是否有效?这应该在您开始尝试编写脚本之前尝试。
  • 不,我没有尝试过使用命令行界面的 ffprobe,但我尝试了几个 ffprobe 命令,将其安装在上面的代码中,但每次它们返回空字符串作为输出,可悲的是他们应该只返回我需要的视频时长。
  • 嗨@LordNeckbeard,我尝试使用命令行界面和上面的命令它工作正常。你能指导我为什么它不能使用上面的代码吗?

标签: c# ffmpeg ffprobe


【解决方案1】:

谢谢大家,我知道了。

似乎ffprobe没有使用

返回输出
proc.StandardError;

但使用

proc.StandardOutput;

上面的语句在 ffmpeg 上运行良好,但在 ffprobe 上不行,下面的语句在 ffprobe 上运行。 如果有人需要,这是一个工作示例。

   private static void GetVideoDuration()
    {
        string basePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
        string filePath = basePath + @"1.mp4";
        string cmd = string.Format("-v error -select_streams v:0 -show_entries stream=duration -sexagesimal -of default=noprint_wrappers=1:nokey=1  {0}", filePath);
        Process proc = new Process();
        proc.StartInfo.FileName = Path.Combine(basePath, @"ffprobe.exe");
        proc.StartInfo.Arguments = cmd;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.UseShellExecute = false;
        if (!proc.Start())
        {
            Console.WriteLine("Error starting");
            return;
        }
        string duration = proc.StandardOutput.ReadToEnd().Replace("\r\n", "");
        // Remove the milliseconds
        duration = duration.Substring(0, duration.LastIndexOf("."));
        proc.WaitForExit();
        proc.Close();
    }

上述方法将以 hh:mm:ss.fff tt 格式返回您的持续时间,表示包括毫秒,但如果您希望持续时间以秒为单位,请从命令中删除 -sexagesimal。

【讨论】:

    猜你喜欢
    • 2019-11-11
    • 2015-03-03
    • 2022-11-10
    • 2011-05-19
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    相关资源
    最近更新 更多