gpt4 book ai didi

c# - 使用 ffmpeg/ffprobe 获取视频的长度不起作用

转载 作者:行者123 更新时间:2023-12-04 22:46:21 27 4
gpt4 key购买 nike

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

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

最佳答案

谢谢大家,我得到了答案。

似乎 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.

关于c# - 使用 ffmpeg/ffprobe 获取视频的长度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com