25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.7 KiB

  1. # Python program to save a
  2. # video using OpenCV
  3. import cv2
  4. from time import perf_counter
  5. import argparse
  6. import os
  7. os.environ['DISPLAY'] = ':0'
  8. # Create an object to read
  9. # from camera
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument('--video',type=str,default='resources/1-min.mp4',help='Path to video file')
  12. args=parser.parse_args()
  13. initial = perf_counter()
  14. video = cv2.VideoCapture(args.video)
  15. # We need to check if camera
  16. # is opened previously or not
  17. if (video.isOpened() == False):
  18. print("Error reading video file")
  19. # We need to set resolutions.
  20. # so, convert them from float to integer.
  21. frame_width = int(video.get(3))
  22. frame_height = int(video.get(4))
  23. size = (frame_width, frame_height)
  24. # Below VideoWriter object will create
  25. # a frame of above defined The output
  26. # is stored in 'filename.avi' file.
  27. result = cv2.VideoWriter('{}.avi'.format(os.path.basename(args.video)[-4]),
  28. cv2.VideoWriter_fourcc(*'MJPG'),
  29. 30, size)
  30. while(True):
  31. ret, frame = video.read()
  32. if ret == True:
  33. # Write the frame into the
  34. # file 'filename.avi'
  35. result.write(frame)
  36. # Display the frame
  37. # saved in the file
  38. #cv2.imshow('Frame', frame)
  39. # Press S on keyboard
  40. # to stop the process
  41. if cv2.waitKey(1) & 0xFF == ord('s'):
  42. break
  43. # Break the loop
  44. else:
  45. break
  46. # When everything done, release
  47. # the video capture and video
  48. # write objects
  49. video.release()
  50. result.release()
  51. end=perf_counter()
  52. # Closes all the frames
  53. cv2.destroyAllWindows()
  54. print(f'total time taken = {end-initial}')
  55. print("The video was successfully saved")