How to set frame rate, codec, and picture size for video recording (resample video stream) in C#

In this guide you can find information on how to connect to an IP camera and record a video with a given picture size. To implement this example, you need to have Ozeki Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.

How to set the picture size for video recording of an IP camera device using C#?

To establish the connection properly between your application and an IP camera you should apply the same code snippet what you have used in the example (How to connect to an IP camera device using C#?). Important: you should study this article in order to find out how to setup your Console Application correctly.

Getting started

To get started it is recomended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:

Download Ozeki Camera SDK: https://camera-sdk.com/p_6513-download-onvif-ozeki-camera-sdk-for-c-sharp.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
NVR_Set_Resolution_Console\NVR_Set_Resolution_Console.sln

To compile this example you will need Microsoft Visual Studio installed on your computer.

The additional class, statements and methods of this example are the following:

With the ReadComand() method you can read three commands: size, start, stop.

  • Size: You can set the resolution of the video. The default width is 1024 and the default height is 768.
  • Start: Start video recording.
  • Stop: Stop the video recording.

With the VideoResizer class your application will be able to change the resolution of the recorded video.

videoResizer.SetResolution(width, height); method will set the resolution with the given width and height. In this method you create a list which contains supported video formats and set the format that the current list element have to use. After these you connect the camera's videochannel to the videoResizer and then connect videoResizer to the _mpeg4Recorder.

This was you achieved that we record the video with the given resolution.

Implement image settings of an IP camera in C#

Program.cs

using System;
using Ozeki.Media;
using Ozeki.Camera;

namespace Network_Video_Recorder04
{
    class VideoResizer : VideoHandler
    {
        public void SetResolution(int width, int height)
        {
            SetSupportedFormats(new[] { new VideoFormat(VideoType.Uncompressed, new Resolution(width, height)) });
        }

        public override void OnDataReceived(object sender, VideoData data)
        {
            SendData(data);
        }
    }

    class Program
    {
        private static IIPCamera _camera;
        private static MPEG4Recorder _mpeg4Recorder;
        private static MediaConnector _connector;
        private static VideoResizer _videoResizer;
        private static int _width = 1024, _height = 768;

        static void Main(string[] args)
        {
            Connect();
            ReadCommand();
        }

        private static void Connect()
        {
            _connector = new MediaConnector();
            _videoResizer = new VideoResizer();
            _camera = new IPCamera("192.168.112.109:8080", "user", "qwe123");
            _camera.CameraStateChanged += Camera_CameraStateChanged;
            _camera.Start();
        }

        private static void ReadCommand()
        {
            while (true)
            {
                var line = Console.ReadLine();
                if (string.IsNullOrEmpty(line) || line.Equals("exit"))
                    break;

                switch (line)
                {
                    case "start": CreateVideo(); break;
                    case "stop": StopVideoCapture(); break;
                    case "size": ReadSize(); break;
                }
            }
        }

        private static void ReadSize()
        {
            Console.Write("Width: "); _width = int.Parse(Console.ReadLine());
            Console.Write("Height: "); _height = int.Parse(Console.ReadLine());
        }

        private static void Camera_CameraStateChanged(object sender, CameraStateEventArgs e)
        {
            switch (e.State)
            {
                case CameraState.Connecting:
                    Console.WriteLine("Connecting...");
                    break;
                case CameraState.Connected:
                    Console.WriteLine("Successfully connected");
                    break;
                case CameraState.StreamConnecting:
                    Console.WriteLine("Stream Connecting");
                    break;
                case CameraState.Streaming:
                    Console.WriteLine("Streaming");
                    break;
                case CameraState.Disconnected:
                case CameraState.Error: Console.WriteLine("ERROR!!!"); break;
            }
        }

        private static void CreateVideo()
        {
            var Path = "c:\\Users\\Public\\Documents\\";
            Console.WriteLine("Start video recording. Saved to: " + Path);
            var date = DateTime.Now.Year + "y-" + DateTime.Now.Month + "m-" + DateTime.Now.Day + "d-" +
                       DateTime.Now.Hour + "h-" + DateTime.Now.Minute + "m-" + DateTime.Now.Second + "s";
            string currentpath;

            if (String.IsNullOrEmpty(Path))
                currentpath = date + ".mp4";
            else
                currentpath = Path + "\\" + date + ".mp4";

            _videoResizer.SetResolution(_width, _height);
            _connector.Connect(_camera.VideoChannel, _videoResizer);

            _mpeg4Recorder = new MPEG4Recorder(currentpath);
            _mpeg4Recorder.MultiplexFinished += Mpeg4Recorder_MultiplexFinished;

            _connector.Connect(_videoResizer, _mpeg4Recorder.VideoRecorder);
            _connector.Connect(_camera.AudioChannel, _mpeg4Recorder.AudioRecorder);
        }

        private static void Mpeg4Recorder_MultiplexFinished(object sender, Ozeki.VoIP.VoIPEventArgs e)
        {
            Console.WriteLine("Multiplexing...");
            _connector.Disconnect(_camera.VideoChannel, _mpeg4Recorder.VideoRecorder);
            _mpeg4Recorder.Dispose();
            Console.WriteLine("Multiplexed");
        }

        private static void StopVideoCapture()
        {
            _mpeg4Recorder.Multiplex();
            _connector.Disconnect(_camera.VideoChannel, _mpeg4Recorder.VideoRecorder);
            Console.WriteLine("Stop video recording");
        }
    }
}
Code 1 - Implement image settings of an IP camera in C#

Please note that none of the cancel and disconnect methods are included in the example because of the demonstrating intent and briefness of the article.

Console output

your console output
Figure 1 - Your console output

DISCLAIMER: Please note that the following features will only work if your IP camera supports the given function. You should check the user manual of your IP camera to make sure it supports the feature that you wish to implement in C#.

Related Pages

FAQ

Below you can find the answers for the most frequently asked questions related to this topic:

  1. How can I get the URL of the camera?

    You can get the URL from the producer of the camera. (In the 10th tutorial you can find information on how to create an own IP camera discoverer program.)

  2. I have not managed to build the solution. How to solve it?

    • Please set the Target framework property of the project to .NET 4.0.
    • You should add the OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.

More information