最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2014年11月17日 10:38:10   投稿:shichen2014  
這篇文章主要介紹了C#讀取視頻的寬度和高度等信息的方法,通過執(zhí)行一條CMD命令來實(shí)現(xiàn)讀取視頻寬度和高度的功能,具有不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#讀取視頻的寬度和高度等信息的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

讀取方式:使用ffmpeg讀取,所以需要先下載ffmpeg。網(wǎng)上資源有很多。

通過ffmpeg執(zhí)行一條CMD命令可以讀取出視頻的幀高度和幀寬度信息。

運(yùn)行效果如下圖所示:

藍(lán)線框中可以看到獲取到的幀高度和幀寬度。

接下來的事情就簡(jiǎn)單了。構(gòu)造一個(gè)命令,然后執(zhí)行就ok。我并未測(cè)試過所有視頻格式,估計(jì)常見的格式應(yīng)該都支持。

執(zhí)行命令的代碼如下:

復(fù)制代碼 代碼如下:
/// <summary>
/// 執(zhí)行一條command命令
/// </summary>
/// <param name="command">需要執(zhí)行的Command</param>
/// <param name="output">輸出</param>
/// <param name="error">錯(cuò)誤</param>
public static void ExecuteCommand(string command,out string output,out string error)
{
    try
    {
 //創(chuàng)建一個(gè)進(jìn)程
 Process pc = new Process();
 pc.StartInfo.FileName = command;
 pc.StartInfo.UseShellExecute = false;
 pc.StartInfo.RedirectStandardOutput = true;
 pc.StartInfo.RedirectStandardError = true;
 pc.StartInfo.CreateNoWindow = true;

 //啟動(dòng)進(jìn)程
 pc.Start();

 //準(zhǔn)備讀出輸出流和錯(cuò)誤流
 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();

 //關(guān)閉進(jìn)程
 pc.Close();

 //返回流結(jié)果
 output = outputData;
 error = errorData;
    }
    catch(Exception)
    {
 output = null;
 error = null;
    }
}

獲取高度的寬度的代碼如下:(這里假設(shè)ffmpeg存在于應(yīng)用程序目錄)

復(fù)制代碼 代碼如下:
/// <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;
 }

 //執(zhí)行命令獲取該文件的一些信息
 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;
 }

 //通過正則表達(dá)式獲取信息里面的寬度信息
 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;
    }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • UGUI輪播圖組件實(shí)現(xiàn)方法詳解

    UGUI輪播圖組件實(shí)現(xiàn)方法詳解

    這篇文章主要為大家詳細(xì)介紹了UGUI輪播圖組件的實(shí)現(xiàn)方法,支持自動(dòng)輪播、手勢(shì)切換等功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • C#實(shí)現(xiàn)讀取指定盤符硬盤序列號(hào)的方法

    C#實(shí)現(xiàn)讀取指定盤符硬盤序列號(hào)的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)讀取指定盤符硬盤序列號(hào)的方法,涉及C#針對(duì)硬件屬性的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08
  • C#中Lambda表達(dá)式的用法

    C#中Lambda表達(dá)式的用法

    這篇文章介紹了C#中Lambda表達(dá)式的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 最新評(píng)論

    拜城县| 柘荣县| 若尔盖县| 鹤峰县| 高邑县| 谢通门县| 永顺县| 平顶山市| 锦屏县| 龙岩市| 大同市| 兴业县| 西安市| 虎林市| 连城县| 嘉鱼县| 静宁县| 云林县| 东城区| 元阳县| 长宁区| 浦城县| 西贡区| 白银市| 呈贡县| 留坝县| 射洪县| 曲阳县| 阿拉尔市| 自贡市| 淮滨县| 永安市| 西平县| 禹城市| 仙居县| 奈曼旗| 安平县| 芜湖市| 漳州市| 出国| 当雄县|