閒暇時間,搞了個海康IP攝像頭玩一玩,首先你得知道你的IP地址和密碼,這些要是不知道,可以給售後打電話,售後還是可以的。海康的網絡攝像頭雖然可以直接在web端上面直接顯示,但顯然是不可能用這種方法的。
1.把電腦的IP與海康的設置到同一網段後,要開始對視頻進行解碼
2.寫了個demo,簡單暴力的使用opencv直接解碼,不得不說opencv是真的強
# -*- coding: utf-8 -*-
'''
demo
#rtsp://[username]:[password]@[ip]:[port]/
1) username 用戶名,常用 admin
2) password 密碼,nv123
3) ip 攝像頭IP,如 192.168.6.66
4) port 端口號,默認爲 554
'''
import cv2
url = 'rtsp://admin:[email protected]:554/'#根據攝像頭設置IP及rtsp端口
cap = cv2.VideoCapture(url)#讀取視頻流
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('CameraDemo',frame)
if cv2.waitKey(1) & 0xFF == ord('q') :
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
上述就能看到攝像頭實時傳回的圖像了。然後又搞了個保存視頻的demo
#-*- coding = utf-8 -*-
import time
import cv2
print cv2
RTSP_URL = 'rtsp://admin:[email protected]:554/h264/ch1/sub/av_stream' # your camera's rtsp url
DURATION = 30 # how many time in seconds you want to capture
OUTPUT_FILE = 'capture_video.mp4'
cap = cv2.VideoCapture(RTSP_URL)
#fourcc = cv2.VideoWriter_fourcc('h','2','6','4')
fourcc = 0x21
#fps = cap.get(cv2.CAP_PROP_FPS)
fps = 25
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
saver = cv2.VideoWriter(OUTPUT_FILE, fourcc, fps, size)
print "rtsp_url = %s, fps = %d, size = %s"%(RTSP_URL, fps, size)
got_first_frame = False
frame_count = 0
while True:
ret, frame = cap.read()
if not(ret):
continue
frame_count += 1
print "%s: frame %d received" % (time.time(), frame_count)
if got_first_frame == False:
start_time = time.time()
got_first_frame = True
saver.write(frame)
now = time.time()
if int(now - start_time) > DURATION:
break
這段代碼就能把視頻保存到本地,但是你只能中斷程序後才能看看視頻,不然一邊傳到顯示屏上,一邊保存,那樣會花屏。就直接等保存完了之後再看看拍的視頻