My goal is to pass a Video file to FFMPEG and to get its dimension as output.How can I achieve this. Can anyone help me out with sample code?
Asked
Active
Viewed 5,015 times
3 Answers
3
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;
}
You should uncomment some code to get this working. Hope it helps.
I have code that gets more info from the video, different converts, etc. The code above is sliced and may need slight modifications.
0
You should make C# process that execute ffprobe with command "-i {0}" ({0} is video path).
I dont know why but ffmpeg always return output in Errors channel when you run it on c# process, therefore you must read StandardError Stream after process finished.
When we got the output string we can find out resolution with running a Regex on it.
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);
}

Miss Chanandler Bong
- 4,081
- 10
- 26
- 36
-
No need for a regex. [ffprobe can output the resolution directly](https://stackoverflow.com/a/29585066/). – llogan Apr 05 '20 at 17:19