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

FFmpeg的Static版本的bin文件夹中只有三个.exe文件,分别是:ffmpeg.exe,ffplay.exe和ffprobe.exe,各功能如下:

ffmpeg.exe:音视频转码、转换器

ffplay.exe:简单的音视频播放器

ffprobe.exe :多媒体码流分析器

以下实现了基于ffmpeg.exe和ffprobe.exe的视频总帧数获取和取某一帧的图像方法,可根据需求进行扩展。

cs 复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VideoCheck
{
    public class CFFmpeg
    {

        string ffmpegPath = ""; 
        string ffprobePath = "";

        
        public CFFmpeg()
        {
            if (System.IntPtr.Size == 4)
            {
                ffmpegPath = Environment.CurrentDirectory + "\\ffmpeg\\x86\\ffmpeg.exe";
                ffprobePath = Environment.CurrentDirectory + "\\ffmpeg\\x86\\ffprobe.exe";
            }
            else if (System.IntPtr.Size == 8)
            {
                ffmpegPath = Environment.CurrentDirectory + "\\ffmpeg\\x64\\ffmpeg.exe";
                ffprobePath = Environment.CurrentDirectory + "\\ffmpeg\\x64\\ffprobe.exe";
            }


     
        }



        const int SW_HIDE = 0;

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        /// <summary>
        /// 获取视频有效总帧数
        /// </summary>
        /// <param name="videoPath"></param>
        /// <returns></returns>
        public int GetTotalFrameCnt(string videoPath)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = new System.Diagnostics.ProcessStartInfo(ffprobePath);
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;

            //示例: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
            string args = " -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 " + "\"" + videoPath + "\"";
            process.StartInfo.Arguments = args.Replace("\\", "/");
            try
            {
                process.Start();
                while (process.MainWindowHandle == IntPtr.Zero)
                {
                    process.Refresh();
                }
                ShowWindow(process.MainWindowHandle, SW_HIDE);

                StringBuilder sb = new StringBuilder();
                while (!process.HasExited)
                {
                    sb.Append(process.StandardOutput.ReadToEnd());
                }
                string str = sb.ToString().Trim();
                int num = 0;
                bool ret = int.TryParse(str,out num);
                return num;
            }
            catch (Exception ex)
            {
                return 0;
            }

        }
 

        /// <summary>
        /// 截取视频图片
        /// </summary>
        /// <param name="videoPath">视频路径</param>
        /// <param name="videoFrameIndex">视频帧号</param>
        /// <param name="outImagePath">图像输出路径</param>
        /// <returns></returns>
        public bool CatchImage(string videoPath,int videoFrameIndex, string outImagePath)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = new System.Diagnostics.ProcessStartInfo(ffmpegPath);
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    
            if (File.Exists(outImagePath))
                File.Delete(outImagePath);
            else
            {
                FileInfo fileInfo = new FileInfo(videoPath);
                if (!Directory.Exists(fileInfo.DirectoryName))
                    Directory.CreateDirectory(fileInfo.DirectoryName);
            }

            int sec = (int)(videoFrameIndex / 25.0f);
            //-ss后跟的时间单位为秒 ,放前面可加快速度
            string args = " -y -ss " + sec+ " -i " + "\""+ videoPath +"\""+ " -frames 1 -f image2 " + "\"" + outImagePath+"\"";
            process.StartInfo.Arguments = args.Replace("\\","/");
            try
            {
                process.Start();
                process.WaitForExit();
            }
            catch(Exception ex)
            {
               
                return false;
            };
            if (System.IO.File.Exists(outImagePath))
            {
                return true;
            }
            return false;
        }
    }
}
 
相关推荐
归秋1428 小时前
多轨音频智能混音软件怎么选:从 demo 到更完整作品的 AI 后期工具思路
人工智能·音视频
小猴子爱上树11 小时前
跨马翻译:批量图片翻译与视频字幕、智能抠图一站式在线图片翻译工具
大数据·人工智能·python·音视频
每天死循环11 小时前
ComfyUI MiniMax H3文生视频(t2v)提示词教程:四段式写法+5秒768p实测(附模板和参数)
音视频·文生视频·comfyui·ai视频·minimax
企业数字化笔记13 小时前
视频像素分割怎么选?HSV 阈值、传统掩膜、语义分割和实例分割的效果与代价
opencv·音视频
Maynor99613 小时前
6张商品图→17秒带货短视频:开源12个Claude Skills,配音、字幕花字、BGM本地一条命令出片
ffmpeg·ai编程·claude·电商·agent skills
youdexiang14 小时前
AI 生成会议纪要好用吗?多款 APP 功能分析
java·人工智能·音视频
AI创界者14 小时前
【开源实战】MiniMax-H3 本地离线高自由度 ComfyUI 工作流搭建指南(含规则松绑与提示词映射)
人工智能·aigc·音视频
AIGC小尼14 小时前
8G 显存零基础本地部署 AI 漫剧全流程|ComfyUI+Wan2.2+FFmpeg 离线成片完整方案(含代码 / 指令 / 排坑)
人工智能·ffmpeg·php·comfyui·ai漫剧
hai31524754314 小时前
文本矩阵化拆解与视频矩阵化构建
线性代数·矩阵·音视频
昨日之日200614 小时前
Visual Enhancer:一键让模糊照片视频变清晰,升分辨率、插帧、转HDR,一键搞定素材修复
计算机视觉·音视频