国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#:讀取視頻的寬度和高度等信息

2019-11-17 02:45:42
字體:
來源:轉載
供稿:網友

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


上一篇:DbContext 查詢

下一篇:CLR

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭阳县| 大田县| 辰溪县| 宁陵县| 象州县| 青海省| 本溪| 郎溪县| 阳信县| 泌阳县| 兰坪| 六安市| 宝丰县| 衡阳县| 横山县| 明光市| 晋宁县| 甘泉县| 金秀| 天门市| 临汾市| 武邑县| 司法| 佳木斯市| 乳山市| 清镇市| 射阳县| 赤峰市| 怀宁县| 闽侯县| 襄城县| 宜宾县| 崇礼县| 辉县市| 庆安县| 昌宁县| 喜德县| 松溪县| 田林县| 浑源县| 响水县|