frame.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import os
  2. from tracemalloc import start
  3. import cv2
  4. import glob
  5. import numpy as np
  6. # 视频来源 地址需要替换自己的可识别文件地址
  7. # videoPath = '/Users/gemer/Desktop/test-video'
  8. # folders = glob.glob('/Users/gemer/Desktop/test-video-1')
  9. folders = sorted(filter(os.path.isfile,
  10. glob.glob('/Users/gemer/Desktop/test-video-1' + '/*.264', recursive=True)))
  11. imagePath = '/Users/gemer/Desktop/test-video-1/image/'
  12. videonames_list = []
  13. print('list_of_files', folders)
  14. for folder in folders:
  15. videonames_list.append(folder)
  16. print(folder)
  17. print('There are {} videos in Folder'.format(len(videonames_list)))
  18. filePath = '/Users/gemer/Desktop/'
  19. out_video = np.empty([30, 1920, 1080, 3], dtype=np.uint8)
  20. out_video = out_video.astype(np.uint8)
  21. start_frame = 12
  22. count = 0
  23. # for i in range(0, len(videonames_list)):
  24. # video_data = videonames_list[i]
  25. # video = cv2.VideoCapture(video_data)
  26. # success = True
  27. # while success:
  28. # success, image = video.read()
  29. # name = imagePath + str(i) + '-' + str(count)+'.jpg'
  30. # if success == True:
  31. # if count == 10:
  32. # cv2.imwrite(name, image)
  33. # print('video {} '.format(i))
  34. # print('Frame {} Extracted Successfully'.format(count))
  35. # count += 1
  36. # else:
  37. # count = 0
  38. # print('\n\n\nVideo {} Extracted Successfully\n\n\n'.format(video_data))
  39. caps = [cv2.VideoCapture(i) for i in videonames_list]
  40. print('cap', caps)
  41. frameCount = 0
  42. vidIndex = 0
  43. # for vid in caps:
  44. for index, vid in enumerate(caps):
  45. # print(x)
  46. fourcc = cv2.VideoWriter_fourcc('H', '2', '6', '4')
  47. size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
  48. int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
  49. fps = vid.get(cv2.CAP_PROP_FPS) # 30p/self
  50. fps = int(fps)
  51. hz = int(1000.0 / fps)
  52. sizeStr = str(size[0]) + 'x' + str(size[1])
  53. print('index {}'.format(index))
  54. while(True):
  55. success, image = vid.read()
  56. if success == True:
  57. name = imagePath + str(frameCount)+'.jpg'
  58. cv2.imwrite(name, image)
  59. print('Frame {} Extracted Successfully'.format(frameCount))
  60. frameCount += 1
  61. else:
  62. frameCount = 0