CITIC localization has been the trend, in the domestic operating system on the application development needs more and more, for example, some customers need to be in the Galaxy Kirin and Unicom UOS to achieve the recording desktop to generate a mp4 file. So how to realize this?
I. Technical program
To accomplish these functions, specifically, the following technical issues need to be addressed:
(1) Desktop image data acquisition.
(2) Video data encoding (H264).
(3) Write the encoded data to the file container in .mp4 file format.
NET Core (C#), a cross-platform UI framework, and with the help of these two components, it is easy to collect desktop image data from domestic operating systems and encode them into mp4 files.
Let's take a look at how the desktop recording program works on Galaxy Kirin:
Clicking the "Start Device" button will start the desktop image capture thread to capture desktop images at a frame rate of 30fps.
Click the "Start Recording" button, the recording component will be initialized, and then the captured desktop image will be recorded to generate an mp4 file (in the run directory).
Click the "End Recording" button, the recording will be completed, at this time you can open the generated mp4 file to play.
II. Concrete realization
(1) IDesktopCapturer is a desktop image capture component.
(2) We can create a capturer instance by calling the CreateDesktopCapturer 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 captured data will be exposed through the corresponding events (ImageCaptured), and we can get the captured 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:
private void OpenDevice() { this.desktopCapturer = CapturerFactory.CreateDesktopCapturer(frameRate); this. += DesktopCapturer_ImageCaptured; videoSize = this.; this.(); }
Create and start the recorder:
private void StartRecord() { string desktopPath = Environment.GetFolderPath(Environment.) + .FileHelper.GetFilePathSeparatorChar().ToString(); this.silenceVideoFileMaker = new SilenceVideoFileMaker(); this.(desktopPath+"test.mp4", VideoCodecType.H264, , , frameRate, ); this.recording = true; this. = ; }
Feed the captured data to the recorder:
private void DesktopCapturer_ImageCaptured(byte[] obj) { if(this.recording) { this.(obj); } }
Stop recording:
private void StopRecord() { this.recording = false; this.(true); this. = true; this. = Visibility.Collapsed; }
III. Deployment operations
To run the recording program here on Galaxy Kirin or Unix UOS, you need to install .NET Core 3.1 on the target OS first.
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
After enter and run, the UI interface of the previous screenshot will appear, and then we can record the desktop image.
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.