当前位置:   article > 正文

opencv python播放视频和保存视频_linux python opencv ffmpeg cv2.videocapture

linux python opencv ffmpeg cv2.videocapture
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import cv2
  4. def playVideo(videoFile):
  5. cap = cv2.VideoCapture(videoFile)
  6. if cap.isOpened():
  7. print("Open ", videoFile, " success!")
  8. else:
  9. print("Open ", videoFile, " failed!")
  10. return
  11. #print(cap) # print cap address
  12. # cap.get(propId) 来获得视频的一些参数信息
  13. print("fps: ", cap.get(cv2.CAP_PROP_FPS))
  14. print("frame counts: ", cap.get(cv2.CAP_PROP_FRAME_COUNT))
  15. print("frame width: ", cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  16. print("frame hight: ", cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  17. # cap.set(propId,value) 来修改
  18. while True:
  19. # Capture frame-by-frame
  20. ret, frame = cap.read()
  21. # cap.read() get a frame, if ret = false then frame is empty
  22. if not ret:
  23. break
  24. # Our operations on the frame come here
  25. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  26. # Display the resulting frame
  27. cv2.imshow(videoFile, frame)
  28. cv2.imshow('frame',gray)
  29. # q or ESC to exit
  30. if cv2.waitKey(1) & 0xFF == ord('q') or cv2.waitKey(1) & 0xFF == 27:
  31. break;
  32. # When everything done, release the capture
  33. cap.release()
  34. cv2.destroyAllWindows()
  35. # 确保已经装了合适版本的 ffmpeg 或者 gstreamer
  36. def saveVideo(videoFile):
  37. cap = cv2.VideoCapture(videoFile)
  38. if cap.isOpened():
  39. print("Open ", videoFile, " success!")
  40. else:
  41. print("Open ", videoFile, " failed!")
  42. return
  43. #print(cap) # print cap address
  44. width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
  45. height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
  46. # cap.get(propId) 来获得视频的一些参数信息
  47. print("fps: ", cap.get(cv2.CAP_PROP_FPS))
  48. print("frame counts: ", cap.get(cv2.CAP_PROP_FRAME_COUNT))
  49. print("frame width: ", width)
  50. print("frame hight: ", height)
  51. # cap.set(propId,value) 来修改
  52. # Define the codec and create VideoWriter object
  53. # fourcc = cv2.VideoWriter_fourcc(*'XVID')
  54. fourcc = cv2.VideoWriter_fourcc(*'DIVX')
  55. out = cv2.VideoWriter('output.avi', fourcc, 20.0, (int(width),int(height)))
  56. if out.isOpened():
  57. print("Create output video success!")
  58. else:
  59. print("Create output video failed!")
  60. while cap.isOpened():
  61. # Capture frame-by-frame
  62. ret, frame = cap.read()
  63. # cap.read() get a frame, if ret = false then frame is empty
  64. if not ret:
  65. break
  66. # 上下翻转
  67. # frame = cv2.flip(frame,0)
  68. # 左右翻转
  69. frame = cv2.flip(frame,1)
  70. # write the flipped frame
  71. out.write(frame)
  72. cv2.imshow("frame", frame)
  73. if cv2.waitKey(1) & 0xFF == ord('q'):
  74. break;
  75. # When everything done, release the capture
  76. cap.release()
  77. out.release()
  78. cv2.destroyAllWindows()
  79. def main():
  80. #videoFile = 0
  81. #videoFile = "F:\\dataSet\\video.avi"
  82. videoFile = "F:\\dataSet\\opencv33.mp4"
  83. #playVideo(videoFile)
  84. saveVideo(videoFile)
  85. if __name__ == '__main__':
  86. main()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/484753
推荐阅读
相关标签
  

闽ICP备14008679号