# Cap\_ffmpeg\_impl.hpp changes are not applied to opencv build

**URL:** <https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980>\
**Category:** C++\
**Tags:** ffmpeg, windows, build, videoio\
**Created:** [April 26, 2021, 6:59am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980 "2021-04-26T06:59:26Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![zadok](https://avatars.discourse-cdn.com/v4/letter/z/d2c977/32.png) [@zadok](https://forum.opencv.org/u/zadok)\
**Post date:** [April 26, 2021, 6:59am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/1 "2021-04-26T06:59:26Z")

</div>

I am changed `LIBAVFORMAT_INTERRUPT_OPEN_TIMEOUT_MS` value. However, I do not think it is being applied after I built opencv.

Is there a way to change the interrupt callback timeout for ffmpeg in windows build?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 26, 2021, 7:56am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/2 "2021-04-26T07:56:48Z")

</div>

to my best knowledge, cap\_ffmpeg\_impl code is used for the opencv\_videoio\_ffmpeg.dll, which gets compiled on an external build server,  
and downloaded from there via cmake.  
it is not used when building the opencv libs locally

---

<div class="post-metadata">

**Author:** ![zadok](https://avatars.discourse-cdn.com/v4/letter/z/d2c977/32.png) [@zadok](https://forum.opencv.org/u/zadok)\
**Post date:** [April 26, 2021, 8:54am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/3 "2021-04-26T08:54:29Z")

</div>

Does this mean that there is a pre-built opencv\_ffmpeg.dll somewhere (external)? and the changes to modules/videoio/src/cap\_ffmpeg\_impl.hpp does not matter because it is just a copy from the external source?

---

<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:** [April 26, 2021, 9:16am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/4 "2021-04-26T09:16:18Z")

</div>

Yes that’s correct, the .dll is downloaded by cmake. I found the best way to change anything inside cap\_ffmpeg\_impl is to link against [ffmpeg](https://github.com/BtbN/FFmpeg-Builds/releases) libraries directly but you have to modify the cmake scripts to do this.

---

<div class="post-metadata">

**Author:** ![zadok](https://avatars.discourse-cdn.com/v4/letter/z/d2c977/32.png) [@zadok](https://forum.opencv.org/u/zadok)\
**Post date:** [April 26, 2021, 9:26am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/5 "2021-04-26T09:26:09Z")

</div>

Thank you. This much is clear now.

---

<div class="post-metadata">

**Author:** ![Tykyo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tykyo/32/2206_2.png) [@Tykyo](https://forum.opencv.org/u/Tykyo)\
**Post date:** [May 24, 2021, 2:58pm UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/6 "2021-05-24T14:58:21Z")

</div>

Hello zadok,

Maybe you already found a solution for your problem. If it’s not the case, I share with you a tutorial I made to build the opencv\_videoio\_ffmpeg\*.dll. It’s made to be use by anybody so don’t be mad if you found it too childish.

## Tutorial to compil you own modified version of opencv\_videoio\_ffmpeg\*.dll

**Goal:**  
We will modify the library to change the pixel format used when writing raw video with the fourcc codec 0x00000000 which stand for BI\_RGB label. There is no fourcc label to use this, you need to specify the hex code when creating the VideoWriter, example:

> ```
> cv::VideoWriter outputVideo;
> cv::Size S = cv::Size(800, 600);
> outputVideo.open(
> "out.avi", //output file
> 0x00000000, //fourcc codec hex code [or use label VideoWriter::fourcc('I','4','2','0') = 0x30323449 in hex]
> 24, //framerate (must be greater than 6 for compatibility with most video player)
> S, //the video size
> true //boolean value to set if video is with color (which is the case here)
> );
> 
> ```

In the actual repository maintained by the developper of OpenCV, the 0x000000 codec use by default yuv420p pixel, which for me is a mistake (but who am I to judge).

_Note: 0x000000 can be replace by 0, of course, but I prefer the hex form since it’s the one used by the fourcc web site. And if you want to change the 0x30323449? Uuhh!!! Yeah, me too._

**Prerequisites:**  
Before the modification, you will need a few tools:

1. A linux installation on you windows system : Ubuntu for example in the Microsoft Store.
2. Docker desktop  
—Optional—
3. GitHub Desktop

From Ubuntu, you need to configure git if it’s not already done with:

> ```
> git config --global user.email "You@example.com"
> git config --global user.name "Your Name"
> 
> ```

Before starting to download the source code needed, you need to find the good one for your version of OpenCV. I use the 4.5.0 version released on 11th October 2020 (commit d5fd2f0). The date is important, because you need to download the good branch of opencv\_3rdparty from github (at least the closer one in time).

opencv\_3rdparty : [GitHub - opencv/opencv\_3rdparty at ffmpeg/master\_20200908](https://github.com/opencv/opencv_3rdparty/tree/ffmpeg/master_20200908)  
opencv : [GitHub - opencv/opencv at 4.5.0](https://github.com/opencv/opencv/tree/4.5.0)

For opencv\_3rdparty you can download the zip archive of the branch. No need to clone the repository with git. Extract the zip archive somewhere. Inside the extracted directory (opencv\_3rdparty-ffmpeg-master\_20200908 or opencv\_3rdparty for the remaining of this tuto), there is an empty opencv directory. We will clone opencv inside.  
If you want to git clone opencv\_3rdparty:

> ```
> git clone https://github.com/opencv/opencv_3rdparty.git --single-branch --branch ffmpeg/master_20200908
> 
> ```

For opencv you absolutly need to clone the repository and switch to the good branch.  
So inside you opencv\_3rdparty directory (not the empty opencv), do from the ubuntu command line ( **not the windows command line** )

> ```
> git clone https://github.com/opencv/opencv.git --single-branch --branch 4.5.0
> 
> ```

this will clone to opencv\_3rdparty/opencv and switch to the 4.5.0 branch

Using --single-branch can reduce the total size by two, mostly for opencv\_3rdparty. Don’t try to build the dll with a different version of opencv than the targeted one, because it doesn’t work. Of course the compilation will be fine, but you will have problem when you use the dll. If you choose the wrong version of opencv\_3rdparty, the compilation will probably failed.

**Modification:**  
This part is up to you, you can modify opencv as you which but for opencv\_videoio\_ffmpeg\*.dll, there is only three files inside opencv\_3rdparty/opencv/modules/videoio/src

> ```
> cap_ffmpeg_impl.hpp
> cap_ffmpeg_legacy_api.hpp
> ffmpeg_codecs.hpp
> 
> ```

In my case, to modify the pixel format for 0x00000000 codec, the file is cap\_ffmpeg\_impl.hpp. The modification is:

> ```
> [line 2295]
> case CV_CODEC(CODEC_ID_RAWVIDEO):
> // RGBA is the only RGB fourcc supported by AVI and MKV format
> if(fourcc == CV_FOURCC('R','G','B','A'))
> {
> codec_pix_fmt = AV_PIX_FMT_RGBA;
> }
> + else if(fourcc == 0x00000000)
> + {
> + codec_pix_fmt = AV_PIX_FMT_BGR24;
> + }
> else
> {
> switch(input_pix_fmt)
> {
> case AV_PIX_FMT_GRAY8:
> case AV_PIX_FMT_GRAY16LE:
> case AV_PIX_FMT_GRAY16BE:
> codec_pix_fmt = input_pix_fmt;
> break;
> default:
> codec_pix_fmt = AV_PIX_FMT_YUV420P;
> break;
> }
> }
> [line 2314]
> 
> ```

The four lines added (at position 2301 of the file) start with a ‘+’. As you can see, if the fourcc is not RGBA or the input is not GRAY, the pixel format used by default is AV\_PIX\_FMT\_YUV420P. So I added AV\_PIX\_FMT\_BGR24 for the 0x00000000.

**Building:**  
Aaah, the most important part. First thing to do is to commit you change to the local repository of opencv. It’s realy important, if you not commit the changes, their will not apply to the build. The easiest way is to use GitHub Desktop. Or inside the opencv\_3rdparty/opencv directory from ubuntu command line:

> ```
> git commit -am "modify cap_ffmpeg_impl.hpp for pix_fmt"
> 
> ```

1. Start Docker Desktop if it’s not already running or you will get the following error:

> The command ‘docker’ could not be found in this WSL 2 distro.  
> We recommend to activate the WSL integration in Docker Desktop settings.

1. From Ubuntu, inside the opencv\_3rdparty directory :

> ```
> ffmpeg/build_via_docker.sh
> 
> ```

That’s all 🙂

The resulting dll can be found in opencv\_3rdparty\ffmpeg, the previous one as been replaced.

_Note : during the build, you will see that git package the three .hpp files above, at this stage you can check that inside this archive opencv\_3rdparty\sources\opencv\opencv-videoio-ffmpeg.tar.xz, the files are actually modified. If it’s not the case it’s because you don’t commit you changes._

When you run ffmpeg/build\_via\_docker.sh, if you get ‘: wrong parameter’ or something like that, it’s because you probably clone the opencv\_3rdparty from the windows console. And since it’s windows, the endline is CRLF, not LF. A solution is to run dos2unix on all the file inside opencv\_3rdparty directory.

During the build you can see the below warnings:

> ```
> /app/opencv_ffmpeg.rc:12: digit exceeds base
> /app/opencv_ffmpeg.rc:13: digit exceeds base
> 
> ```

It’s nothing you should care about.

I hope it will help.

---

<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:** [July 27, 2021, 6:40am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/7 "2021-07-27T06:40:49Z")

</div>

Hi Tykyo, I read your guide above and thought I would give it a try. Unfortunately I have never used docker before and I have hit a problem. If I use

```auto
ffmpeg/build_via_docker.sh

```

I get the following error

```auto
=> ERROR [internal] load metadata for docker.io/library/ubuntu:18.04 3.5s
------
 > [internal] load metadata for docker.io/library/ubuntu:18.04:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: rpc error: code = Unknown desc = open /home/b/.docker/.token_seed: permission denied

```

and if I use

```auto
sudo ffmpeg/build_via_docker.sh

```

I get

```auto
Running docker container:

groupadd: GID '0' already exists
useradd: group 'appgroup' does not exist
No passwd entry for user 'appuser'

```

Do you know what could be causing this?

---

<div class="post-metadata">

**Author:** ![Tykyo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tykyo/32/2206_2.png) [@Tykyo](https://forum.opencv.org/u/Tykyo)\
**Post date:** [July 27, 2021, 10:03am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/8 "2021-07-27T10:03:52Z")

</div>

Hi cudawarped,

For the sudo command, it’s normal since the script entry.sh (ffmpeg\docker\entry.sh) try to create a group (and a user) with the id=0 (the root group is 0) because you use sudo. If you don’t use sudo your id is 1000.

For the first part, I don’t know. Can you try to run the following command from the windows command line and from the ubuntu WSL command line?

> docker run --rm --tty --interactive ubuntu:18.04

If it doesn’t work, you best option is to remove docker, restart the computer and make a new install [https://docs.docker.com/docker-for-windows/install/](https://docs.docker.com/docker-for-windows/install/)

Are you on Windows 10 or Linux? If it’s Linux, you need to set permission for your user to use docker without using sudo [https://docs.docker.com/engine/install/linux-postinstall/](https://docs.docker.com/engine/install/linux-postinstall/)

---

<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:** [July 27, 2021, 11:44am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/9 "2021-07-27T11:44:42Z")

</div>

> [@Tykyo](#):
>
> docker run --rm --tty --interactive ubuntu:18.04

Excellent that worked perfectly for me, thank you.

---

<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:** [May 3, 2023, 2:14pm UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/10 "2023-05-03T14:14:26Z")

</div>

> [@Tykyo](#):
>
> docker run --rm --tty --interactive ubuntu:18.04

@Tykyo Not sure if your still around but I’m trying to build the dll again but I’m facing the same issue

> [@cudawarped](#):
>
> ```auto
> Running docker container:
> 
> groupadd: GID '0' already exists
> useradd: group 'appgroup' does not exist
> No passwd entry for user 'appuser'
> 
> ```

Last time I thought it worked when I issued

> [@Tykyo](#):
>
> docker run --rm --tty --interactive ubuntu:18.04

however I’m not so sure now as its not making any difference, have you any futher insight as to what the issue is?

---

<div class="post-metadata">

**Author:** ![Tykyo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tykyo/32/2206_2.png) [@Tykyo](https://forum.opencv.org/u/Tykyo)\
**Post date:** [May 3, 2023, 2:53pm UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/11 "2023-05-03T14:53:29Z")

</div>

Hi cudawarped,

I think you launch the docker container with the sudo command. Or as root. You can’t, because as previously said, the script try to create the user inside de container with the GID=0. And the GID 0 already exist, it’s the root group.

If the error is the first error : “failed to solve with frontend …”, it’s a permission problem for the user to launch docker. The solution can be found on the docker website, inside the help section to install docker on linux system : [https://docs.docker.com/engine/install/linux-postinstall/](https://docs.docker.com/engine/install/linux-postinstall/)

Hope it will help. If it’s not the case, I don’t know how to solve this problem.

Have a nice day

---

<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:** [May 3, 2023, 3:25pm UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/12 "2023-05-03T15:25:28Z")

</div>

Hi Tykyo,

Thanks for the immediate reply. The problem in the end turns out to be “me”, I forgot what I did two years ago, misread your previous reply and have been trying to run the build command as sudo every single time with various docker modifications.

Anyway the solution as you pointed out is to run it as a normal user, I really appreciate you replying so fast, sorry for bothering you.

---

<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:** [May 4, 2023, 8:06am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/14 "2023-05-04T08:06:21Z")

</div>

> [@vidhayakji79](#):
>
> because it doesn’t work. Of course the compilation will be fine, but you will have problem when you use the dll. If you choose the wrong version of opencv\_3rdparty, the compilation will probably fail

Tykyo covered all of this in his first post above

> [@Cap\_ffmpeg\_impl.hpp changes are not applied to opencv build](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/6):
>
> Hello zadok, Maybe you already found a solution for your problem. If it’s not the case, I share with you a tutorial I made to build the opencv\_videoio\_ffmpeg\*.dll. It’s made to be use by anybody so don’t be mad if you found it too childish. Tutorial to compil you own modified version of opencv\_videoio\_ffmpeg\*.dll Goal: We will modify the library to change the pixel format used when writing raw video with the fourcc codec 0x00000000 which stand for BI\_RGB label. There is no fourcc label to us…

I am not sure what extra information your comment provides.

---

<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:** [May 30, 2023, 10:38am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/16 "2023-05-30T10:38:37Z")

</div>

vidhayakji79 is still spamming this post!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 30, 2023, 11:22am UTC](https://forum.opencv.org/t/cap-ffmpeg-impl-hpp-changes-are-not-applied-to-opencv-build/2980/17 "2023-05-30T11:22:36Z")

</div>

spammers are getting creative, dropping generic non-spam posts first, to circumvent early defenses. if you see an obvious spam post (that URL couldn’t be more blatant), just flag it. sometimes, that even hides it temporarily until moderation has passed a verdict.
