C++ wrapper for FFmpeg

AvCpp Build Status Language grade: C/C++ PRs Welcome

Wrapper for the FFmpeg that simplify usage it from C++ projects.

Currently covered next functionality:

  • Core helper & utility classes (AVFrame -> av::AudioSample & av::VideoFrame, AVRational -> av::Rational and so on)
  • Container formats & contexts muxing and demuxing
  • Codecs & codecs contexts: encoding and decoding
  • Streams (AVStream -> av::Stream)
  • Filters (audio & video): parsing from string, manual adding filters to the graph & other
  • SW Video & Audio resamplers

You can read the full documentation here.

Requirements

  • FFmpeg >= 2.0
    • libavformat >= 54.x.x
    • libavcodec >= 54.x.x
    • libavfilter >= 3.x.x
    • libavutil >= 51.x.x
    • libswscale >= 2.x.x
    • libswresample >= 0.x.x
    • libpostproc >= 52.x.x
  • GCC >= 5.0 (C++11 is required)
  • CMake (> 3.11) or Meson(> 50.0)

Debian, Ubuntu 19.10 and Linux Mint 20.x or newer

You should install FFmpeg packages from the deb-multimedia.org site:

sudo apt-get install libavformat-dev \
                     libavcodec-dev \
                     libavutil-dev \
                     libavfilter-dev \
                     libswscale-dev \
                     libswresample-dev \
                     libpostproc-dev \
                     libavdevice-dev

Note 1: I did not test building on Debian.

Note 2: Debian Wheezy repo contains only FFmpeg 1.0.8. I tested building only with 2.x. So it is strongly recoment use Wheezy back-ports repo.

Ubuntu 18.04 and Linux Mint 19.x

If you are on Ubuntu bionic or Linux Mint 19.x you should add ffmpeg-4 PPA:

sudo add-apt-repository ppa:jonathonf/ffmpeg-4 -y
sudo apt update && sudo apt upgrade

After that just install the same packages as above.


Build

There are two ways to compile eitehr with CMake or with meson. By default meson is faster, but if your project uses CMake, those instructions might be better for integration.

Building with CMake

git clone --recurse-submodules https://github.com/h4tr3d/avcpp.git avcpp-git
cd avcpp-git
mkdir build
cd build
cmake ..
make -j8

If your Git version so old (refer to the SO for clarification) you can just replace --recurse-submodules with pair of git submodule init && git submodule update.

If FFmpeg located in non-standard place:

cmake -DPC_FFMPEG_LIBRARY_DIRS=<some_path> -DPC_FFMPEG_INCLUDE_DIRS=<some_path> ..

To point install prefix:

cmake -DCMAKE_INSTALL_PREFIX=/usr ..

Install:

sudo make install

or (for packaging)

sudo make DESTDIR=<some_prefix> install

Refer to CMake documentation for more details that can cover some special cases.

Building with meson

Before you can begin with the building you have to clone the repository like this:

git clone https://github.com/h4tr3d/avcpp.git avcpp-git
cd avcpp-git

IDE Integration:

There are extentions for various IDEs like VS Code/Codium, Eclipse, Xcode, etc. Refer to the docs for more information.

Building the project:

If you don't have the dependencies installed, meson will download and compile them. Because ffmpeg is so large (~2000 c files), you should consider using your package manager to install them. You can then build the project with the following commands:

mkdir build
cd build
meson ..
meson compile

Configuring the project:

By default the sample projects and the test are compiled. If you don't want this you can disable it with the following commands:

meson configure -Dbuild_tests=false
meson configure -Dbuild_samples=false

You can set the install prefix using meson --prefix <your/own/prefix>. To see all of the available options just type meson configure and meson configure --help to get more insormation.

Installing the project:

Just type meson install and the project will be installed in the configured prefix (/usr/local by default).

Running the tests:

To run the test just use meson test. If you disabled the test this will do nothing.

