C# 获取视频编码 及 分辨率
C# 获取视频编码 及 分辨率
1、需要借助外部工具:ffmpeg.exe
下载地址:https://download.csdn.net/download/qq_41054313/20680212
2、将下载的内容解压缩,并将bin文件夹的内容复制到debug文件夹


复制到运行程序根目录

3、复制如下程序:
using MediaInfoDotNet; using MediaInfoLib; using RabbitMQ.Client; using System; using System.Diagnostics; using System.IO; using System.Net.Http; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace ConsoleApp1 { internal class Program { public static async Task Main(string[] args) { string videoUrl = "https://xxxx.oss-cn-shanghai.aliyuncs.com/b3bbbca25d10488eaed62d634317907b.mp4"; string tempVideoPath = Path.Combine("D:\\logs\\", $"temp_video_{Guid.NewGuid()}.mp4"); using (HttpClient client = new HttpClient()) { var response = await client.GetAsync(videoUrl); response.EnsureSuccessStatusCode(); using (var streamToReadFrom = await response.Content.ReadAsStreamAsync()) using (var streamToWriteTo = File.OpenWrite(tempVideoPath)) { await streamToReadFrom.CopyToAsync(streamToWriteTo); } } int? width = 0; int? height = 0; string error = string.Empty; GetMovWidthAndHeight(tempVideoPath, out width, out height,ref error); if (error.Contains("h264")) { Console.WriteLine("视频编码正确。"); } if (width == 1080 && height == 1600) { Console.WriteLine("视频分辨率正确。"); } else { Console.WriteLine("视频分辨率错误。"); } } /// <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; } } /// <summary> /// 获取视频的帧宽度和帧高度 /// </summary> /// <param name="videoFilePath">mov文件的路径</param> /// <returns>null表示获取宽度或高度失败</returns> public static void GetMovWidthAndHeight(string videoFilePath, out int? width, out int? height,ref string error) { try { //判断文件是否存在 if (!File.Exists(videoFilePath)) { width = null; height = null; } //执行命令获取该文件的一些信息 string ffmpegPath = new FileInfo(Process.GetCurrentProcess().MainModule.FileName).DirectoryName + @"\ffmpeg.exe"; string output; 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; } } } }
4、完毕 over
更新:
using Newtonsoft.Json; using RabbitMQ.Client; using System; using System.Diagnostics; using System.IO; using System.Net.Http; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace ConsoleApp1 { internal class Program { public static async Task Main(string[] args) { var s = await GetMovWidthAndHeight("https://xxxx.oss-cn-shanghai.aliyuncs.com/65d28880d76548deb49bdbbabac79199.mp4"); Console.WriteLine(JsonConvert.SerializeObject(s)); } /// <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; } } /// <summary> /// 获取视频的帧宽度和帧高度 /// </summary> /// <param name="videoFilePath">mov文件的路径</param> /// <returns>null表示获取宽度或高度失败</returns> public static async Task<checkVideoResult> GetMovWidthAndHeight(string videoUrl) { checkVideoResult result = new checkVideoResult(); string tempVideoPath = Path.Combine("D:\\logs\\", $"temp_video_{Guid.NewGuid()}.mp4"); using (HttpClient client = new HttpClient()) { var response = await client.GetAsync(videoUrl); response.EnsureSuccessStatusCode(); using (var streamToReadFrom = await response.Content.ReadAsStreamAsync()) using (var streamToWriteTo = File.OpenWrite(tempVideoPath)) { await streamToReadFrom.CopyToAsync(streamToWriteTo); } } try { //判断文件是否存在 if (!File.Exists(tempVideoPath)) { return new checkVideoResult(); } //执行命令获取该文件的一些信息 string ffmpegPath = new FileInfo(Process.GetCurrentProcess().MainModule.FileName).DirectoryName + @"\ffmpeg.exe"; string output; string error; ExecuteCommand("\"" + ffmpegPath + "\"" + " -i " + "\"" + tempVideoPath + "\"", out output, out error); if (string.IsNullOrEmpty(error)) { return new checkVideoResult(); } //通过正则表达式获取信息里面的宽度信息 Regex regex = new Regex("(\\d{2,4})x(\\d{2,4})", RegexOptions.Compiled); Match m = regex.Match(error); if (m.Success) { result.width = int.Parse(m.Groups[1].Value); result.height = int.Parse(m.Groups[2].Value); } if (error.Contains("h264")) { result.code = "h264"; } } catch (Exception) { return new checkVideoResult(); } return result; } } public class checkVideoResult { public int? width { get; set; } public int? height { get; set; } public string code { get; set; } } }

浙公网安备 33010602011771号