-
C#教程之C#读取视频的宽度和高度等信息的方法
本文实例讲述了C#读取视频的宽度和高度等信息的方法。分享给大家供大家参考。具体实现方法如下:读取方式:使用ffmpeg读取,所以需要先下载ffmpeg。网上资源有很多。通过ffmpeg执行一条CMD命令可以读取出视频的帧高度和帧宽度信息。运行效果如下图所示:蓝线框中可以看到获取到的帧高度和帧宽度。接下来的事情就简单了。构造一个命令,然后执行就ok。我并未测试过所有视频格式,估计常见的格式应该都支持。执行命令的代码如下:复制代码 代码如下:/// <summary>/// 执行一条command命令/// </summary>/// <param name="command">需要执行的Command</param>/// <param name="output">输出</param>/// <param name="error">错误</param>public static void ExecuteCommand(string command,out string output,out string error){try{//创建一个进程Process pc = new Process();pc.StartInfo.FileName = command;pc.StartInfo.UseShellExecute = false;pc.StartInfo.RedirectStandardOutput = true;pc.StartInfo.RedirectStandardError = true;pc.StartInfo.CreateNoWindow = true;//启动进程pc.Start();//准备读出输出流和错误流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;};//等待退出pc.WaitForExit();//关闭进程pc.Close();//返回流结果output = outputData;error = errorData;}catch(Exception){output = null;error = null;}}获取高度的宽度的代码如下:(这里假设ffmpeg存在于应用程序目录)复制代码 代码如下:/// <summary>/// 获取视频的帧宽度和帧高度/// </summary>/// <param name="videoFilePath">mov文件的路径</param>/// <returns>null表示获取宽度或高度失败</returns>public static void GetMovWidthAndHeight(string videoFilePath, out int? width, out int? height){try{//判断文件是否存在if (!File.Exists(videoFilePath)){width = null;height = null;}//执行命令获取该文件的一些信息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;}//通过正则表达式获取信息里面的宽度信息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;}}希望本文所述对大家的C#程序设计有所帮助。
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能