Owner
Comments
  • bytesPerSample or sampleBitDepth return confusing results for audio

    bytesPerSample or sampleBitDepth return confusing results for audio

    I'm using avcpp and I really like it! I'm decoding mp4 (h264+acc) and aac decoder returns AV_SAMPLE_FMT_FLTP (planar floats). In my case I have stereo aac and when I get a decoded audio av::AudioSamples frame values that I get for samples count size etc are all confusing.

    these are the values that I have:

    aframe.raw.nb_samples 1024
    aframe.channelsCount() => 2
    aframe.size() => 16384
    aframe.sampleFormat().bytesPerSample() => 1 (?!)
    aframe.sampleBitDepth() => 8 (?!)
    aframe->linesize[0] = 8192
    

    aframe.raw.buf[0] and buf[1] both have size 8192, however, looking at the memory in debugger it seems that samples actually take half of that buffer. Either it's ffmpeg allocates more (padding, rounding) or it's a bug? For 1024 FLTP sample it should be 4K in each plane or total 8K for 2 channels.

    IMO, aframe.size() should return size of actual samples stored, and but currently it seem to return size of allocated memory. In any case, bytes per sample seems to be completely wrong there (it should be 32 or 64 if a sample is counted for all channels).

  • Examples don't work

    Examples don't work

    Hi! I wonder whether the examples are supposed to work in the master branch. I'm trying the api2-decode-encode-video.cpp

    There might be a problem in the Stream::mediaType() method, since m_raw->codecpar doesn't seem to contain valid data, while m_raw->codec has it, but the former is chosen by preprocessor directive.

    Additionally I don't see code where you set the width/height/pix_fmt fields of AVStream's codec/codecpar. Having these unset causes an error like this: [mp4 @ 000001bae753ba00] dimensions not set while calling octx.writeHeader(); line of api2-decode-encode-video.cpp

    I want to use avcpp in my project and willing to contribute the hardware-accelerated encoding code, but with these basic problems it might not be possible. Am I doing something wrong?

    Libavformat version 57.66.102, target platform Windows 10 64-bit (visual studio toolchain is used)

  • Revert

    Revert "Adjust to ffmpeg avformat 59 API changes."

    Reverts h4tr3d/avcpp#75

    PR https://github.com/h4tr3d/avcpp/pull/75 broke compilation of avcpp:

    ../../../avcpp/src/codec.cpp:131:40: error: invalid conversion from const AVOutputFormat* to AVOutputFormat* [-fpermissive]
    
  • added meson support

    added meson support

    This pull request adds full support for use with the meson build system.

    All the dependencies are automatically checked and if required even compiled.

    It is possible to enable or disable the build for the sample code and the tests.

    The library, samples and tests are subprojects. Since all subprojects are required to be in the subprojects folder there are symbolic links to the actual location of the folder.

    For meson the three test sources where split in individual test programs that are called if meson test is called. If one test function fails, it will be reported that 2 out of 3 test were successful.

    The readme was updated with instructions for using meson and the syntax problem in it where fixed.

  • MSVC compatibility

    MSVC compatibility

    The changes in this PR change some parts of the code to be compatible with MSVC. I haven't tested it on gcc for cross compatibility, but it should be fine.

    I'm not happy about the AV_TIMEBASE_Q_CPP macro, but I couldn't think of a properly elegant solution.

  • Allow to get/set average frame rate

    Allow to get/set average frame rate

    This is important for containers with predefined timebase like Matroska with fixed 1 ms timebase. Without setting the average FPS manually you'll get 1000 fps (or 1k) as a result. It's usually not the true FPS but rather a best guess by the tools like ffprobe. Still, it looks ugly and can confuse less intelligent tools like TV software. Note that setFrameRate() doesn't help in this case hence introducing a new method.

  • ffmpeg package issues

    ffmpeg package issues

    Hello, I want to compile avcpp in Windows. Can you support the PkgConfig module lookup for Windows FFmpeg? cmake tool reported wrong:

    Call Stack (most recent call first): CMake Error at cmake/FindFFmpeg.cmake:186 (set_target_properties): set_target_properties called with incorrect number of arguments. Call Stack (most recent call first): CMakeLists.txt:20 (find_package) CMake Error at D:/develop/CMake/share/cmake-3.14/Modules/FindPackageHandleStandardArgs.cmake:137 (message): Could NOT find FFmpeg (missing: FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS AVCODEC_LIBRARIES AVCODEC_INCLUDE_DIRS AVFORMAT_LIBRARIES AVFORMAT_INCLUDE_DIRS AVUTIL_LIBRARIES AVUTIL_INCLUDE_DIRS AVDEVICE_LIBRARIES AVDEVICE_INCLUDE_DIRS AVFILTER_LIBRARIES AVFILTER_INCLUDE_DIRS SWSCALE_LIBRARIES SWSCALE_INCLUDE_DIRS SWRESAMPLE_LIBRARIES SWRESAMPLE_INCLUDE_DIRS) Call Stack (most recent call first): D:/develop/CMake/share/cmake-3.14/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE) cmake/FindFFmpeg.cmake:204 (find_package_handle_standard_args) CMakeLists.txt:20 (find_package)

  • rfc // cmake // use cmake 3 for build

    rfc // cmake // use cmake 3 for build

    Can library be switched to cmake v3.1 ?

    cmake_minimum_required (VERSION 3.1)

    and re-use new features like

    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED yes)
    

    etc.

  • This fixes compilation in OSX.

    This fixes compilation in OSX.

    The required flag in clang to enable C++11 is different than gcc as also there was an implicit include (i.e. std::string) in GNU libs.

    Environment: XCode 7.2, OSX 10.11 (El Capitan).

  • initial doxyfile

    initial doxyfile

    This adds a simple doxyfile to build html documentation. #67

    If you enable github pages, I can write a small github actions ci to automatically publish the documentation on every commit to master

  • recursive cloning

    recursive cloning

    Hello! Firstly thanks a lot for a great work. I have a little remark -- I think git cloning should be made in recursive way to clone catch2, isn't It? Otherwise during cmake It will crash in some cases and give something like

    CMake Error at tests/CMakeLists.txt:20 (include): include could not find load file:

    Catch
    

    Best regards, Nadya

  • CMake: INTERFACE_LIBRARY targets may only have whitelisted properties.

    CMake: INTERFACE_LIBRARY targets may only have whitelisted properties.

    When following the build instructions, the following errors appear:

    -- The CXX compiler identification is GNU 9.4.0
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Looking for C++ include pthread.h
    -- Looking for C++ include pthread.h - found
    -- Performing Test CMAKE_HAVE_LIBC_PTHREAD
    -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
    -- Check if compiler accepts -pthread
    -- Check if compiler accepts -pthread - yes
    -- Found Threads: TRUE  
    -- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
    -- Checking for module 'libavcodec'
    --   Found libavcodec, version 58.54.100
    -- Checking for module 'libavformat'
    --   Found libavformat, version 58.29.100
    -- Checking for module 'libavdevice'
    --   Found libavdevice, version 58.8.100
    -- Checking for module 'libavutil'
    --   Found libavutil, version 56.31.100
    -- Checking for module 'libavfilter'
    --   Found libavfilter, version 7.57.100
    -- Checking for module 'libswscale'
    --   Found libswscale, version 5.5.100
    -- Checking for module 'libpostproc'
    --   Found libpostproc, version 55.5.100
    -- Checking for module 'libswresample'
    --   Found libswresample, version 3.5.100
    -- Libs: avcodec | avcodec
    CMake Error at cmake/FindFFmpeg.cmake:166 (set_target_properties):
      INTERFACE_LIBRARY targets may only have whitelisted properties.  The
      property "IMPORTED_LINK_INTERFACE_MULTIPLICITY" is not allowed.
    Call Stack (most recent call first):
      CMakeLists.txt:31 (find_package)
    
    
    -- Libs: avformat | avformat
    CMake Error at cmake/FindFFmpeg.cmake:166 (set_target_properties):
      INTERFACE_LIBRARY targets may only have whitelisted properties.  The
      property "IMPORTED_LINK_INTERFACE_MULTIPLICITY" is not allowed.
    Call Stack (most recent call first):
      CMakeLists.txt:31 (find_package)
    
    
    -- Libs: avutil | avutil
    CMake Error at cmake/FindFFmpeg.cmake:166 (set_target_properties):
      INTERFACE_LIBRARY targets may only have whitelisted properties.  The
      property "IMPORTED_LINK_INTERFACE_MULTIPLICITY" is not allowed.
    Call Stack (most recent call first):
      CMakeLists.txt:31 (find_package)
    
    
    -- Libs: avdevice | avdevice
    CMake Error at cmake/FindFFmpeg.cmake:166 (set_target_properties):
      INTERFACE_LIBRARY targets may only have whitelisted properties.  The
      property "IMPORTED_LINK_INTERFACE_MULTIPLICITY" is not allowed.
    Call Stack (most recent call first):
      CMakeLists.txt:31 (find_package)
    
    
    -- Libs: avfilter | avfilter
    CMake Error at cmake/FindFFmpeg.cmake:166 (set_target_properties):
      INTERFACE_LIBRARY targets may only have whitelisted properties.  The
      property "IMPORTED_LINK_INTERFACE_MULTIPLICITY" is not allowed.
    Call Stack (most recent call first):
      CMakeLists.txt:31 (find_package)
    
    
    -- Libs: swscale | swscale
    CMake Error at cmake/FindFFmpeg.cmake:166 (set_target_properties):
      INTERFACE_LIBRARY targets may only have whitelisted properties.  The
      property "IMPORTED_LINK_INTERFACE_MULTIPLICITY" is not allowed.
    Call Stack (most recent call first):
      CMakeLists.txt:31 (find_package)
    
    
    -- Libs: swresample | swresample
    CMake Error at cmake/FindFFmpeg.cmake:166 (set_target_properties):
      INTERFACE_LIBRARY targets may only have whitelisted properties.  The
      property "IMPORTED_LINK_INTERFACE_MULTIPLICITY" is not allowed.
    Call Stack (most recent call first):
      CMakeLists.txt:31 (find_package)
    
    
    -- Found FFmpeg: /usr/lib/x86_64-linux-gnu/libavcodec.so;avcodec;/usr/lib/x86_64-linux-gnu/libavformat.so;avformat;/usr/lib/x86_64-linux-gnu/libavutil.so;avutil;/usr/lib/x86_64-linux-gnu/libavdevice.so;avdevice;/usr/lib/x86_64-linux-gnu/libavfilter.so;avfilter;/usr/lib/x86_64-linux-gnu/libswscale.so;swscale;/usr/lib/x86_64-linux-gnu/libswresample.so;swresample  
    -- LIBS:  / 
    -- /home/teo/tly/avcpp-git/tests/../catch2/contrib
    -- Configuring incomplete, errors occurred!
    

    Reverting commit 4629a902 fixes the problem and avcpp compiles successfully. I'm using CMake 3.16.3.

  • Create aggregate headers roughly corresponding to FFmpeg's libraries

    Create aggregate headers roughly corresponding to FFmpeg's libraries

    FFmpeg provides aggregate headers, which include all relevant code corresponding to that particular library. Examples include libavformat/avformat.h and libavcodec/avcodec.h. It would be convenient to provide similar aggregate headers here.

  • Why do you use new Codec() object in examples?

    Why do you use new Codec() object in examples?

    Hello! I am examining your project and decode/encode examples.

    I found some strange places in the examples

    Decode:

        if (vst.isValid()) {
            vdec = VideoDecoderContext(vst);
            vdec.setRefCountedFrames(true);
    
            vdec.open(Codec(), ec);
            if (ec) {
                cerr << "Can't open codec\n";
                return 1;
            }
        }
    

    During initialization of VideoDecoderContext your code initializes Codec object and sets the code to the context

        } else {
            m_raw->codec_id   = !codec.isNull() ? codec.raw()->id : AV_CODEC_ID_NONE;
            m_raw->codec_type = type;
            m_raw->codec      = codec.raw();
    
            if (!codec.isNull()) {
                if (codec.raw()->pix_fmts != 0)
                    m_raw->pix_fmt = *(codec.raw()->pix_fmts); // assign default value
                if (codec.raw()->sample_fmts != 0)
                    m_raw->sample_fmt = *(codec.raw()->sample_fmts);
            }
        }
    

    After that you call open() function with new object Codec().

    However, in the example from ffmpeg documentation the codec initialized with avcodec_find_decoder is passed to the avcodec_open2.

    The similar strange thing is with encode example:

        OutputFormat  ofrmt;
        FormatContext octx;
    
        ofrmt.setFormat(string(), out);
        octx.setFormat(ofrmt);
    
        Codec        ocodec  = findEncodingCodec(ofrmt);
        Stream      ost     = octx.addStream(ocodec);
        VideoEncoderContext encoder {ost};
    ....
        encoder.open(Codec(), ec);
        if (ec) {
            cerr << "Can't opent encodec\n";
            return 1;
        }
    

    You create ocodec, but pass to encoder some new object.

    Could you please explain why do you use uninitialized Codec object when you open decoder and encoder ?

  • how can i use avcpp to push h264 rtmp stream to web server?

    how can i use avcpp to push h264 rtmp stream to web server?

    i test api2-decode-encode-video example, i use this command : ./api2-decode-encode-video rtmp://xx.xx.xx.xx:1935/livetv/stream rtmp://127.0.0.1/live/stream then i got error: terminate called after throwing an instance of 'av::Exception' what(): Codec context invalid media type

    question: how can i realize the function to PUSH RTMP Stream to some rtmp server? please help !

