/analyseVideo.py
https://github.com/azurlane-doujin/azurlane-painting · Python · 163 lines · 136 code · 15 blank · 12 comment · 32 complexity · a14a31cbe439ae5b1a9ea307f2390be1 MD5 · raw file
- import cv2
- import numpy as np
- import time
- import paint
- # import draw
- # import draw2
- import os
- def old():
- cap = cv2.VideoCapture('videos/chm.flv')
- # Check if camera opened successfully
- if (cap.isOpened() == False):
- print("Error opening video stream or file")
- i = 0
- while (cap.isOpened()):
- ret, frame = cap.read()
- if ret == True:
- try:
- o = paint.drawRandBg(frame, blur=None)
- o.save(f"outputs/chm/{i}.png", "PNG")
- except:
- pass
- i += 1
- else:
- break
- cap.release()
- cv2.destroyAllWindows()
- def makeVideo(inputVideoPath: str, output: str, cutFrames: int = 1, w: int = 1, h: int = 1, blur: int = 0):
- """
- 输出的视频没有音频轨道没有原视频对比,请放入其他剪辑软件中自行添加.另,如果w和h调的特别大的话,视频分辨率会超级高.
- :param input:输入视频的路径
- :param output:输出视频的路径,请以".avi"结尾
- :param cutFrames:抽帧频率,比如填1的话,则原视频每一帧都会处理后加入新视频中;填2的话,原视频每2帧会有一帧处理后加入新视频中
- :param w:横向画板数
- :param h:纵向画板数
- :param blur:控制线条加粗,设为0的话不做加粗处理.不为0的话只能填单数,即1,3,5,7等.某些情况下线条加粗比较符合像素风,请按需取用
- :return:None
- """
- inVideo = cv2.VideoCapture(inputVideoPath)
- inFps = inVideo.get(cv2.CAP_PROP_FPS)
- outFps = inFps / cutFrames
- framesCount = int(inVideo.get(cv2.CAP_PROP_FRAME_COUNT))
- outVideo = cv2.VideoWriter(output, cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), outFps,
- (37 * 20 * w + 278 + 262, 22 * 20 * h + 110 + 170))
- if (inVideo.isOpened() == False):
- print("Error opening video stream or file")
- i = 0
- while (inVideo.isOpened()):
- ret, frame = inVideo.read()
- if ret == True:
- if i % cutFrames == 0:
- try:
- o = paint.drawN(frame,w=w,h=h, blur=blur)
- img = cv2.cvtColor(np.asarray(o), cv2.COLOR_RGB2BGR)
- except:
- pass
- else:
- outVideo.write(img)
- print(f"{i:>10} of {framesCount} frames handdled")
- i += 1
- else:
- break
- inVideo.release()
- outVideo.release()
- def futuresMakeVideo(inputVideoPath: str, output: str, cutFrames: int = 1, w: int = 1, h: int = 1, blur: int = 0,works=os.cpu_count()):
- from concurrent import futures
- inVideo = cv2.VideoCapture(inputVideoPath)
- inFps = inVideo.get(cv2.CAP_PROP_FPS)
- outFps = inFps / cutFrames
- framesCount = int(inVideo.get(cv2.CAP_PROP_FRAME_COUNT))
- inVideo.get()
- outVideo = cv2.VideoWriter(output, cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), outFps,
- (37 * 20 * w + 278 + 262, 22 * 20 * h + 110 + 170))
- if (inVideo.isOpened() == False):
- print("Error opening video stream or file")
- works = works if 0 < works <= os.cpu_count() else os.cpu_count()
- toHanddle = {}
- handdledFrames = {}
- readVideoI = 0
- handdleVideoI = 0
- def readOneGroup():
- nonlocal readVideoI
- for x in range(works):
- ret, frame = inVideo.read()
- toHanddle[str(readVideoI)] = frame
- readVideoI += 1
- return toHanddle
- def handdleFrame(x:int):
- nonlocal toHanddle,handdledFrames,handdleVideoI
- sx = str(x)
- frame = toHanddle[sx]
- handdledFrame = paint.drawN(frame, w=w, h=h, blur=blur)
- handdledFrame = cv2.cvtColor(np.asarray(handdledFrame), cv2.COLOR_RGB2BGR)
- handdledFrames[sx] = handdledFrame
- for i in range(handdleVideoI,x + 1):
- si = str(i)
- if si in handdledFrames.keys():
- outVideo.write(handdledFrames[si])
- handdleVideoI += 1
- else:
- continue
- if toHanddle.__len__() <= 16:
- readOneGroup()
- handdled = 0
- readOneGroup()
- readOneGroup()
- print(toHanddle)
- with futures.ProcessPoolExecutor(works) as exe:
- all_task = [exe.submit(handdleFrame,i) for i in range(framesCount)]
- for future in futures.as_completed(all_task):
- handdled += 1
- print(f"{handdled}/{framesCount} handdled")
- inVideo.release()
- outVideo.release()
- def futuresMakeVideoFuck(inputVideoPath: str, output: str, cutFrames: int = 1, w: int = 1, h: int = 1, blur: int = 0,works=os.cpu_count()):
- from concurrent import futures
- inVideo = cv2.VideoCapture(inputVideoPath)
- inFps = inVideo.get(cv2.CAP_PROP_FPS)
- outFps = inFps / cutFrames
- framesCount = int(inVideo.get(cv2.CAP_PROP_FRAME_COUNT))
- outVideo = cv2.VideoWriter(output, cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), outFps,
- (37 * 20 * w + 278 + 262, 22 * 20 * h + 110 + 170))
- if (inVideo.isOpened() == False):
- print("Error opening video stream or file")
- works = works if 0 < works <= os.cpu_count() else os.cpu_count()
- frames = []
- while (inVideo.isOpened()):
- ret, frame = inVideo.read()
- if ret == True:
- frames.append(frame)
- def handdleOne(frame):
- o = paint.drawN(frame, w=w, h=h, blur=blur)
- img = cv2.cvtColor(np.asarray(o), cv2.COLOR_RGB2BGR)
- pass
- with futures.ProcessPoolExecutor(works) as exe:
- all_task = [exe.submit(handdleFrame, i) for i in range(framesCount)]
- for future in futures.as_completed(all_task):
- handdled += 1
- print(f"{handdled}/{framesCount} handdled")
- inVideo.release()
- outVideo.release()
- if __name__ == '__main__':
- # makeVideo("videos/sjw.mp4", "sjw.avi",1,4,4)
- futuresMakeVideoFuck("videos/ng.mp4", "ng.avi",1,1,1)
- # print("1" in {"1":3}.keys())
- # x = 0
- # for n in range(x,10):
- # print(n)
- # x = 5