1#!/usr/bin/env python3 2import sys 3import cv2 as cv 4from oled.img2code import convert_frame_to_bytes 5 6DEFAULT_PORT = 5678 7 8def convert_video_to_bin(videoFile, binFile): 9 cap = cv.VideoCapture(videoFile) 10 frameCount = cap.get(cv.CAP_PROP_FRAME_COUNT) 11 print('frame count:', frameCount) 12 print('frame width:', cap.get(cv.CAP_PROP_FRAME_WIDTH)) 13 print('frame height:', cap.get(cv.CAP_PROP_FRAME_HEIGHT)) 14 lastPercent = 0 15 with open(binFile, 'wb+') as f: 16 while True: 17 retval, frame = cap.read() 18 if not retval: 19 print('video done!') 20 break 21 bitmap = convert_frame_to_bytes(frame) 22 f.write(bitmap) 23 pos = cap.get(cv.CAP_PROP_POS_FRAMES) 24 percent = pos / frameCount * 100 25 if percent - lastPercent >= 1: 26 lastPercent = percent 27 sys.stdout.write('=') 28 sys.stdout.flush() 29 print('convert all frames done!') 30 cap.release() 31 32def main(): 33 if len(sys.argv) < 3: 34 print("Usage: {} videoFile binFile\n\t".format(sys.argv[0])) 35 exit(-1) 36 37 try: 38 videoFile = sys.argv[1] 39 binFile = sys.argv[2] 40 convert_video_to_bin(videoFile, binFile) 41 except Exception as e: 42 print('exception raised:', e) 43 44if __name__ == "__main__": 45 main() 46