Related tags
FFmpeg library inspector

ffmpeg-lib-info Prints the license, version, and configuration for FFmpeg libraries.

Jun 7, 2021
ffmpeg supporting EVC codec and file formats.

ffevc ffmpeg supporting EVC codec and file formats. MPEG-5 Essential Video Coding (EVC) integration with FFmpeg project. It is supported under Linux a

Nov 23, 2022
FFmpeg coding tutorial - learn how to code custom transmuxing, transcoding, metadata extraction, frame-by-frame reading and more

FFmpeg code examples FFmpeg coding tutorial - learn how to code custom transmuxing, transcoding, metadata extraction, frame-by-frame reading and more

Nov 29, 2022
Implement a universal video player based on FFmpeg

qiaopcmusic 实现一个万能视频播放器 添加依赖方式: To get a Git project into your build: Step 1. Add the JitPack repository to your build file Add it in your root build.

Oct 15, 2021
Custom FFMpeg build script which support cross-compile to macOS/iOS/android

FFmpegCompileScript Custom FFMpeg build script which support cross-compile to macOS/iOS/Android Usage: First, init all submodule: git submodule update

Apr 20, 2022
FFmpeg powered audio player in node.js

sange FFmpeg powered audio player in node.js prerequisites node.js cmake sudo apt install cmake c++ compiler sudo apt install g++ gcc ffmpeg sudo apt

