Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with RTSP Stream Stuck on Last Frame #50

Open
abdul3909 opened this issue Aug 27, 2024 · 13 comments
Open

Issue with RTSP Stream Stuck on Last Frame #50

abdul3909 opened this issue Aug 27, 2024 · 13 comments

Comments

@abdul3909
Copy link

abdul3909 commented Aug 27, 2024

Hello,

My RTSP stream gets stuck on the last frame and doesn’t release or return false. Is there a solution for this? I am getting a stream via RTSP from VMS, which provides rtsp links of 30-minute videos.
The issue is the same on VideoCaptureStream and VideoCaptureStreamRT.

@chenxinfeng4
Copy link
Owner

Did you configure your URL in proper way? I used the VideoCaptureStreamRT for many projects, including RTSP and the VMS, no problem.

Check the URL before you use ffmpegcv.

# in bash or CMD
ffprobe YOUR_URL #make sure it has correct outcome
ffprobe rtsp://10.50.60.6:8554/mystream_preview   # stream with no passwd protect
ffprobe rtsp://admin:PASSWD@192.168.1.142:554/Streaming/Channels/102    # IP CAM
# in python
import ffmpegcv
vid = ffmpegcv.ReadLiveLast(ffmpegcv.VideoCaptureStreamRT, URL)
ret, frame = vid.read()

@abdul3909
Copy link
Author

image
My ffprobe seems good(sc attached)and if I use opencv cv2.VideoCapture(), it works well and closes the stream as soon as it is ended but ffmpegcv is stuck on the last frame.

@chenxinfeng4
Copy link
Owner

chenxinfeng4 commented Aug 27, 2024

I see, you question is

ffmpegcv can open and read the live stream, but when the stream is closed that the ffmpegcv will get stuck.

That's indeed the issue because I didn't handle the 'close event' yet. My assumption for the VideoCaptureStream(RT) is that the stream source will not be closed, the source will keep live forever.

@abdul3909
Copy link
Author

Is there any temporary solution for this considering openCV is not processing the H264/H265 videos properly even with ffmpeg backend, ffmpegcv works well but the stream does not close.

Repository owner deleted a comment Aug 27, 2024
@chenxinfeng4
Copy link
Owner

chenxinfeng4 commented Aug 27, 2024

You can probably remove the wrapper of 'ReadLiveLast'. But make sure the video processing speed is fast enough to consume the frames in buffer, otherwise the application will get unexpected result.

import ffmpegcv
vid = ffmpegcv.VideoCaptureStreamRT(URL)
ret, frame = vid.read()

Or, you can close reading the stream in specific iteration

for i in range(3000):
    vid.read()
vid.release()

Thank's for pointing out this issue. Quite busy now, I'll fixed it in next release in weeks.

@abdul3909
Copy link
Author

Thank you!
I will be waiting for the new release as the VideoCaptureStreamRT also didn't work.

@chenxinfeng4
Copy link
Owner

chenxinfeng4 commented Aug 28, 2024

Did you update to the lastest version (released in weeks ago, version 0.3.15). Seems that issue has already been handled in the lastest version.

pip install -U ffmpegcv

Here is my test code.

# source pusher, bash or cmd
ffmpeg -re -i E:\WeChat_20240801170849.mp4 -t 00:00:20 -f rtsp -c copy rtsp://10.50.xxx.xxx:8554/mystream
#python code
import ffmpegcv
import sys
import tqdm

file = 'rtsp://10.50.xxx.xxx:8554/mystream'
# cap = ffmpegcv.VideoCaptureStreamRT(file)  #works fine
cap = ffmpegcv.ReadLiveLast(ffmpegcv.VideoCaptureStreamRT, file)   #works fine
tbar = tqdm.tqdm()
while True:
    ret, frame = cap.read()
    tbar.update(1)
    if not ret:
        print('this is the end')
        break
cap.release()
$ python test_stream.py rtsp://10.50.xxx.xxx:8554/mystream
0it [00:00, ?it/s][hevc @ 0x55a19aa16d00] Could not find ref with POC 60
[hevc @ 0x55a19aa16d00] Could not find ref with POC 57
[hevc @ 0x55a19aa16d00] Could not find ref with POC 54
[hevc @ 0x55a19aa16d00] Could not find ref with POC 63
61it [00:06, 42.75it/s]this is the end    <<<===============AUTO STOPED
61it [00:06,  9.66it/s]

@chenxinfeng4
Copy link
Owner

Did you figure out the issue @abdul3909

@abdul3909
Copy link
Author

No, It is not working. I am using the latest version of ffmpegcv.
If I use the same code with VideoCapture() and mp4 files, it exits as soon as the stream is over but this is not the case with RTSP and VideoCaptureStreamRT or VideoCaptureStream.

@chenxinfeng4
Copy link
Owner

Can you post a short testing scipt?

@abdul3909
Copy link
Author

abdul3909 commented Sep 7, 2024

Here is the script producing same problem:

def process_stream(rtsp_url):


    #cap = ffmpegcv.VideoCaptureStream(rtsp_url)
    cap = ffmpegcv.ReadLiveLast(ffmpegcv.VideoCaptureStreamRT, rtsp_url)

    model = YOLO("models/face_model.pt")


    while True:
        ret, frame = cap.read()

        # Break loop if no frame is returned after timeout
        if not ret:
            print("Exiting...")

            break

        model(frame, conf=0.70, iou=0.5, show=True, verbose=True)

  

        # if cv2.waitKey(1) == ord('q'):
        #     break

    cap.release()
    cv2.destroyAllWindows()
    ```

@chenxinfeng4
Copy link
Owner

How to simulate the 'rtsp_url' to repeat the issue.

@abdul3909
Copy link
Author

The RTSP URLs I am processing are only accessible within the network. Is there any other way I can assist?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants