# Python OpenCV pause without ending on error

**URL:** <https://forum.opencv.org/t/python-opencv-pause-without-ending-on-error/2090>\
**Category:** Python\
**Tags:** videoio\
**Created:** [March 10, 2021, 9:34pm UTC](https://forum.opencv.org/t/python-opencv-pause-without-ending-on-error/2090 "2021-03-10T21:34:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfoglio](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mfoglio/32/1368_2.png) [@mfoglio](https://forum.opencv.org/u/mfoglio)\
**Post date:** [March 10, 2021, 9:34pm UTC](https://forum.opencv.org/t/python-opencv-pause-without-ending-on-error/2090/1 "2021-03-10T21:34:17Z")

</div>

I am reading a video using OpenCV:

```auto
def get_frames(video_path):
    # Generate frames
    cap = cv2.VideoCapture(video_path)
    while(cap.isOpened()):
        try:
            ret, frame = cap.read()
            if ret is True:
                yield frame
        except Exception as e:
            print(e)

for _ in get_frames(input_video_path_1):
    # do stuff

```

When running my process I got the following errors:

```auto
[mpeg4 @ 0x7feec69d4400] ac-tex damaged at 21 17
[mpeg4 @ 0x7feec69d4400] Error at MB: 1398

```

The problem is that no python error is raised and therefore I cannot catch the error. The code seems to pause without ending. It just stops there. How can I at least catch the error?

---

<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:** [March 10, 2021, 9:43pm UTC](https://forum.opencv.org/t/python-opencv-pause-without-ending-on-error/2090/2 "2021-03-10T21:43:07Z")

</div>

```auto
...
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened(): return
    while True:
        ret, frame = cap.read()
        if not ret: break
        yield frame
...

```

`isOpened` is checked once, at the beginning. that is sufficient.

you HAVE TO stop reading frames when ret becomes false. that’s how you know the video file has ended. NOT via isOpened

your code “freezes” because you keep reading frames, but ret is false, and you never break the loop.

some very bad advice out there on the internet. unfortunately, some of it is [also in the official OpenCV docs](https://docs.opencv.org/master/dd/d43/tutorial_py_video_display.html)… since someone contributed it back in 2013/2014.

---

<div class="post-metadata">

**Author:** ![mfoglio](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mfoglio/32/1368_2.png) [@mfoglio](https://forum.opencv.org/u/mfoglio)\
**Post date:** [March 11, 2021, 1:37am UTC](https://forum.opencv.org/t/python-opencv-pause-without-ending-on-error/2090/3 "2021-03-11T01:37:13Z")

</div>

Thank you. This makes the code end. That was one of two problem. Now, how can I skip "damaged"frames? Because this way I am not reading the entire video, probably because the following error is thrown:

```auto
mpeg4 @ 0x7fa87c877a00] ac-tex damaged at 78 17
[mpeg4 @ 0x7fa87c877a00] Error at MB: 1455

```

I can confirm that I see the video on VLC and that with the same code I have processed, on the same machine, another video with the same format and encoding (I know it because I generated both video on another machine).

---

<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:** [March 11, 2021, 11:31am UTC](https://forum.opencv.org/t/python-opencv-pause-without-ending-on-error/2090/4 "2021-03-11T11:31:17Z")

</div>

OpenCV is not a media handling library. it isn’t supposed to deal with broken video files.

you can either ignore those warnings and let them scroll by (they come from ffmpeg), or you can use ffmpeg’s libraries directly. have a look at “PyAV”.
