# Do VideoCapture.read() and VideoWriter.write() interact with each other

**URL:** <https://forum.opencv.org/t/do-videocapture-read-and-videowriter-write-interact-with-each-other/14194>\
**Category:** C++\
**Tags:** videoio\
**Created:** [August 9, 2023, 6:56pm UTC](https://forum.opencv.org/t/do-videocapture-read-and-videowriter-write-interact-with-each-other/14194 "2023-08-09T18:56:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaniMambo](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DaniMambo](https://forum.opencv.org/u/DaniMambo)\
**Post date:** [August 9, 2023, 6:56pm UTC](https://forum.opencv.org/t/do-videocapture-read-and-videowriter-write-interact-with-each-other/14194/1 "2023-08-09T18:56:52Z")

</div>

Hi,

I am having a camera, which I am reading frames from, using **cv::VideoCapture**. Those frames are then saved into a Q, which another Thread is reading from, doing some image modification and writing them into a Video File using **cv::VideoWriter**.

Once upon a time it seems **VideoWriter.write()** and **VideoCapture.read()** happen at the same time. I thought, this should have no interaction, however in these events **VideoCapture.read()** returns false and an empty image (took me quite some time to figure that out). My programm storage for the new image to be read and for the old image to be written is for sure not the same. Is there any interdependency within those two classes, that leads to **VideoCapture.read()** failing, when **VideoWriter.write()** is doing it’s magic?  
I tried to look into the source Code but only found, that both call **CV\_INSTRUMENT\_REGION()**, but did not get very deep, before getting completely confused.

Cheers Daniel

---

<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:** [August 9, 2023, 7:57pm UTC](https://forum.opencv.org/t/do-videocapture-read-and-videowriter-write-interact-with-each-other/14194/2 "2023-08-09T19:57:00Z")

</div>

no, they act independantly of each other.

---

<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:** [August 10, 2023, 5:13am UTC](https://forum.opencv.org/t/do-videocapture-read-and-videowriter-write-interact-with-each-other/14194/3 "2023-08-10T05:13:24Z")

</div>

Except when opening a file, where one waits on the other unless you explicity set `OPENCV_FFMPEG_IS_THREAD_SAFE==true`

> <https://github.com/opencv/opencv/issues/20114>
>
> On windows when opening up RTSP streams with VideoCapture::open() and the FFMPEG… back end, there is normally a delay, say 2000ms while the stream is setup, which is to be expected.
> While this is happening any additional calls to VideoCapture::open() and VideoWriter.open() in other threads are blocked.
> Is this blocking/queuing part of OpenCV or FFMPEG and can it be removed?
> 
> \##### System information (version)
> \- OpenCV =\> 4.5.2
> \- Operating System / Platform =\> Windows 64 Bit
> \- Compiler =\> Visual Studio 2019
> 
> \##### Steps to reproduce
> 
> \<!-- to add code example fence it with triple backticks and optional file extension
> \`\`\`.cpp
> // C++ code example
> \`\`\`
> or attach as .txt or .zip file
> \--\>
> 
> \##### Issue submission checklist
> 
> - \[\* \] I report the issue, it's not a question
> \<!--
> OpenCV team works with forum.opencv.org, Stack Overflow and other communities
> to discuss problems. Tickets with question without real issue statement will be
> closed.
> --\>
> - \[\*\] I checked the problem with documentation, FAQ, open issues,
> forum.opencv.org, Stack Overflow, etc and have not found solution
> \<!--
> Places to check:
> \* OpenCV documentation: https://docs.opencv.org
> \* FAQ page: https://github.com/opencv/opencv/wiki/FAQ
> \* OpenCV forum: https://forum.opencv.org
> \* OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
> \* Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
> --\>
> - \[\] I updated to latest OpenCV version and the issue is still there
> \<!--
> master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
> OpenCV team supports only latest release for each branch.
> The ticket is closed, if the problem is not reproduced with modern version.
> --\>
> - \[\] There is reproducer code and related data files: videos, images, onnx, etc
> \<!--
> The best reproducer -- test case for OpenCV that we can add to the library.
> Recommendations for media files and binary files:
> \* Try to reproduce the issue with images and videos in opencv\_extra repository
> to reduce attachment size
> \* Use PNG for images, if you report some CV related bug, but not image reader
> issue
> \* Attach the image as archive to the ticket, if you report some reader issue.
> Image hosting services compress images and it breaks the repro code.
> \* Provide ONNX file for some public model or ONNX file with with random weights,
> if you report ONNX parsing or handling issue. Architecture details diagram
> from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
> --\>

---

<div class="post-metadata">

**Author:** ![DaniMambo](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DaniMambo](https://forum.opencv.org/u/DaniMambo)\
**Post date:** [August 10, 2023, 7:28am UTC](https://forum.opencv.org/t/do-videocapture-read-and-videowriter-write-interact-with-each-other/14194/4 "2023-08-10T07:28:34Z")

</div>

Thanks for your answers!  
Is there any other possible way **VideoCapture.read()** can fail immediately (without timeout), when it worked several times before and also works thereafter (I am using OpenCV 4.8 on Linux compiled without GStreamer)?  
Also this only happens in the VideoWriter Context. When I modify the Writer Thread and it does not write the images into a Video FIle, but just grabs and discards the images a Million Frames go by just fine. No exception otherwise also. It just returns false every let’s say 500-1000 Frames. Any idea, that could lead me on the right track?

---

<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:** [August 10, 2023, 8:38am UTC](https://forum.opencv.org/t/do-videocapture-read-and-videowriter-write-interact-with-each-other/14194/5 "2023-08-10T08:38:59Z")

</div>

there have been other people who witnessed this “intermittent” failure of VideoCapture.

for now, you could just continue to read, regardless of one read() call’s failure. perhaps give up after several failed calls.
