test022_mobile_video.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import ffmpeg
  2. from cv2 import cv2 as cv2
  3. # import pyaudio
  4. def readCamra():
  5. # 初始化摄像头
  6. cv2.namedWindow("camera", 1)
  7. # 开启ip摄像头
  8. video = "rtsp://admin:admin@172.16.101.64:8554/live" # 此处@后的ipv4 地址需要改为app提供的地址
  9. cap = cv2.VideoCapture(video)
  10. # 截图次数
  11. num = 0
  12. # 分辨率大小
  13. size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
  14. int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
  15. # fps = cap.get(cv2.CAP_PROP_FPS) 帧率过大
  16. # 录视频
  17. # .flv 格式 , 25为 FPS 帧率, (640,480)为大小
  18. # fourcc = cv2.VideoWriter_fourcc(*'flv1')
  19. # outVideo = cv2.VideoWriter('output.flv', fourcc, 25, size)
  20. # # .mp4格式 , 25为 FPS 帧率, (640,480)为大小
  21. fourcc = cv2.VideoWriter_fourcc(*'mp4v')
  22. outVideo = cv2.VideoWriter('output.mp4', fourcc, 60, size)
  23. # # .avi格式 , 25为 FPS 帧率, (640,480)为大小
  24. # fourcc = cv2.VideoWriter_fourcc(*'XVID')
  25. # out = cv2.VideoWriter('output.avi', fourcc, 60, size)
  26. while True:
  27. # 读取视频
  28. success, img = cap.read()
  29. # 录制视频
  30. outVideo.write(img)
  31. # 显示
  32. cv2.imshow("camera", img)
  33. # 按键处理,注意,焦点应当在摄像头窗口,不是在终端命令行窗口
  34. key = cv2.waitKey(10)
  35. if key == 27:
  36. # esc键退出
  37. print("esc break...")
  38. break
  39. if key == ord('q'):
  40. # 保存一张图像
  41. num = num+1
  42. filename = "frames_%s.jpg" % num
  43. cv2.imwrite(filename, img)
  44. # Clean up
  45. outVideo.release()
  46. cap.release()
  47. cv2.destroyAllWindows()
  48. pass
  49. def palyvideos():
  50. host = 'rtsp://admin:admin@172.16.101.64:8554/live'
  51. # allowed_media_types='audio' 只读取音频流
  52. # 音量大小控制
  53. # ac是声道,ar是采样率
  54. ffmpeg.input(host, allowed_media_types='audio', rtsp_transport='tcp')['a'].filter('volume', 1).output(
  55. 'saved_audio.wav', acodec='pcm_s16le', ac=1, ar='16k').overwrite_output().run(capture_stdout=True)
  56. # 输出视频文件格式不对
  57. # ffmpeg.input(host, rtsp_transport='tcp').output(
  58. # 'saved_rtsp.mp4').overwrite_output().run(capture_stdout=True)
  59. pass
  60. if __name__ == '__main__':
  61. readCamra()
  62. # palyvideos()