Use ffmpeg library under Windows

Today we will talk about how to use the ffmpeg library for audio and video development under Windows. Generally, I rarely use ffmpeg under Windows. The main reason is that compiling ffmpeg under Windows is not as convenient as compiling under Linux/Mac.

However, relatively speaking, there are still many students who use Windows. In order to facilitate them to learn audio and video development more smoothly, today we will take a look at how to build a development environment using ffmpeg on Windows.

Building the ffmpeg development environment under Windows can be divided into the following steps:

  • Compile ffmpeg that can be used by Windows
  • When using VS to create a project, choose the x86 or x64 architecture instruction set
  • Specify the ffmpeg header file directory path
  • Specify the ffmpeg library file directory path
  • Specify the ffmpeg library you want to use
  • Copy the specified ffmpeg DLL library file to the executable program directory

Below we will introduce the above steps in detail.

Compile ffmpeg under Windows

If we want to use ffmpeg, we must have the ffmpeg library that can be used under Windows. You can download this library directly from the official website, or compile it through the ffmpeg source code.

Relatively speaking, the library generated by compiling the ffmpeg source code is more flexible. For example, if you want to add fdk_aac, x264 and other modules, it will be very convenient. Just turn on the option when compiling. If you use the ffmpeg library on the official website, you can only use the default settings. up.

However, compiling the ffmpeg source code under Windows is still a troublesome thing, and a bunch of environments need to be configured. In general, there are three ways to compile ffmpeg under Windows:

  • Cygwin way to compile ffmpeg
  • Msys2 way to compile ffmpeg
  • Msys2 + VS way to compile ffmpeg

Among them, the first two methods are similar, and they are all compiled in the Linux way. The last method is the most complicated, but the ffmpeg compiled in this way is most in line with Windows style.

Since this process is too complicated, I will not introduce it in detail here. Students who are interested can watch my video class , and there is a detailed introduction in the video class.

Choose x86 or x64 in VS

When the ffmpeg library is compiled, how do we use it? Let's take VS as an example to explain.

The first step is to choose whether to use x86 or x64 in the project created by VS. What's the difference? In layman's terms, x86 compiles executable programs for 32-bit systems, while x64 compiles executable programs for 64-bit systems.

Since Windows is backward compatible, the x86 program you compile can run on a 64-bit system. On the contrary, if you choose the output to be x64, it can only run on 64-bit systems, and cannot run on 32-bit systems.

But everything has advantages and disadvantages. If you compile an x86 program, although it can run on an x64-bit system, its running speed is much slower than that of the compiled x64 program. Since most systems are 64-bit at present, I suggest compiling into x64 programs as much as possible.

Another thing to note is that when we choose x86 or x64, we should also pay attention to whether it is consistent with the ffmpeg we compiled earlier? If your ffmpeg compiles x86, you can only choose to output an x86 program in VS.

Specify the ffmpeg header file path

After choosing the output as x86 or x64, we need to specify the header file path of ffmpeg for the project.

In fact, whether it is compiled by VS or gcc/clang under Linux/Mac, they all follow the same principle when compiling.

Let's give an example. If you are very familiar with Linux, you should know that the command to compile code under Linux system is as follows:

gcc -g -o executable source code -I xxxx -L xxxx -lxxx

Among them, -I specify the header file path of the library you want to reference; -Lspecify the path of the library you use;  -l specify which library to use at the time.

VS is no exception, let's see where Windows sets the ffmpeg header file, as shown in the figure below:

The first step is to find your project

The second step, right click on the project

Step 3, click Properties

The fourth step, modify the C++/General in Additional Include Directories

So far, we have specified the header file path of ffmpeg.

Specify the ffmpeg library path

Specifying the ffmpeg library file path is similar to specifying the ffmpeg header file path. The process is as follows:

  • First right click on the project -> properties.
  • Then select the Linker item in the pop-up property window, as shown in the figure:

Additional Library Directories Add ffmpeg library path in In this way, the ffmpeg library file path is set.

Specify the ffmpeg library file to use

Next, let's take a look at how to specify the ffmpeg library file to use. The compiled ffmpeg has many library files, such as libavutil, libavformat ..., each library has its specific function, if you are interested, you can read my other course "Intensive Lectures on ffmpeg " .

In my example, because I use an audio device, I need to introduce avdevice, and I want to encode the collected audio, so I also need avcodec and so on. So I need to add the following libraries to my project: avutil, avformat, avdevice, avcodec and swresample. Let's see how to set it up.

Copy the DLL file to the execution directory

The last step is to put the compiled ffmpeg library file in the directory of the compiled executable file. Directly on the picture:

After the above steps, we have set up the audio and video environment using ffmpeg under Windows. Hope this article can help you!

References

Introduction to Audio and Video Xiaobai System
ffmpeg Intensive Lecture

 

Using the ffmpeg library under the original Windows-Knowledge 

 

★The business card at the end of the article can receive audio and video development learning materials for free, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.

see below!

 

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/132050576