Nov 25, 2022
A UE4 plugin used ffmpeg library to record/publish game viewport output and in-game audio.

Streaming A UE4 plugin used ffmpeg library to record/publish game viewport output and in-game audio. GPL license beacuse used ffmpeg. Structures GameV

Aug 3, 2022
PowerVS is a standalone broadcast video server based on ffmpeg
PowerVS is a standalone broadcast video server based on ffmpeg

PowerVS PowerVS is a standalone broadcast video server based on ffmpeg ?? Table of Contents About Getting Started Running the tests Usage Development

Mar 17, 2022
AAAAnimation - An ASCII Art animation program working with FFmpeg and Qt. Some code is written in assembly.
AAAAnimation - An ASCII Art animation program working with FFmpeg and Qt. Some code is written in assembly.

AAAAnimation About This is a term project of the Assembly Language and System Programming course in NCU. It decodes video with FFmpeg and plays ASCII

May 25, 2022
Example how to use ffmpeg to decode video file.

FFMpeg-decode-example Example how to use ffmpeg to decode video file. Link to article about decode with FFMpeg. Russian article. Example shows you the

Nov 16, 2022
C++ wrapper for FFmpeg

AvCpp Wrapper for the FFmpeg that simplify usage it from C++ projects. Currently covered next functionality: Core helper & utility classes (AVFrame ->

Nov 28, 2022
nvidia nvmpi encoder for streamFX and obs-studio (e.g. for nvidia jetson. Requires nvmpi enabled ffmpeg / libavcodec)

nvmpi-streamFX-obs nvidia nvmpi encoder for streamFX and obs-studio (e.g. for nvidia jetson. Requires nvmpi enabled ffmpeg / libavcodec) Purpose This

Jun 25, 2022
FFmpeg library inspector

ffmpeg-lib-info Prints the license, version, and configuration for FFmpeg libraries.

Jun 7, 2021
FFVideo - an example FFmpeg lib, and wxWidgets Player with video filters and face detection
 FFVideo - an example FFmpeg lib, and wxWidgets Player with video filters and face detection

An example FFmpeg lib, and wxWidgets Player application with video filters and face detection, it is a no-audio video player intended for video experiments and developers learning how to code media applications.

Mar 26, 2022
ffmpeg supporting EVC codec and file formats.

ffevc ffmpeg supporting EVC codec and file formats. MPEG-5 Essential Video Coding (EVC) integration with FFmpeg project. It is supported under Linux a

Nov 23, 2022
FFmpeg coding tutorial - learn how to code custom transmuxing, transcoding, metadata extraction, frame-by-frame reading and more

FFmpeg code examples FFmpeg coding tutorial - learn how to code custom transmuxing, transcoding, metadata extraction, frame-by-frame reading and more

Nov 29, 2022
Implement a universal video player based on FFmpeg

qiaopcmusic 实现一个万能视频播放器 添加依赖方式: To get a Git project into your build: Step 1. Add the JitPack repository to your build file Add it in your root build.

Oct 15, 2021
Custom FFMpeg build script which support cross-compile to macOS/iOS/android

FFmpegCompileScript Custom FFMpeg build script which support cross-compile to macOS/iOS/Android Usage: First, init all submodule: git submodule update

Apr 20, 2022
FFmpeg powered audio player in node.js

sange FFmpeg powered audio player in node.js prerequisites node.js cmake sudo apt install cmake c++ compiler sudo apt install g++ gcc ffmpeg sudo apt

Nov 25, 2022