【发布时间】: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,我尝试使用命令行界面和上面的命令它工作正常。你能指导我为什么它不能使用上面的代码吗?