:: AForge.NET Framework :: Articles :: Forums ::

Console app to capture video in background

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

Console app to capture video in background

Postby sktrinh » Sun Oct 22, 2017 5:18 pm

I just want to make a simple console app that can record video from webcam and save it to a filepath in the background without a GUI interface. I just want to call it from another program and have it run based on the runtime of the mother program that is calling the console app. I've only found examples using win Form as a GUI. My code is below, however it doesn't save or generate any file. I greatly appreciate any help. I am a newb. Thanks.

Code: Select all
using System;
using System.Drawing;
using System.Threading;

using AForge.Video.DirectShow;
using AForge.Video;
using AForge.Video.FFMPEG;

namespace WebCamVideo
    public class Program
        static FilterInfoCollection WebcamColl;
        static VideoCaptureDevice Device;
        static int width = 320;
        static int height = 240;
        static Bitmap video;

        static void Main(string[] args)

            WebcamColl = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            Device = new VideoCaptureDevice(WebcamColl[0].MonikerString);
            Device.NewFrame += new NewFrameEventHandler(Device_NewFrame);
//            Thread.Sleep(5000);

        static void Device_NewFrame(object sender, NewFrameEventArgs e)
            VideoFileWriter filewriter = new VideoFileWriter();
            video = (Bitmap)e.Frame.Clone();
            filewriter.Open(@"C:\Users\trinh\Desktop\test.avi", width, height, 25, VideoCodec.Default, 100000);
Posts: 2
Joined: Sun Oct 22, 2017 5:13 pm

Re: Console app to capture video in background

Postby andrew.kirillov » Mon Oct 23, 2017 7:01 am

Your application exits immediately, so there is no chance for it to do anything at all. Even if it would keep running, you overwrite video file on every new video frame. There is no support for appending frames to existing video file. You need to open it once, keep adding frames and then close it in the end.
With best regards,

Interested in supporting AForge.NET Framework?
User avatar
Site Admin, AForge.NET Developer
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Console app to capture video in background

Postby sktrinh » Tue Oct 24, 2017 3:14 am

Hi, would you be so kind to provide more details on how to get the code to work? I am a beginner and do not have enough experience to edit the code based on your suggestion. Thank you.
Posts: 2
Joined: Sun Oct 22, 2017 5:13 pm

Return to AForge.NET Framework