# How to use FFMPEG of my System with OpenCV?

**URL:** <https://forum.opencv.org/t/how-to-use-ffmpeg-of-my-system-with-opencv/1015>\
**Category:** Uncategorized\
**Tags:** videowriter, ffmpeg\
**Created:** [January 21, 2021, 9:27am UTC](https://forum.opencv.org/t/how-to-use-ffmpeg-of-my-system-with-opencv/1015 "2021-01-21T09:27:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael-gary](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/michael-gary/32/308_2.png) [@Michael-gary](https://forum.opencv.org/u/Michael-gary)\
**Post date:** [January 21, 2021, 9:27am UTC](https://forum.opencv.org/t/how-to-use-ffmpeg-of-my-system-with-opencv/1015/1 "2021-01-21T09:27:05Z")

</div>

Hello community,  
I use openCV with C++ to process images and write them with the `cv::VideoWriter` class.  
It works just fine, as backend I use ` cv::CAP_FFMPEG`.  
My problem here is, sometimes this is a little slow (depending on which codec is used). It bothers my hardware a lot even if I have a nvidia GPU with hardware encoding, because it is not used.  
If I use my FFMPEG of my system in terminal, with the right codec, my GPU is doing the job.  
When I got this right, OpenCV is not using my system FFMPEG, which means it does not even have access to this functions, here is the output of `cv::getBuildInformation()`:

> General configuration for OpenCV 4.5.1 =====================================  
> Version control: unknown
> 
> Extra modules:  
> Location (extra): /home/mic/opencv-4.5.1/opencv\_contrib/modules  
> Version control (extra): unknown
> 
> Platform:  
> Timestamp: 2020-12-28T15:48:46Z  
> Host: Linux 5.4.0-58-generic x86\_64  
> CMake: 3.10.2  
> CMake generator: Unix Makefiles  
> CMake build tool: /usr/bin/make  
> Configuration: RELEASE
> 
> CPU/HW features:  
> Baseline: SSE SSE2 SSE3  
> requested: SSE3  
> Dispatched code generation: SSE4\_1 SSE4\_2 FP16 AVX AVX2 AVX512\_SKX  
> requested: SSE4\_1 SSE4\_2 AVX FP16 AVX2 AVX512\_SKX  
> SSE4\_1 (17 files): + SSSE3 SSE4\_1  
> SSE4\_2 (2 files): + SSSE3 SSE4\_1 POPCNT SSE4\_2  
> FP16 (1 files): + SSSE3 SSE4\_1 POPCNT SSE4\_2 FP16 AVX  
> AVX (5 files): + SSSE3 SSE4\_1 POPCNT SSE4\_2 AVX  
> AVX2 (31 files): + SSSE3 SSE4\_1 POPCNT SSE4\_2 FP16 FMA3 AVX AVX2  
> AVX512\_SKX (7 files): + SSSE3 SSE4\_1 POPCNT SSE4\_2 FP16 FMA3 AVX AVX2 AVX\_512F AVX512\_COMMON AVX512\_SKX
> 
> C/C++:  
> Built as dynamic libs?: YES  
> C++ standard: 11  
> C++ Compiler: /usr/bin/c++ (ver 8.4.0)  
> C++ flags (Release): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG  
> C++ flags (Debug): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D\_DEBUG  
> C Compiler: /usr/bin/cc  
> C flags (Release): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG  
> C flags (Debug): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D\_DEBUG  
> Linker flags (Release): -Wl,–exclude-libs,libippicv.a -Wl,–exclude-libs,libippiw.a -Wl,–gc-sections -Wl,–as-needed  
> Linker flags (Debug): -Wl,–exclude-libs,libippicv.a -Wl,–exclude-libs,libippiw.a -Wl,–gc-sections -Wl,–as-needed  
> ccache: NO  
> Precompiled headers: NO  
> Extra dependencies: dl m pthread rt /usr/lib/x86\_64-linux-gnu/libGL.so /usr/lib/x86\_64-linux-gnu/libGLU.so  
> 3rdparty dependencies:
> 
> OpenCV modules:  
> To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn\_objdetect dnn\_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img\_hash imgcodecs imgproc intensity\_transform line\_descriptor mcc ml objdetect optflow phase\_unwrapping photo plot quality rapid reg rgbd saliency shape stereo stitching structured\_light superres surface\_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto  
> Disabled: world  
> Disabled by dependency: -  
> Unavailable: alphamat cnn\_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv hdf java julia matlab ovis python2 python3 sfm viz  
> Applications: tests perf\_tests apps  
> Documentation: NO  
> Non-free algorithms: YES
> 
> GUI:  
> GTK+: YES (ver 2.24.32)  
> GThread : YES (ver 2.56.4)  
> GtkGlExt: YES (ver 1.2.0)  
> OpenGL support: YES (/usr/lib/x86\_64-linux-gnu/libGL.so /usr/lib/x86\_64-linux-gnu/libGLU.so)  
> VTK support: NO
> 
> Media I/O:  
> ZLib: /usr/lib/x86\_64-linux-gnu/libz.so (ver 1.2.11)  
> JPEG: /usr/lib/x86\_64-linux-gnu/libjpeg.so (ver 80)  
> WEBP: build (ver encoder: 0x020f)  
> PNG: /usr/lib/x86\_64-linux-gnu/libpng.so (ver 1.6.34)  
> TIFF: /usr/lib/x86\_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)  
> JPEG 2000: build (ver 2.3.1)  
> OpenEXR: build (ver 2.3.0)  
> HDR: YES  
> SUNRASTER: YES  
> PXM: YES  
> PFM: YES
> 
> Video I/O:  
> DC1394: YES (2.2.5)  
> FFMPEG: YES  
> avcodec: YES (57.107.100)  
> avformat: YES (57.83.100)  
> avutil: YES (55.78.100)  
> swscale: YES (4.8.100)  
> avresample: YES (3.7.0)  
> GStreamer: YES (1.14.5)  
> v4l/v4l2: YES (linux/videodev2.h)
> 
> Parallel framework: TBB (ver 2017.0 interface 9107)
> 
> Trace: YES (with Intel ITT)
> 
> Other third-party libraries:  
> Intel IPP: 2020.0.0 Gold [2020.0.0]  
> at: /home/mic/opencv-4.5.1/opencv/build\_smi\_pc/3rdparty/ippicv/ippicv\_lnx/icv  
> Intel IPP IW: sources (2020.0.0)  
> at: /home/mic/opencv-4.5.1/opencv/build\_smi\_pc/3rdparty/ippicv/ippicv\_lnx/iw  
> Lapack: NO  
> Eigen: NO  
> Custom HAL: NO  
> Protobuf: build (3.5.1)
> 
> OpenCL: YES (no extra features)  
> Include path: /home/mic/opencv-4.5.1/opencv/3rdparty/include/opencl/1.2  
> Link libraries: Dynamic load
> 
> Python (for build): /usr/bin/python2.7
> 
> Java:  
> ant: NO  
> JNI: NO  
> Java wrappers: NO  
> Java tests: NO
> 
> Install to: /usr/local

For many of the used libraries there is the path of my system included, but not for FFMPEG, there is just written

> YES

So, is it possible to use my System FFMPEG and use the codecs together with my GPU?  
Or possible to use the nvidia GPU for encoding in any other way?  
I was already looking at the [[OpenCV: cv::cudacodec::VideoWriter Class Reference](https://docs.opencv.org/4.5.1/df/dde/classcv_1_1cudacodec_1_1VideoWriter.html)]([https://cudacodec](https://cudacodec) videowriter) but there is written

> Currently only Windows platform is supported.

And Windows is not an option.  
Grateful for your support.  
BR Michael  
.

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [January 21, 2021, 9:58am UTC](https://forum.opencv.org/t/how-to-use-ffmpeg-of-my-system-with-opencv/1015/2 "2021-01-21T09:58:42Z")

</div>

> [@Michael-gary](#):
>
> Or possible to use the nvidia GPU for encoding in any other way?  
> I was already looking at the [[OpenCV: cv::cudacodec::VideoWriter Class Reference](https://docs.opencv.org/4.5.1/df/dde/classcv_1_1cudacodec_1_1VideoWriter.html)]([https://cudacodec](https://cudacodec) videowriter) but there is written

FYI this hasn’t worked on windows for a long time.

---

<div class="post-metadata">

**Author:** ![visualbill](https://avatars.discourse-cdn.com/v4/letter/v/b38774/32.png) [@visualbill](https://forum.opencv.org/u/visualbill)\
**Post date:** [January 21, 2021, 2:01pm UTC](https://forum.opencv.org/t/how-to-use-ffmpeg-of-my-system-with-opencv/1015/3 "2021-01-21T14:01:07Z")

</div>

I have successfully used ffmpeg.exe by forking a process and feeding images to stdin. It is tricky but can be done.

Performance depends on input image size, codec used, hardware acceleration used, and compression chosen. Try less compression to test performance if you are using lossful compression.

Opencv uses a special interface dll to to hook in ffmpeg library. This is a requirement for opencv, but as I mentioned it is possible to grab images and pipe to stdin of an ffmpeg process to compress images into video. With some codecs you must be careful how to finish or you get no video because the to close the video it has to properly write a header at the beginning. FFMPEG does this but it must have the correct close signaling.

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [January 21, 2021, 2:35pm UTC](https://forum.opencv.org/t/how-to-use-ffmpeg-of-my-system-with-opencv/1015/4 "2021-01-21T14:35:07Z")

</div>

I thought it was only windows which used its own shared library built on top of ffmpeg?

If this is not the case then I would imagine you can tweak the [cmake script](https://github.com/opencv/opencv/blob/master/modules/videoio/cmake/detect_ffmpeg.cmake) to use the following variables to achieve what you want  
`-DWITH_FFMPEG=ON -DFFMPEG_ROOT_DIR=<path to compiled libs> -DOPENCV_FFMPEG_SKIP_DOWNLOAD=ON`  
in the same way you can on windows.

---

<div class="post-metadata">

**Author:** ![visualbill](https://avatars.discourse-cdn.com/v4/letter/v/b38774/32.png) [@visualbill](https://forum.opencv.org/u/visualbill)\
**Post date:** [January 21, 2021, 2:42pm UTC](https://forum.opencv.org/t/how-to-use-ffmpeg-of-my-system-with-opencv/1015/5 "2021-01-21T14:42:35Z")

</div>

It is possible that it is windows only using the custom shared library - I have not checked it. I will take a look.

---

<div class="post-metadata">

**Author:** ![Michael-gary](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/michael-gary/32/308_2.png) [@Michael-gary](https://forum.opencv.org/u/Michael-gary)\
**Post date:** [January 21, 2021, 6:09pm UTC](https://forum.opencv.org/t/how-to-use-ffmpeg-of-my-system-with-opencv/1015/6 "2021-01-21T18:09:15Z")

</div>

Hi cudawarped, this sounds good.  
I installed FFMPEG via snapd. Can you pls tell me which “path to compiled libs” I need to specify here.  
I tried to reference

> /snap/ffmpeg/1286/usr/lib

because here are libavcodec.so and so on located. But I am not sure if this is working.  
In the build info there is still

> FFMPEG YES

Without any path to my system libs.  
Can you pls help me on that one?  
BR Michael

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [January 21, 2021, 6:59pm UTC](https://forum.opencv.org/t/how-to-use-ffmpeg-of-my-system-with-opencv/1015/7 "2021-01-21T18:59:37Z")

</div>

I think it would be better to ask someone more familiar with linux as I would assume something like pkg-config would pick up the version of ffmpeg you have installed and the manual root of hacking the cmake file is the wrong way to do things.
