DEV Community

Cover image for How to use FFmpeg with C++ (Windows and GNU/Linux)
Marcos Oliveira
Marcos Oliveira

Posted on

How to use FFmpeg with C++ (Windows and GNU/Linux)

📺 I Created a Dynamic Library with C++ for the FFmpeg C API to Make Integration Easier and Faster for Graphical Applications.


ffpp is a dynamic library written in C++ with an API for most major tasks using FFmpeg. Much faster for GUI applications than using processes.

ffpp

Running on Windows.


ffpp

Running on GNU/Linux.


Usage on Windows

Requires Clang

  1. Download libffppwin
Invoke-WebRequest -Uri "https://bit.ly/libffppwin" -OutFile "libffppwin.rar" 
Enter fullscreen mode Exit fullscreen mode
  1. Extract the .rar

  2. Enter the folder:

cd .\libffppwin 
Enter fullscreen mode Exit fullscreen mode
  1. Create a basic code, e.g., main.cpp:
#include "ffpp/ffpp.hpp" #include <memory>  int main(){ auto ffpp = std::make_unique<FFPP>(); std::cout << "Duration: " << ffpp->ffpp_info(FFPP_INFO::DURATION, "video.mp4") << '\n'; } 
Enter fullscreen mode Exit fullscreen mode

Optional test video: video.mp4

  1. Compile and run:
# PowerShell pwsh build.ps1 main.cpp # Or more files # Or: Windows [PowerShell](https://terminalroot.com/tags#powershell) powershell build.ps1 main.cpp # Or more files # Or directly .\build.ps1 main.cpp # Or more files 
Enter fullscreen mode Exit fullscreen mode

If Windows Defender blocks execution, allow the script:

pwsh -ExecutionPolicy Bypass -File build.ps1 main.cpp # Or more files 
Enter fullscreen mode Exit fullscreen mode

The build.ps1 script compiles and runs the generated binary. Output example: Duration: 00:00:05

To build the libffmpeg and compile the DLL from scratch, check: build-win.md


On GNU/Linux

Dependencies:

On Arch:

sudo pacman -S gcc ffmpeg make cmake pkg-config git 
Enter fullscreen mode Exit fullscreen mode

On Debian/Ubuntu/Mint:

sudo apt install build-essential ffmpeg make cmake pkg-config git 
Enter fullscreen mode Exit fullscreen mode

Build and install

Run all the commands below:

git clone https://github.com/terroo/ffpp cd ffpp cmake . -B build cmake --build build sudo cmake --install build # Important after install # Something like: export LD_LIBRARY_PATH=/usr/local/lib # But system-wide: echo /usr/local/lib | sudo tee /etc/ld.so.conf.d/ffpp.conf sudo ldconfig 
Enter fullscreen mode Exit fullscreen mode

Create a basic code, e.g., main.cpp:

#include <ffpp/ffpp.hpp> #include <memory>  int main(){ auto ffpp = std::make_unique<FFPP>(); std::cout << "Duration: " << ffpp->ffpp_info(FFPP_INFO::DURATION, "video.mp4") << '\n'; } 
Enter fullscreen mode Exit fullscreen mode

Optional test video: video.mp4

Compile and run:

g++ main.cpp -lavformat -lavcodec -lavutil -lswscale -lffpp ./a.out 
Enter fullscreen mode Exit fullscreen mode

Example output: Duration: 00:00:05.


API Examples

Assuming you created the object on the heap (auto ffpp = std::make_unique<FFPP>();).

01. Convert MP4 to WMV:

ffpp->ffpp_convert("video.mp4", "new.wmv"); 
Enter fullscreen mode Exit fullscreen mode

Converts only between these video formats: .mp4, .flv, .wmv, .mov


02. Extract frames:

ffpp->ffpp_extract_frames("video.mp4", "my_frames_dir"); 
Enter fullscreen mode Exit fullscreen mode

.ppm images will be created inside the given folder (my_frames_dir/).


03. Get video information:

std::cout << "Duration: " << ffpp->ffpp_info(FFPP_INFO::DURATION, "video.mp4") << '\n'; std::cout << "Bitrate: " << ffpp->ffpp_info(FFPP_INFO::BITRATE, "video.mp4") << '\n'; std::cout << "FPS: " << ffpp->ffpp_info(FFPP_INFO::FPS, "video.mp4") << '\n'; std::cout << "Audio Frequency: " << ffpp->ffpp_info(FFPP_INFO::AUDIO_FREQUENCY, "video.mp4") << '\n'; std::cout << "Resolution: " << ffpp->ffpp_info(FFPP_INFO::RESOLUTION, "video.mp4") << '\n'; 
Enter fullscreen mode Exit fullscreen mode

04. Cut a video from a time point with specific duration:

ffpp->ffpp_cut("video.mp4", "00:00:10", 6, "output.mp4"); 
Enter fullscreen mode Exit fullscreen mode

Cuts the video starting at 10 seconds for a duration of 6 seconds.


📹 Watch the Video

https://youtu.be/3bm84QckF8E

The video is in Brazilian Portuguese, but you can enable YouTube auto-translation or audio track (if available).


👀 See Also


Top comments (0)