Capture and Save IP Camera in Python OpenCV

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import cv2
#print("Before URL")
cap = cv2.VideoCapture('rtsp://admin@192.168.10.29:554/cam/realmonitor?channel=1&subtype=1')
#print("After URL")
(grabbed, frame) = cap.read()
fshape = frame.shape
fheight = fshape[0]
fwidth = fshape[1]
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (fwidth,fheight))
while True:
#print('About to start the Read command')
ret, frame = cap.read()
#print('About to show frame of Video.')
cv2.imshow("Capturing",frame)
#print('Running..')
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
import cv2 #print("Before URL") cap = cv2.VideoCapture('rtsp://admin@192.168.10.29:554/cam/realmonitor?channel=1&subtype=1') #print("After URL") (grabbed, frame) = cap.read() fshape = frame.shape fheight = fshape[0] fwidth = fshape[1] fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi',fourcc, 20.0, (fwidth,fheight)) while True: #print('About to start the Read command') ret, frame = cap.read() #print('About to show frame of Video.') cv2.imshow("Capturing",frame) #print('Running..') out.write(frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
import cv2

#print("Before URL")
cap = cv2.VideoCapture('rtsp://admin@192.168.10.29:554/cam/realmonitor?channel=1&subtype=1')
#print("After URL")

(grabbed, frame) = cap.read()
fshape = frame.shape
fheight = fshape[0]
fwidth = fshape[1]
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (fwidth,fheight))

while True:

    #print('About to start the Read command')
    ret, frame = cap.read()
    #print('About to show frame of Video.')
    cv2.imshow("Capturing",frame)
    #print('Running..')

    out.write(frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

References
https://pupli.net/2021/01/access-ip-camera-in-python-opencv/
https://stackoverflow.com/questions/29317262/opencv-video-saving-in-python