ffmpeg视频总帧数获取,取某一帧的图像方法

打印 上一主题 下一主题

主题 1016|帖子 1016|积分 3048

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
FFmpeg的Static版本的bin文件夹中只有三个.exe文件,分别是:ffmpeg.exe,ffplay.exe和ffprobe.exe,各功能如下:
ffmpeg.exe:音视频转码、转换器
ffplay.exe:简单的音视频播放器
ffprobe.exe:多媒体码流分析器
以下实现了基于ffmpeg.exe和ffprobe.exe的视频总帧数获取和取某一帧的图像方法,可根据需求举行扩展。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace VideoCheck
  10. {
  11.     public class CFFmpeg
  12.     {
  13.         string ffmpegPath = "";
  14.         string ffprobePath = "";
  15.         
  16.         public CFFmpeg()
  17.         {
  18.             if (System.IntPtr.Size == 4)
  19.             {
  20.                 ffmpegPath = Environment.CurrentDirectory + "\\ffmpeg\\x86\\ffmpeg.exe";
  21.                 ffprobePath = Environment.CurrentDirectory + "\\ffmpeg\\x86\\ffprobe.exe";
  22.             }
  23.             else if (System.IntPtr.Size == 8)
  24.             {
  25.                 ffmpegPath = Environment.CurrentDirectory + "\\ffmpeg\\x64\\ffmpeg.exe";
  26.                 ffprobePath = Environment.CurrentDirectory + "\\ffmpeg\\x64\\ffprobe.exe";
  27.             }
  28.      
  29.         }
  30.         const int SW_HIDE = 0;
  31.         [DllImport("user32.dll")]
  32.         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  33.         /// <summary>
  34.         /// 获取视频有效总帧数
  35.         /// </summary>
  36.         /// <param name="videoPath"></param>
  37.         /// <returns></returns>
  38.         public int GetTotalFrameCnt(string videoPath)
  39.         {
  40.             System.Diagnostics.Process process = new System.Diagnostics.Process();
  41.             process.StartInfo = new System.Diagnostics.ProcessStartInfo(ffprobePath);
  42.             process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  43.             process.StartInfo.RedirectStandardOutput = true;
  44.             process.StartInfo.UseShellExecute = false;
  45.             //示例:ffprobe - v error - count_frames - select_streams v: 0 - show_entries stream = nb_read_frames - of default = nokey = 1:noprint_wrappers = 1 C: \Users\whf\Desktop\大和路Y430 - Y428.ASF
  46.             string args = " -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 " + """ + videoPath + """;
  47.             process.StartInfo.Arguments = args.Replace("\", "/");
  48.             try
  49.             {
  50.                 process.Start();
  51.                 while (process.MainWindowHandle == IntPtr.Zero)
  52.                 {
  53.                     process.Refresh();
  54.                 }
  55.                 ShowWindow(process.MainWindowHandle, SW_HIDE);
  56.                 StringBuilder sb = new StringBuilder();
  57.                 while (!process.HasExited)
  58.                 {
  59.                     sb.Append(process.StandardOutput.ReadToEnd());
  60.                 }
  61.                 string str = sb.ToString().Trim();
  62.                 int num = 0;
  63.                 bool ret = int.TryParse(str,out num);
  64.                 return num;
  65.             }
  66.             catch (Exception ex)
  67.             {
  68.                 return 0;
  69.             }
  70.         }
  71.         /// <summary>
  72.         /// 截取视频图片
  73.         /// </summary>
  74.         /// <param name="videoPath">视频路径</param>
  75.         /// <param name="videoFrameIndex">视频帧号</param>
  76.         /// <param name="outImagePath">图像输出路径</param>
  77.         /// <returns></returns>
  78.         public bool CatchImage(string videoPath,int videoFrameIndex, string outImagePath)
  79.         {
  80.             System.Diagnostics.Process process = new System.Diagnostics.Process();
  81.             process.StartInfo = new System.Diagnostics.ProcessStartInfo(ffmpegPath);
  82.             process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  83.    
  84.             if (File.Exists(outImagePath))
  85.                 File.Delete(outImagePath);
  86.             else
  87.             {
  88.                 FileInfo fileInfo = new FileInfo(videoPath);
  89.                 if (!Directory.Exists(fileInfo.DirectoryName))
  90.                     Directory.CreateDirectory(fileInfo.DirectoryName);
  91.             }
  92.             int sec = (int)(videoFrameIndex / 25.0f);
  93.             //-ss后跟的时间单位为秒 ,放前面可加快速度
  94.             string args = " -y -ss " + sec+ " -i " + """+ videoPath +"""+ " -frames 1 -f image2 " + """ + outImagePath+""";
  95.             process.StartInfo.Arguments = args.Replace("\","/");
  96.             try
  97.             {
  98.                 process.Start();
  99.                 process.WaitForExit();
  100.             }
  101.             catch(Exception ex)
  102.             {
  103.                
  104.                 return false;
  105.             };
  106.             if (System.IO.File.Exists(outImagePath))
  107.             {
  108.                 return true;
  109.             }
  110.             return false;
  111.         }
  112.     }
  113. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表