실시간 비디오 스트리밍: RTSP

김성빈·2024년 5월 23일
0

Modern Computer Vision

목록 보기
47/117

"RTSP(Real Time Streaming Protocol)는 실시간 멀티미디어 데이터(예: 오디오/비디오)를 클라이언트와 서버 간에 전송하는 데 사용되는 프로토콜"

특별한 이유 없이 사용 가능한 무료 RTSP 스트리밍 주소를 사용해서 테스트 하겠다.

Big Buck Bunny 영화의 트레일러를 스트리밍 무료로 스트리밍 해주는 주소로,

실시간 스트리밍 데이터 처리 공부를 위해 많이 사용된다.

rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov
import cv2 

# 우리의 무료 테스트 RTSP 링크
# CCTV IPTV 카메라를 구성하여 RSTP 스트림을 출력하도록 설정할 수 있습니다.
cap = cv2.VideoCapture("rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov")

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

    cv2.imshow('RTSP Stream', frame)
    
    if cv2.waitKey(1) == 13: #13은 엔터 키입니다.
        break
        
# 카메라를 해제하고 창을 닫습니다.
cap.release()
cv2.destroyAllWindows()      

계속 실행을 해봤는데 스트리밍이 되지 않음

포럼에 인터넷 문제일수도 있어서 공유기랑 연결되있지 않은

하스팟으로 테스트 해봣는데 안됨

더 찾다가 아래 글 확인

해당 주소는 더이상 동작하지 않는다는 사실 확인.

공공데이터 포털에 RSTP 주소 제공,

스트리밍을 계속 시도해봤는데 안돼서 10초 동영상으로 만들어서 출력

import cv2
import time
from google.colab.patches import cv2_imshow

def save_rtsp_stream(rtsp_url, output_file, duration=10):
    # RTSP 스트림 캡처기 생성
    cap = cv2.VideoCapture(rtsp_url)

    if not cap.isOpened():
        print("Failed to open RTSP stream.")
        return

    # 비디오 코덱 설정 (XVID 코덱 사용)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    fps = 20  # 프레임 속도 설정
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    out = cv2.VideoWriter(output_file, fourcc, fps, (width, height))

    start_time = time.time()
    while True:
        ret, frame = cap.read()
        if not ret:
            print("Failed to receive frame.")
            break

        # 프레임 저장
        out.write(frame)

        # 종료 조건 (duration 초 경과)
        if time.time() - start_time > duration:
            break

    # 자원 해제
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    print(f"Video saved to {output_file}")

# RTSP 스트림 URL
rtsp_url = "rtsp://210.99.70.120:1935/live/cctv007.stream"
# 저장할 비디오 파일 이름
output_file = "output.avi"

# RTSP 스트림을 10초 동안 저장
save_rtsp_stream(rtsp_url, output_file, duration=10)

# 저장된 비디오 파일 재생
from IPython.display import HTML
from IPython.display import display

display(HTML(f"""
<video width="640" height="480" controls>
  <source src="{output_file}" type="video/mp4">
  Your browser does not support the video tag.
</video>
"""))

스트리밍 문제의 원인은 제대로 확인하지 못함,

colab에서 하기에는 성능이 좋지 않아 버벅거리고

더 나은 GPU 를 사용하는 local 에서 코드를 실행하면 반응이 없다.

애초에 스트리밍인데 인터넷의 영향이 더 클것같지만.

지금으로서 알 방법이 없다.

이러한 예제들을 계속 공부해 가다보면 언젠가는 발견할것

profile
감사합니다. https://www.youtube.com/channel/UCxlkiu9_aWijoD7BannNM7w

0개의 댓글

관련 채용 정보