C#控制台应用把视频转为图片

using System;
using System.IO;
using OpenCvSharp;

class Program
{
    static void Main(string[] args)
    {
        // 视频文件路径
        string videoPath = @"I:\OpenCvSharp学习\cylindrical.avi";
        // 输出图片的目录
        string outputFolder = @"I:\OpenCvSharp学习\output_frames";

        if (!Directory.Exists(outputFolder))
        {
            Directory.CreateDirectory(outputFolder);
        }

        using (var capture = new VideoCapture(videoPath))
        {
            if (!capture.IsOpened())
            {
                Console.WriteLine("无法打开视频文件");
                return;
            }

            int frameCount = (int)capture.Get(CapProp.FrameCount); // 获取视频总帧数

            for (int i = 0; i < frameCount; i++)
            {
                Mat frame = new Mat();
                capture.Read(frame);

                if (frame.Empty())
                {
                    break;
                }

                // 每隔一帧保存图片(此处修改为每20帧保存一次)
                if (i % 20 == 0)
                {
                    string outputPath = Path.Combine(outputFolder, $"frame_{i:D5}.jpg"); // 格式化输出文件名
                    Cv2.ImWrite(outputPath, frame);

                    Console.WriteLine($"已保存第 {i} 帧到:{outputPath}");
                }
            }
        }

        Console.WriteLine("视频转图片完成。按任意键退出...");
        Console.ReadKey();
    }
}

你可能感兴趣的:(c#,音视频,开发语言)