【发布时间】:2020-04-06 05:09:12
【问题描述】:
我的目标是将视频文件传递给 FFMPEG 并将其尺寸作为输出。我该如何实现这一点。谁能帮我提供示例代码?
【问题讨论】:
我的目标是将视频文件传递给 FFMPEG 并将其尺寸作为输出。我该如何实现这一点。谁能帮我提供示例代码?
【问题讨论】:
public void GetVideoInfo(string input)
{
// set up the parameters for video info.
string @params = string.Format("-i {0}", input);
string output = Run(ffmpegProcess, @params);
//get the video format
re = new Regex("(\\d{2,3})x(\\d{2,3})");
Match m = re.Match(output);
if (m.Success)
{
int width = 0; int height = 0;
int.TryParse(m.Groups[1].Value, out width);
int.TryParse(m.Groups[2].Value, out height);
}
}
private static string Run(string process/*ffmpegFile*/, string parameters)
{
if (!File.Exists(process))
throw new Exception(string.Format("Cannot find {0}.", process));
// Create a process info.
ProcessStartInfo oInfo = new ProcessStartInfo(process, parameters);
//oInfo.UseShellExecute = false;
//oInfo.CreateNoWindow = true;
//oInfo.RedirectStandardOutput = true;
//oInfo.RedirectStandardError = true;
// Create the output and streamreader to get the output.
string output = null;
//StreamReader outputStream = null;
// Try the process.
//try
//{
// Run the process.
Process proc = System.Diagnostics.Process.Start(oInfo);
proc.WaitForExit();
//outputStream = proc.StandardError;
//output = outputStream.ReadToEnd();
proc.Close();
//}
//catch( Exception ex )
//{
// output = ex.Message;
//}
//finally
//{
// // Close out the streamreader.
// if( outputStream != null )
// outputStream.Close();
//}
return output;
}
您应该取消注释某些代码才能使其正常工作。希望对您有所帮助。
我的代码可以从视频、不同的转换等中获取更多信息。上面的代码是切片的,可能需要稍作修改。
【讨论】:
你试过FFMPEG-C# Library吗?
【讨论】:
您应该使用命令“-i {0}”({0} 是视频路径)创建执行 ffprobe 的 C# 进程。
我不知道为什么,但是当你在 c# 进程上运行时,ffmpeg 总是在错误通道中返回输出,因此你必须在进程完成后阅读 StandardError Stream。
当我们得到输出字符串时,我们可以通过对其运行正则表达式来找出解决方案。
int intVideoWidth = 0; int intVideoHeight = 0;
Process processGetOriginalVideoData = new Process();
processGetOriginalVideoData.StartInfo.CreateNoWindow = true;
processGetOriginalVideoData.StartInfo.ErrorDialog = false;
processGetOriginalVideoData.StartInfo.RedirectStandardOutput = true;
processGetOriginalVideoData.StartInfo.RedirectStandardInput = true;
processGetOriginalVideoData.StartInfo.RedirectStandardError = true;
processGetOriginalVideoData.StartInfo.UseShellExecute = false;
processGetOriginalVideoData.StartInfo.FileName = "C:\\FFmpeg\\bin\\ffprobe.exe";
processGetOriginalVideoData.StartInfo.Arguments = string.Format("-i {0}", strMyVideoPath);
processGetOriginalVideoData.Start();
processGetOriginalVideoData.WaitForExit();
StreamReader processGetOriginalVideoDataOutputStream = processGetOriginalVideoData.StandardError;
string strProcessGetOriginalVideoDataOutput = await processGetOriginalVideoDataOutputStream.ReadToEndAsync();
processGetOriginalVideoData.Kill();
var resolutionRegex = new System.Text.RegularExpressions.Regex("(\\d{2,4})x(\\d{2,4})");
System.Text.RegularExpressions.Match resolutionRegexMatches = resolutionRegex.Match(strProcessGetOriginalVideoDataOutput);
if (resolutionRegexMatches.Success)
{
int.TryParse(resolutionRegexMatches.Groups[1].Value, out intVideoWidth);
int.TryParse(resolutionRegexMatches.Groups[2].Value, out intVideoHeight);
}
【讨论】: