Hello FFMPEG

  • FFMPEG based audio codec (a) : Hello FFMPEG, installation and compilation
  • Audio codec based on FFMPEG (2) : audio decoding
  • Audio codec based on FFMPEG (3) : audio coding

FFmpeg is an open source computer program that can record, convert, and stream digital audio and video

As an audio developer, I have recently been learning how to use FFMPEG for audio codec. Most of the material on the web is based on video, with little introduction on the audio side, or the code is too old to run

After fumbling for some time, I have mastered how to use FFMPEG for audio codec correct posture, recorded in this blog. If you find any mistakes, please comment on them. Thank you very much.

In this article, I will focus on how to install FFEMPG and how to run FFEMPG correctly

The installation

To be exact, install the FFMPEG SDK

In addition to a set of SDKS, FFMPEG includes an executable file that allows you to perform audio and video operations without programming. For example, we can convert the audio format:

ffmpeg -i input.mp3 output.wma
Copy the code

For details on how to use FFMPEG, see the FFMPEG tool or FFMPEG gist

As developers, we need to use FFMPEG in our programs for audio and video codec operations, this is the time to let FFMPEG SDK come on stage. Here’s how to install FFMPEG on different platforms. Since I only have a Mac, I cannot verify the Linux and Windows installations, so this is for reference only.

MacOS

brew install ffmpeg
Copy the code

Linux

  • Linux install ffmpeg

Windows

  • How to install FFMPEG in Linux subsystem WSL under Win

Hello FFMPEG

It is recommended to use Cmake during compilation, so that we can reference FFMPEG headers, libraries, etc.

The code structure is as follows:

├─ cmake ├─ cmake ├─ cmake ├─ cmake ├─ cmake ├─ cmake ├─ cmakeCopy the code

Among them

  • Cmakelists. TXT root cmake file
  • FindFFMPEG. Cmake asfind_packageThe prepared files allow us to easily find FFMPEG related components. I copied this file directly from somewhere else. The original address inhere
  • 0_hello_ffmpeg. CPP source file

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(ffmepg_audio_tutorial)

list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

find_package(FFMPEG REQUIRED)
if(NOT FFMPEG_FOUND)
    message(ERROR "Cannot find ffmpeg")
else(a)message(STATUS "Found ffmpeg")
    message(STATUS "ffmpeg include dir is ${FFMPEG_INCLUDE_DIRS}")
    message(STATUS "ffmpeg libraries are ${FFMPEG_LIBRARIES}")
endif(a)add_executable(hello_ffmpeg 0_hello_ffmpeg.cpp)
target_include_directories(hello_ffmpeg PRIVATE ${FFMPEG_INCLUDE_DIRS})
target_link_libraries(hello_ffmpeg PRIVATE ${FFMPEG_LIBRARIES})
Copy the code

In the above cmake:

  • First of all inCMAKE_MODULE_PATHAdding a search path
  • find_packageTo find information about ffmPEG components and header files

0_hello_ffmpeg.cpp


#if defined(__cplusplus)
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#if defined(__cplusplus)
}
#endif

#include <iostream>

using namespace std;

int main(a)
{
    cout << "avcodec_configuration : " << avcodec_configuration() << endl;
    return 0;
}
Copy the code

In the above code:

  • If compiling in C++, pay attention to useextern "C"Wrap the FFmPEG header file
  • avcodec_configuration()To display ffMPEG build information

If all goes well, run the above code and you get something similar:

avcodec_configuration : --prefix=/usr/local/Cellar/ffmpeg/4.3.1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
Copy the code

The full code has been uploaded to Github. Star: github.com/jiemojiemo/…

conclusion

We looked at the differences between the FFMPEG command line and FFMPEG, and how to install them. Next, use cmake to build the Hello FFMEpg program. If all goes well, you can get the FFMPEG build information.

Reprint please indicate the source: juejin.cn/post/687145…

The resources

  • Ffmpeg Tool
  • ffmpeg gist
  • 【FFmpeg】Hello World! Try how to compile FFmpeg programs