|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
-
- # Python program to save a
- # video using OpenCV
-
-
- import cv2
- from time import perf_counter
- import argparse
- import os
- os.environ['DISPLAY'] = ':0'
- # Create an object to read
- # from camera
- parser = argparse.ArgumentParser()
- parser.add_argument('--video',type=str,default='resources/1-min.mp4',help='Path to video file')
- args=parser.parse_args()
- initial = perf_counter()
- video = cv2.VideoCapture(args.video)
-
- # We need to check if camera
- # is opened previously or not
- if (video.isOpened() == False):
- print("Error reading video file")
-
- # We need to set resolutions.
- # so, convert them from float to integer.
- frame_width = int(video.get(3))
- frame_height = int(video.get(4))
-
- size = (frame_width, frame_height)
-
- # Below VideoWriter object will create
- # a frame of above defined The output
- # is stored in 'filename.avi' file.
- result = cv2.VideoWriter('{}.avi'.format(os.path.basename(args.video)[-4]),
- cv2.VideoWriter_fourcc(*'MJPG'),
- 30, size)
-
- while(True):
- ret, frame = video.read()
-
- if ret == True:
-
- # Write the frame into the
- # file 'filename.avi'
- result.write(frame)
-
- # Display the frame
- # saved in the file
- #cv2.imshow('Frame', frame)
-
- # Press S on keyboard
- # to stop the process
- if cv2.waitKey(1) & 0xFF == ord('s'):
- break
-
- # Break the loop
- else:
- break
-
- # When everything done, release
- # the video capture and video
- # write objects
- video.release()
- result.release()
- end=perf_counter()
- # Closes all the frames
- cv2.destroyAllWindows()
- print(f'total time taken = {end-initial}')
- print("The video was successfully saved")
|