nager / Nager.VideoStream

Get images from a network camera stream or webcam
MIT License
55 stars 10 forks source link
c-sharp camera camera-image camera-preview camera-stream csharp dotnet dotnet-core dotnet-standard ffmpeg h264 h265 network-camera-stream rtsp video video-frame video-frames video-processing webcam webcam-streaming

Nager.VideoStream

This project supports the sources listed below as input, it triggers an event for each frame in one of the following formats (jpg, png, bmp).

Requirements

The library requires ffmpeg. You can download the ffmpeg binary here, they are needed to access the video stream. Just copy the ffmpeg.exe into the execution directory.

How can I use it?

The package is available on nuget

PM> install-package Nager.VideoStream

Examples of use

Network Camera (RTSP Stream)

var inputSource = new StreamInputSource("rtsp://videoserver.example/testvideo.mp4");

var cancellationTokenSource = new CancellationTokenSource();

var client = new VideoStreamClient();
client.NewImageReceived += NewImageReceived;
var task = client.StartFrameReaderAsync(inputSource, OutputImageFormat.Bmp, cancellationTokenSource.Token);

//wait for exit
Console.ReadLine();

client.NewImageReceived -= NewImageReceived;

void NewImageReceived(byte[] imageData)
{
    File.WriteAllBytes($@"{DateTime.Now.Ticks}.bmp", imageData);
}

Webcam

You can find out the name of your webcam in the Windows Device Manager in the section Cameras
Windows Device Manager

var inputSource = new WebcamInputSource("HP HD Camera");

var cancellationTokenSource = new CancellationTokenSource();

var client = new VideoStreamClient();
client.NewImageReceived += NewImageReceived;
var task = client.StartFrameReaderAsync(inputSource, OutputImageFormat.Bmp, cancellationTokenSource.Token);

//wait for exit
Console.ReadLine();

client.NewImageReceived -= NewImageReceived;

void NewImageReceived(byte[] imageData)
{
    File.WriteAllBytes($@"{DateTime.Now.Ticks}.bmp", imageData);
}

Custom Input Source - Select manual attributes

var inputSource = new CustomInputSource("-rtsp_transport tcp -i rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 -vf transpose=dir=1");

var cancellationTokenSource = new CancellationTokenSource();

var client = new VideoStreamClient();
client.NewImageReceived += NewImageReceived;
var task = client.StartFrameReaderAsync(inputSource, OutputImageFormat.Bmp, cancellationTokenSource.Token);

//wait for exit
Console.ReadLine();

client.NewImageReceived -= NewImageReceived;

void NewImageReceived(byte[] imageData)
{
    File.WriteAllBytes($@"{DateTime.Now.Ticks}.bmp", imageData);
}