Summary of C # using ffmpeg

Time:2021-3-23

The last article mentioned that ffmpeg solves the video and voice problems in the project, and said that C # and ffmpeg have to mention two class libraries.

1、Xabe.FFmpeg

A simple look at the source code and demo, found based on ffmpeg.exe The command line parameters of.

2、FFmpeg.AutoGen

Translate C language API and type into C # API and object.

Take a look at the source code generation, in which cppsharp is used to generate the g.cs file, and then compile the g.cs file.

Compared with these two libraries, the first one is relatively simple, the second one is relatively complex and powerful.

Final choice FFmpeg.AutoGen Through the API way, the flexibility is better, and it can meet its own individual needs (the push stream should realize the function of pause and continue push stream).

FFmpeg.AutoGen Not every version of the source code remains on GitHub. It’s better to clone it into your own project. The API of different versions still varies greatly. Even if there are small problems after a stable version, you can modify the source code yourself.

 

In the project, mobile phone and computer video are streaming in RTMP mode. Now let’s summarize

At first, I said that I only need video. At that time, I searched the Internet https://blog.csdn.net/vanjoge/article/details/79657874 ,

On this basis, we will sort out and improve a little and give it to the customer. However, it didn’t take long to put forward a new demand for displaying sound. It took a lot of time to solve this problem

The solution is recorded as follows:

1、 Use ffplay.exe Play the sound and report the error all the time.

2、 Using VLC,. Net has a VLC.DotNet .

After testing, it can play video and voice normally, but the delay of playing RTMP sound is 7 seconds, and various parameter settings are invalid. As the playing time increases, the delay is more serious, and it can’t be used normally.In addition, when watching the video, you need to save it locally at any time, and you need to request the address separately, which obviously does not meet the requirements.

3、 On the basis of playing video, using sdl2. Net to play sound has no effect.

4、 Refer to network C + + code, write c + + dynamic library, let C # call

   C # call C + + code. First, make a brief summary, and then write a blog. There are two common ways to call it:

1. Call the external API in the pure C + + library, that is, C # uses [dllimport](“ Test.dll “)] mode.

2. Call the managed C + + library, that is, C # direct reference. Add a class in C + + to call the original C + + method, that is, create a class to simply wrap it.

When you start using the first method, there will be the problem of calling video for many times to report errors. Changing to the second method, the new object solves the problem of the first method, but finds many other problems, such as different operating systems need to generate different DLLs, You should not choose the debug version. Otherwise, the performance of different computers will be different. The most important thing is that some memory is not released in time, and the video will collapse after 10 minutes. In short, if you don’t know much about C + +, you will encounter all kinds of problems and be scared.

5、 I found a C + + encapsulated player and streaming device on the Internet, and then provided it to C #.

The player is based on ffmpeg. The player is 30000, and the streaming device is 30000. I think it’s not very cost-effective to check it, because I just have a problem that the sound can’t be played, and the project is not very anxious. I want to challenge myself for a while.

6、 The protagonist goes on the stage and uses FFmpeg.AutoGen To operate the API

      There’s really no better way. I decided to use C #, because there are so few data about ffmpeg using C #. It’s hard to know before I start. If I want to be perfect, I’ll stick to it. It took some time to realize these functions. I’m very proud that the delay of this video player is very low. I feel that the delay is 0. If there is a delay, it is about 1 second. It’s faster than many players on the market, such as potplayer.

 

    After the sixth way is adopted, it is handed over to the customer. The customer is very satisfied, and then the customer deploys it to the customer to run in the production environment. The customer has a problem. The voice sent by Apple mobile phone plays baby voice on the computer. When testing and developing, we use Android mobile phone, because the mobile terminal uses HTML5 to develop and release the app. The same code was not tested carefully with apple. There’s no problem with Android phones. It’s impossible to achieve perfect functions on Apple phones. There are a thousand annimas in my heart. Developers like apple like to do special things. Later, it was found that the sampling rate was different, especially the low sampling rate (32000) of Apple had this problem, and the common problems of 44100 and 48000 had no problem, which was solved after a period of research.

 

I’m going to write a series of articles to summarize the results FFmpeg.AutoGen To achieve push flow and pull flow functions, the functions that have been realized mainly include the following

In terms of streaming:

1、 C # ffmpeg play RTMP video (bitmap or BitmapSource to display image)

2、 C # ffmpeg uses sdl2 to play RTMP video

3、 C? Ffmpeg uses naudio to play RTMP audio.

4、 C? Ffmpeg uses sdl2 to play RTMP audio.

5、 C # ffmpeg realizes RTMP player naudio to play sound and (bitmap or BitmapSource to display image).

6、 Using sdl2 to realize RTMP player in C # ffmpeg

7、 C # ffmpeg local MP3, MP4 file playback (two ways sdl2 and naudio)

8、 C? Ffmpeg plays RTMP video and saves it to local file.

There are two main difficulties in streaming 

1. Player and network audio address are not synchronized

2. C # ffmpeg play local files, using naudio play sound cache problem.

Because the local file parsing is very fast and the cache cleaning is too fast, the playback is incomplete. If the cache is not cleaned, it is easy to explode the memory, such as playing 60 minutes of MP3 files.

In terms of streaming:

1、 Push the remote camera to FFC # RTMP address

2、 Using remote Mac and ffrtmp

3、 C? Ffmpeg uses API to push local file MP4, MKV to RTMP remote address

4、 C # ffmpeg uses API to save local files while pushing stream Preview

5、 C # ffmpeg push stream to realize pause function

 

 

 

 

 

 

 

 

 

Recommended Today

[rust] basic data type

This article introduces the built-in data types provided by rust. Boolean type Boolean typeLogical values representing yes and No. It has two values:trueandfalseGenerally used in logical expressions, and, or, and not operations can be performed: fn main() { let x = true; let y: bool = ! x; // False, inverse operation let z = […]