nager / Nager.VideoStream

Get images from a network camera stream or webcam
MIT License
54 stars 10 forks source link

How to use your repo to extract all frames from a local video MP4 file? #5

Closed zydjohnHotmail closed 3 years ago

zydjohnHotmail commented 3 years ago

Hello: I have one video MP4 file (C:\Videos\VLCPlayer\testVideo0.mp4), it has size of 4.2MB. I want to extract all frames in this video file to one folder: C:\Videos\VideoFrames. I created one C# console project, target .Net 5.0. The following is my code: using Nager.VideoStream; using System; using System.IO; using System.Threading; using System.Threading.Tasks;

namespace NagerVideoStreamFrames { class Program { public const string Video_MP4_File = @"C:\Videos\VLCPlayer\testVideo0.mp4"; public const string Frame_Folder = @"C:\Videos\VideoFrames\";

    private static async Task StartStreamProcessingAsync(InputSource inputSource, CancellationToken cancellationToken = default)
    {
        Console.WriteLine("Start Stream Processing");
        var client = new VideoStreamClient();
        client.NewImageReceived += NewImageReceived;
        await client.StartFrameReaderAsync(inputSource, OutputImageFormat.Png, cancellationToken);
        client.NewImageReceived -= NewImageReceived;
        Console.WriteLine("End Stream Processing");
    }

    private static void NewImageReceived(byte[] imageData)
    {
        Console.WriteLine($"New image received, bytes:{imageData.Length}");
        File.WriteAllBytes($@"C:\Videos\VideoFrames\{DateTime.Now.Ticks}.png", imageData);
    }

    static void Main()
    {
        var inputSource = new FileInputSource(Video_MP4_File);
        var cancellationTokenSource = new CancellationTokenSource();

        _ = Task.Run(async () => await StartStreamProcessingAsync(inputSource, cancellationTokenSource.Token));
        Console.WriteLine("Press any key for stop");
        Console.ReadKey();
        cancellationTokenSource.Cancel();

        Console.WriteLine("Press any key for quit");
        Console.ReadKey();
    }
}

}

However, my code did NOT work, and I can’t debug it. There is no output frames in the target folder. Please advise on how to fix this? Thanks,