Coder Social home page Coder Social logo

nager / nager.videostream Goto Github PK

View Code? Open in Web Editor NEW
47.0 6.0 10.0 76 KB

Get images from a network camera stream or webcam

License: MIT License

C# 100.00%
dotnet dotnet-core dotnet-standard csharp rtsp h264 h265 video-frame video-frames video-processing

nager.videostream's Introduction

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).

  • Network Camera Stream (e.x. RTSP)
  • Webcam
  • Video File

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);
}

nager.videostream's People

Contributors

tinohager avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nager.videostream's Issues

ffmpeg.exe not shutting down on application crash

Context

Running on Windows 10 and .NET framework 4.8

This is how I start the video stream:

stopTokenSource = new CancellationTokenSource();
Client = new VideoStreamClient();
Client.NewImageReceived += Client_NewImageReceived;
var inputSource = new StreamInputSource(streamUrl);
streamReadingTask = Client.StartFrameReaderAsync(inputSource, OutputImageFormat.Bmp, stopTokenSource.Token);

And this is how it is stopped and disposed:

stopTokenSource?.Cancel();
stopTokenSource?.Dispose();
stopTokenSource = null;

if (Client != null)
    Client.NewImageReceived -= Client_NewImageReceived;

Problem

When closing my application normally, the videostream is stopped and dealt with accordingly.
However, when an unexpected exception occurs and the application crashes, ffmpeg.exe will still be running.
This is causing a lot of issues for me, mainly because the TCP ports used by my application will remain open until ffmpeg.exe is shut down manually through the Task Manager.

Solution

The problem can be solved by either:

  • Setting UseShellExecute = true in the ProcessStartInfo in the StartFrameReaderAsync method
  • Adding an extra (optional) parameter: StartFrameReaderAsync(InputSource inputSource, OutputImageFormat outputImageFormat, CancellationToken cancellationToken, bool useShellExecute = false)

Nager.VideoStream is not working on linux

Hey,
I'm trying to run a dotnet program on Raspberry Pi (running Pi OS) using this package.
I connect a USB camera to the RPi and trying to record a video from it.
It works perfect on windows.
How can I make it work on Linux?
I tried setting the ffmpeg path to the path on the machine and it still not working.

Thanks

RTSP user and password as an arguments?

Very useful project! thx.

quastion:
In case of user and password in the RTSP string cause problems, for example when the password contains @ char
so the string could not handle that
Can I pass it as an argument?

"rtsp://user:[email protected]...."
"rtsp://root:example@2345@#353@192....."

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

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,

Add CustomVideoSource

Need to add custom video source (needed for additional parameters - certain RTSP for example)
I'd be happy to add it

Linux deployment

Hi, thanks for sharing your experinces with us . I need guidance on linux deployment. In surce code, tou are checking ffmpeg.exe file as I can see. So i think there should be some modifications. If you guide me to the right points, I can make this work on linux also.

Thank you again.

StartFrameReaderAsync can't be stopped when rtsp is offline

I'm using Nager.VideoStream for getting frames from a security camera on a Windows 10 PC with a .NET framework 4.8 application.

When the source is offline/not reachable, it is not possible to stop StartFrameReaderAsync.
Even when you cancel the provided CancellationToken, StartFrameReaderAsync won't stop.
The only way to then stop it, is by closing my entire application.

I reproduced this issue in the Nager.VideoStream.TestConsole by only changing:

  • The input source: var inputSource = new StreamInputSource("my_secret_rtsp_url");
  • The location of the ffmpeg.exe: var client = new VideoStreamClient(@"my_secret_folder\ffmpeg.exe");

The console application clearly cancels the cancellation token:

Console.WriteLine("Press any key for stop");
Console.ReadKey();
cancellationTokenSource.Cancel();

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

But this console writeline is never executed:

Console.WriteLine("End Stream Processing");

In my application I need to call StartFrameReaderAsync quite frequently.
That means without proper cancellation, multiple StartFrameReaderAsync threads will be active at the same time.
This is a major performance issue for my application when the camera is offline/not reachable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.