zoukankan      html  css  js  c++  java
  • 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;
                }
            }

    转载声明:本文转载自http://www.zhoumy.cn/,原文链接:http://www.zhoumy.cn/?id=9

  • 相关阅读:
    安装VisualStudio时失败,错误信息安装包失败或证书不在有效期内
    解决方案看起来是受源代码管理,但无法找到它的绑定信息。由于无法自动恢复这些缺少的信息,缺少绑定的项目将被视为不受源代码管理。
    ASP.NET+MVC+EntityFramework快速实现增删改查
    C#服务端Web Api
    Android DrawerLayout侧滑菜单
    Redis的简单使用
    windows10安装sqlmap与简单配置

    并发编程
    网络基础知识
  • 原文地址:https://www.cnblogs.com/DoNetCoder/p/4100127.html
Copyright © 2011-2022 走看看