Location>code7788 >text

C# realize domestic Linux video recording to generate mp4 (with source code, Galaxy Kirin, Unicorn UOS)

Popularity:226 ℃/2024-08-18 09:23:46

With the coming of the wave of localization of Xinchuang, there are more and more demands for application development on local operating system. Recently, a customer needs to record camera video and microphone sound on Galaxy Kirin or Unicom UOS, and record them into an mp4 file. So how to realize such a function?

I. Technical program

To accomplish these functions, specifically, the following technical issues need to be addressed:

(1) Microphone data acquisition.

(2) Camera data acquisition.

(3) Audio data encoding.

(4) Video data encoding.

(5) Write the encoded data to the file container in .mp4 file format.

(6) Ensure synchronization of audio-video playback.

Using the cross-platform C# .NET Core, the cross-platform UI framework Avalonia, and the and components, it is easy to capture microphone and camera data and encode and write them to mp4 files.

Let's take a look at how the recording program works on Galaxy Kirin:

Two drop-down lists allow you to select the microphone and camera devices to be used.

Click the "Start" button, the microphone and camera will start to collect data and record to generate an mp4 file (in the run directory).

Click the "End" button, the recording will be completed, at this time you can open the generated mp4 file to play.

II. Concrete realization

(1) ICameraCapturer is the camera video capture component; IMicrophoneCapturer is the microphone sound capture component.

(2) We can create the corresponding collector instance by calling the CreateXXXX method of CapturerFactory.

(3) After getting the collector instance, call the Start method to start the collection; call the Stop method to stop the collection.

(4) The collected data will be exposed through the corresponding events (ImageCaptured, AudioCaptured), and we can get the collected data by booking these events.

(5) Feed the obtained data to VideoFileMaker, and VideoFileMaker will encode it and write it to the mp4 file.

We list the core code here, the complete code you can download the source code from the end of the article to understand.

Create and start the collector:

            //Camera Collector
             = (cameraIndex, videoSize, frameRate);
             += CameraCapturer_ImageCaptured;
             += CameraCapturer_CaptureError;
            //microphone pickup
             = (micIndex);
             += MicrophoneCapturer_AudioCaptured;
             += MicrophoneCapturer_CaptureError;
 
            ();
            ();

Create and start the recorder:

             = new VideoFileMaker();
            ("test.mp4", VideoCodecType.H264, , , frameRate, , , audioSampleRate, channelCount, true);

Feed the captured data to the recorder:

    private void CameraCapturer_ImageCaptured(byte[] obj)
    {
        if ()
        {
            (obj);
        }
    }
 
    private void MicrophoneCapturer_AudioCaptured(byte[] obj)
    {
        if ()
        {
            (obj);
        }
    }

Stop recording:

    private void FinishRecorded(object? sender, e)
    {
        this.RecordState_Changed(false);
        ?.Stop();
        ?.Dispose();
        ?.Stop();
        ?.Dispose();
        ?.Close(true);
        ("Recording completed.!", this);
    }

III. Deployment operations

To run the recording program here on Galaxy Kirin or Unicom UOS, you need to install .NET Core 3.1 on the target OS now.

Then copy the netcoreapp3.1 folder in the VS generated directory to the target computer, enter the netcoreapp3.1 folder, open the terminal, and enter the following command in the terminal:

dotnet Oraycn_Avalonias_RecordDemo.

Once you enter and run it, the UI interface of the previous screenshot will appear, and then we can record the microphone camera.

IV. Source Code Download

The unmanaged libraries included in the source code are for the X64 architecture, if you need to run the program on other architectures of domestic chips, you can contact me to get the unmanaged libraries for the corresponding architectures.