C #: Read the width and height of the video,

Source: Internet
Author: User

C #: Read the width and height of the video,

Read method: Use ffmpeg to read data, so you need to download ffmpeg first. There are many online resources.

Run the command ffmpeg to read the frame height and width of the video.

 

The obtained frame height and frame width are displayed in the blue line box.

The next step is simple. Construct a command and execute it. I have not tested all video formats. It is estimated that common formats should be supported.

The code for executing the command is as follows:

 
/// <Summary> /// execute a command // </summary> /// <param name = "command"> Command to be executed </param>/ // <param name = "output"> output </param> // <param name = "error"> error </param> public static void ExecuteCommand (string command, out string output, out string error) {try {// create a Process pc = new Process (); pc. startInfo. fileName = command; pc. startInfo. useShellExecute = false; pc. startInfo. redirectStandardOutput = true; pc. startInfo. redirectStandardError = true; pc. startInfo. createNoWindow = true; // start the process pc. start (); // prepare to read the output stream and error stream string outputData = string. empty; string errorData = string. empty; pc. beginOutputReadLine (); pc. beginErrorReadLine (); pc. outputDataReceived + = (ss, ee) => {outputData + = ee. data;}; pc. errorDataReceived + = (ss, ee) => {errorData + = ee. data ;}; // wait for exiting the pc. waitForExit (); // shut down the process pc. close (); // return stream result output = outputData; error = errorData;} catch (Exception) {output = null; error = null ;}}
 

 

 

The code for obtaining the height width is as follows: (assume that ffmpeg exists in the application directory)

/// <Summary> /// obtain the video's frame width and frame height /// </summary> /// <param name = "videoFilePath"> mov file path </param> // <returns> null indicates an error occurred while obtaining the width or height. </returns> public static void GetMovWidthAndHeight (string videoFilePath, out int? Width, out int? Height) {try {// determine whether the object exists if (! File. exists (videoFilePath) {width = null; height = null;} // run the command to obtain some information about the file. string ffmpegPath = new FileInfo (Process. getCurrentProcess (). mainModule. fileName ). directoryName + @ "\ ffmpeg.exe"; string output; string error; Helpers. executeCommand ("\" "+ ffmpegPath +" \ "" + "-I" + "\" "+ videoFilePath +" \ "", out output, out error ); if (string. isNullOrEmpty (error) {width = null; height = null ;} // use the regular expression to obtain the width information Regex regex = new Regex ("(\ d {2, 4}) x (\ d {2, 4})", RegexOptions. compiled); Match m = regex. match (error); if (m. success) {width = int. parse (m. groups [1]. value); height = int. parse (m. groups [2]. value) ;}else {width = null; height = null ;}} catch (Exception) {width = null; height = null ;}}

 

 

Reprinted statement: This article is reprinted from http://www.zhoumy.cn/, the original link is: http://www.zhoumy.cn /? P = 35

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.