/analyseVideo.py

https://github.com/azurlane-doujin/azurlane-painting · Python · 163 lines · 136 code · 15 blank · 12 comment · 32 complexity · a14a31cbe439ae5b1a9ea307f2390be1 MD5 · raw file

  1. import cv2
  2. import numpy as np
  3. import time
  4. import paint
  5. # import draw
  6. # import draw2
  7. import os
  8. def old():
  9. cap = cv2.VideoCapture('videos/chm.flv')
  10. # Check if camera opened successfully
  11. if (cap.isOpened() == False):
  12. print("Error opening video stream or file")
  13. i = 0
  14. while (cap.isOpened()):
  15. ret, frame = cap.read()
  16. if ret == True:
  17. try:
  18. o = paint.drawRandBg(frame, blur=None)
  19. o.save(f"outputs/chm/{i}.png", "PNG")
  20. except:
  21. pass
  22. i += 1
  23. else:
  24. break
  25. cap.release()
  26. cv2.destroyAllWindows()
  27. def makeVideo(inputVideoPath: str, output: str, cutFrames: int = 1, w: int = 1, h: int = 1, blur: int = 0):
  28. """
  29. 输出的视频没有音频轨道没有原视频对比,请放入其他剪辑软件中自行添加.,如果w和h调的特别大的话,视频分辨率会超级高.
  30. :param input:输入视频的路径
  31. :param output:输出视频的路径,请以".avi"结尾
  32. :param cutFrames:抽帧频率,比如填1的话,则原视频每一帧都会处理后加入新视频中;填2的话,原视频每2帧会有一帧处理后加入新视频中
  33. :param w:横向画板数
  34. :param h:纵向画板数
  35. :param blur:控制线条加粗,设为0的话不做加粗处理.不为0的话只能填单数,即1,3,5,7.某些情况下线条加粗比较符合像素风,请按需取用
  36. :return:None
  37. """
  38. inVideo = cv2.VideoCapture(inputVideoPath)
  39. inFps = inVideo.get(cv2.CAP_PROP_FPS)
  40. outFps = inFps / cutFrames
  41. framesCount = int(inVideo.get(cv2.CAP_PROP_FRAME_COUNT))
  42. outVideo = cv2.VideoWriter(output, cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), outFps,
  43. (37 * 20 * w + 278 + 262, 22 * 20 * h + 110 + 170))
  44. if (inVideo.isOpened() == False):
  45. print("Error opening video stream or file")
  46. i = 0
  47. while (inVideo.isOpened()):
  48. ret, frame = inVideo.read()
  49. if ret == True:
  50. if i % cutFrames == 0:
  51. try:
  52. o = paint.drawN(frame,w=w,h=h, blur=blur)
  53. img = cv2.cvtColor(np.asarray(o), cv2.COLOR_RGB2BGR)
  54. except:
  55. pass
  56. else:
  57. outVideo.write(img)
  58. print(f"{i:>10} of {framesCount} frames handdled")
  59. i += 1
  60. else:
  61. break
  62. inVideo.release()
  63. outVideo.release()
  64. def futuresMakeVideo(inputVideoPath: str, output: str, cutFrames: int = 1, w: int = 1, h: int = 1, blur: int = 0,works=os.cpu_count()):
  65. from concurrent import futures
  66. inVideo = cv2.VideoCapture(inputVideoPath)
  67. inFps = inVideo.get(cv2.CAP_PROP_FPS)
  68. outFps = inFps / cutFrames
  69. framesCount = int(inVideo.get(cv2.CAP_PROP_FRAME_COUNT))
  70. inVideo.get()
  71. outVideo = cv2.VideoWriter(output, cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), outFps,
  72. (37 * 20 * w + 278 + 262, 22 * 20 * h + 110 + 170))
  73. if (inVideo.isOpened() == False):
  74. print("Error opening video stream or file")
  75. works = works if 0 < works <= os.cpu_count() else os.cpu_count()
  76. toHanddle = {}
  77. handdledFrames = {}
  78. readVideoI = 0
  79. handdleVideoI = 0
  80. def readOneGroup():
  81. nonlocal readVideoI
  82. for x in range(works):
  83. ret, frame = inVideo.read()
  84. toHanddle[str(readVideoI)] = frame
  85. readVideoI += 1
  86. return toHanddle
  87. def handdleFrame(x:int):
  88. nonlocal toHanddle,handdledFrames,handdleVideoI
  89. sx = str(x)
  90. frame = toHanddle[sx]
  91. handdledFrame = paint.drawN(frame, w=w, h=h, blur=blur)
  92. handdledFrame = cv2.cvtColor(np.asarray(handdledFrame), cv2.COLOR_RGB2BGR)
  93. handdledFrames[sx] = handdledFrame
  94. for i in range(handdleVideoI,x + 1):
  95. si = str(i)
  96. if si in handdledFrames.keys():
  97. outVideo.write(handdledFrames[si])
  98. handdleVideoI += 1
  99. else:
  100. continue
  101. if toHanddle.__len__() <= 16:
  102. readOneGroup()
  103. handdled = 0
  104. readOneGroup()
  105. readOneGroup()
  106. print(toHanddle)
  107. with futures.ProcessPoolExecutor(works) as exe:
  108. all_task = [exe.submit(handdleFrame,i) for i in range(framesCount)]
  109. for future in futures.as_completed(all_task):
  110. handdled += 1
  111. print(f"{handdled}/{framesCount} handdled")
  112. inVideo.release()
  113. outVideo.release()
  114. def futuresMakeVideoFuck(inputVideoPath: str, output: str, cutFrames: int = 1, w: int = 1, h: int = 1, blur: int = 0,works=os.cpu_count()):
  115. from concurrent import futures
  116. inVideo = cv2.VideoCapture(inputVideoPath)
  117. inFps = inVideo.get(cv2.CAP_PROP_FPS)
  118. outFps = inFps / cutFrames
  119. framesCount = int(inVideo.get(cv2.CAP_PROP_FRAME_COUNT))
  120. outVideo = cv2.VideoWriter(output, cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), outFps,
  121. (37 * 20 * w + 278 + 262, 22 * 20 * h + 110 + 170))
  122. if (inVideo.isOpened() == False):
  123. print("Error opening video stream or file")
  124. works = works if 0 < works <= os.cpu_count() else os.cpu_count()
  125. frames = []
  126. while (inVideo.isOpened()):
  127. ret, frame = inVideo.read()
  128. if ret == True:
  129. frames.append(frame)
  130. def handdleOne(frame):
  131. o = paint.drawN(frame, w=w, h=h, blur=blur)
  132. img = cv2.cvtColor(np.asarray(o), cv2.COLOR_RGB2BGR)
  133. pass
  134. with futures.ProcessPoolExecutor(works) as exe:
  135. all_task = [exe.submit(handdleFrame, i) for i in range(framesCount)]
  136. for future in futures.as_completed(all_task):
  137. handdled += 1
  138. print(f"{handdled}/{framesCount} handdled")
  139. inVideo.release()
  140. outVideo.release()
  141. if __name__ == '__main__':
  142. # makeVideo("videos/sjw.mp4", "sjw.avi",1,4,4)
  143. futuresMakeVideoFuck("videos/ng.mp4", "ng.avi",1,1,1)
  144. # print("1" in {"1":3}.keys())
  145. # x = 0
  146. # for n in range(x,10):
  147. # print(n)
  148. # x = 5