Ozeki Camera Recorder Stat

How to record audio and video stream into .MPEG-4 in C#

In this guide you can find information about recording an audio and a video stream with an IP camera into MPEG-4 file format. 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 record audio and video stream with 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 Windows Forms/WPF 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: http://www.camera-sdk.com/p_13-download-onvif-standard-ozeki-camera-sdk-for-webcam-and-ip-camera-developments-onvif.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
NVR_Record_MPEG4_WF\NVR_Record_MPEG4_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
NVR_Record_MPEG4_WPF\NVR_Record_MPEG4_WPF.sln

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

To find more about the previously used functions included here as well, please, visit this tutorial

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

Button_CaptureVideoStart_Click(): initializes the path variable that is used to determine the destination folder of the captured video. Then calls the StartVideoCapture() method with this parameter.

StartVideoCapture(): creates a string that contains the actual date and time then uses its parameter to complete the path. Finally, initializes the Mpeg4Recorder object, subscribes to its events and establishes the connection between the media channels and the Mpeg4Recorder object.

Mpeg4Recorder_MultiplexFinished(): terminates all connections and closes the recorder object.

StopVideoCapture(): calls a multiplexing method and terminates the connections.

Button_SaveTo_Click(): makes the user to be able to choose the path and saves the result into the TextBox GUI element that can be seen on the picture below.

Clicking the "Start" button begins the capturing process and when you push the "Stop" button the process ends. The interval of the captured video will be the interval between the two button hits.

If the TextBox GUI element is empty, there will be no path where we could save the captured video so we will save it to the source folder where the application has been started from.

Implement image settings of an IP camera in C#

Windows Form WPF  

Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using Ozeki.Media;
using Ozeki.Camera;

namespace Network_Video_Recorder01
{
    public partial class Form1 : Form
    {
        private IIPCamera _camera;
        private DrawingImageProvider _imageProvider;
        private MediaConnector _connector;
        private VideoViewerWF _videoViewerWf;
        private MPEG4Recorder _mpeg4Recorder;
 
        public Form1()
        {
            InitializeComponent();
            _imageProvider = new DrawingImageProvider();
            _connector = new MediaConnector();
            _videoViewerWf = new VideoViewerWF();
            SetVideoViewer();
        }
 
        private void SetVideoViewer()
        {
            CameraBox.Controls.Add(_videoViewerWf);
            _videoViewerWf.Size = new Size(260, 180);
            _videoViewerWf.BackColor = Color.Black;
            _videoViewerWf.TabStop = false;
            _videoViewerWf.Location = new Point(14, 19);
            _videoViewerWf.Name = "_videoViewerWf";
        }
 
        private void button_Connect_Click(object sender, EventArgs e)
        {
            _camera=new IPCamera("192.168.112.109:8080","user","qwe123");
            _connector.Connect(_camera.VideoChannel, _imageProvider);
            _videoViewerWf.SetImageProvider(_imageProvider);
            _videoViewerWf.Start();
            _camera.Start();
        }
 
        private void Button_CaptureVideoStart_Click(object sender, EventArgs e)
        {
            var path = TextBox_SaveTo.Text;
            if (!String.IsNullOrEmpty(path))
                StartVideoCapture(path);
        }
 
        private void StartVideoCapture(string 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";
 
            _mpeg4Recorder = new MPEG4Recorder(currentpath);
            _mpeg4Recorder.MultiplexFinished += Mpeg4Recorder_MultiplexFinished;
            _connector.Connect(_camera.AudioChannel, _mpeg4Recorder.AudioRecorder);
            _connector.Connect(_camera.VideoChannel, _mpeg4Recorder.VideoRecorder);
        }
 
        private void Mpeg4Recorder_MultiplexFinished(object sender, Ozeki.VoIP.VoIPEventArgs e)
        {
            _connector.Disconnect(_camera.AudioChannel, _mpeg4Recorder.AudioRecorder);
            _connector.Disconnect(_camera.VideoChannel, _mpeg4Recorder.VideoRecorder);
            _mpeg4Recorder.Dispose();
        }
 
        private void Button_CaptureVideoStop_Click(object sender, EventArgs e)
        {
            StopVideoCapture();
        }

        private void StopVideoCapture()
        {
            if (_mpeg4Recorder != null)
            {
                _mpeg4Recorder.Multiplex();
                _connector.Disconnect(_camera.AudioChannel, _mpeg4Recorder.AudioRecorder);
                _connector.Disconnect(_camera.VideoChannel, _mpeg4Recorder.VideoRecorder);
            }
        }
 
        private void Button_SaveTo_Click(object sender, EventArgs e)
        {
            var result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
                TextBox_SaveTo.Text = folderBrowserDialog1.SelectedPath;
        }
    }
}
	

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.

GUI

The Graphical User Interface of an application for recording audio and video stream into .MPEG-4 in C#
Figure 1 - The graphical user interface of your application

Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your Windows Forms Application will be able to work properly.

