-
Notifications
You must be signed in to change notification settings - Fork 6
/
analyseVideo.py
163 lines (148 loc) · 5.9 KB
/
analyseVideo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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