chenxinfeng4 / ffmpegcv

The ffmpegcv is a ffmpeg backbone for open-cv like Video Reader and Writer
157 stars 24 forks source link

Issue with RTSP Stream Stuck on Last Frame #50

Open abdul3909 opened 2 weeks ago

abdul3909 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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.

chenxinfeng4 commented 2 weeks ago

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 commented 2 weeks ago

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

chenxinfeng4 commented 2 weeks ago

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 commented 1 week ago

Did you figure out the issue @abdul3909

abdul3909 commented 1 week ago

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 commented 6 days ago

Can you post a short testing scipt?

abdul3909 commented 6 days ago

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 commented 6 days ago

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

abdul3909 commented 5 days ago

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