Form1.Designer.cs

	namespace Network_Video_Recorder01
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.GroupBox_Connect = new System.Windows.Forms.GroupBox();
            this.button_Connect = new System.Windows.Forms.Button();
            this.CameraBox = new System.Windows.Forms.GroupBox();
            this.button_CaptureVideo1Stop = new System.Windows.Forms.Button();
            this.TextBox_SaveTo = new System.Windows.Forms.TextBox();
            this.button_SaveTo1 = new System.Windows.Forms.Button();
            this.button_CaptureVideo1Start = new System.Windows.Forms.Button();
            this.GroupBox_Capture = new System.Windows.Forms.GroupBox();
            this.GroupBox_Connect.SuspendLayout();
            this.GroupBox_Capture.SuspendLayout();
            this.SuspendLayout();
            // 
            // GroupBox_Connect
            // 
            this.GroupBox_Connect.Controls.Add(this.button_Connect);
            this.GroupBox_Connect.Location = new System.Drawing.Point(10, 10);
            this.GroupBox_Connect.Name = "GroupBox_Connect";
            this.GroupBox_Connect.Size = new System.Drawing.Size(110, 60);
            this.GroupBox_Connect.TabIndex = 0;
            this.GroupBox_Connect.TabStop = false;
            this.GroupBox_Connect.Text = "Connect";
            // 
            // button_Connect
            // 
            this.button_Connect.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.button_Connect.ForeColor = System.Drawing.Color.Black;
            this.button_Connect.Location = new System.Drawing.Point(10, 20);
            this.button_Connect.Name = "button_Connect";
            this.button_Connect.Size = new System.Drawing.Size(90, 25);
            this.button_Connect.TabIndex = 6;
            this.button_Connect.Text = "Connect";
            this.button_Connect.UseVisualStyleBackColor = true;
            this.button_Connect.Click += new System.EventHandler(this.button_Connect_Click);
            // 
            // CameraBox
            // 
            this.CameraBox.Location = new System.Drawing.Point(10, 90);
            this.CameraBox.Name = "CameraBox";
            this.CameraBox.Size = new System.Drawing.Size(290, 210);
            this.CameraBox.TabIndex = 3;
            this.CameraBox.TabStop = false;
            this.CameraBox.Text = "Live camera ";
            // 
            // button_CaptureVideo1Stop
            // 
            this.button_CaptureVideo1Stop.Location = new System.Drawing.Point(190, 20);
            this.button_CaptureVideo1Stop.Name = "button_CaptureVideo1Stop";
            this.button_CaptureVideo1Stop.Size = new System.Drawing.Size(90, 25);
            this.button_CaptureVideo1Stop.TabIndex = 36;
            this.button_CaptureVideo1Stop.Text = "Stop capture";
            this.button_CaptureVideo1Stop.UseVisualStyleBackColor = true;
            this.button_CaptureVideo1Stop.Click += new System.EventHandler(this.Button_CaptureVideoStop_Click);
            // 
            // TextBox_SaveTo
            // 
            this.TextBox_SaveTo.Location = new System.Drawing.Point(140, 60);
            this.TextBox_SaveTo.Name = "TextBox_SaveTo";
            this.TextBox_SaveTo.Size = new System.Drawing.Size(140, 20);
            this.TextBox_SaveTo.TabIndex = 35;
            // 
            // button_SaveTo1
            // 
            this.button_SaveTo1.Location = new System.Drawing.Point(10, 60);
            this.button_SaveTo1.Name = "button_SaveTo1";
            this.button_SaveTo1.Size = new System.Drawing.Size(90, 25);
            this.button_SaveTo1.TabIndex = 34;
            this.button_SaveTo1.Text = "Save to:";
            this.button_SaveTo1.UseVisualStyleBackColor = true;
            this.button_SaveTo1.Click += new System.EventHandler(this.Button_SaveTo_Click);
            // 
            // button_CaptureVideo1Start
            // 
            this.button_CaptureVideo1Start.Location = new System.Drawing.Point(10, 20);
            this.button_CaptureVideo1Start.Name = "button_CaptureVideo1Start";
            this.button_CaptureVideo1Start.Size = new System.Drawing.Size(90, 25);
            this.button_CaptureVideo1Start.TabIndex = 33;
            this.button_CaptureVideo1Start.Text = "Start capture";
            this.button_CaptureVideo1Start.UseVisualStyleBackColor = true;
            this.button_CaptureVideo1Start.Click += new System.EventHandler(this.Button_CaptureVideoStart_Click);
            // 
            // GroupBox_Capture
            // 
            this.GroupBox_Capture.Controls.Add(this.TextBox_SaveTo);
            this.GroupBox_Capture.Controls.Add(this.button_CaptureVideo1Stop);
            this.GroupBox_Capture.Controls.Add(this.button_CaptureVideo1Start);
            this.GroupBox_Capture.Controls.Add(this.button_SaveTo1);
            this.GroupBox_Capture.Location = new System.Drawing.Point(10, 300);
            this.GroupBox_Capture.Name = "GroupBox_Capture";
            this.GroupBox_Capture.Size = new System.Drawing.Size(290, 100);
            this.GroupBox_Capture.TabIndex = 0;
            this.GroupBox_Capture.TabStop = false;
            this.GroupBox_Capture.Text = "Capture";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(314, 414);
            this.Controls.Add(this.GroupBox_Capture);
            this.Controls.Add(this.CameraBox);
            this.Controls.Add(this.GroupBox_Connect);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Network Video Recorder";
            this.GroupBox_Connect.ResumeLayout(false);
            this.GroupBox_Capture.ResumeLayout(false);
            this.GroupBox_Capture.PerformLayout();
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.GroupBox GroupBox_Connect;
        private System.Windows.Forms.Button button_Connect;
        private System.Windows.Forms.GroupBox CameraBox;
        private System.Windows.Forms.Button button_CaptureVideo1Stop;
        private System.Windows.Forms.TextBox TextBox_SaveTo;
        private System.Windows.Forms.Button button_SaveTo1;
        private System.Windows.Forms.Button button_CaptureVideo1Start;
        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
        private System.Windows.Forms.GroupBox GroupBox_Capture;
    }
}
	

Code 2 - GUI example in C#

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 System.Drawing.dll, the System.Windows.Forms.dll and the OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.
  3. At recording I got an error. Why?

    Make sure the you have enough place recording.