How to get video dimensions using FFMPEG

2019-07-11 19:48发布

问题:

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?

回答1:

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.



回答2:

Have you tried the FFMPEG-C# Library?



标签: c# ffmpeg