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