-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameToGif.py
142 lines (124 loc) · 5.22 KB
/
FrameToGif.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
import os
from pathlib import Path
import cv2
import time
from GifUtil import GifUtil
from FileUtil import FileUtil
class FrameToGif:
@staticmethod
def toGif(moviePath=None, createGifPath=None):
files = FileUtil.all_path(moviePath)
print("total files:", len(files))
file_count = 0
for file in files:
if file_count < 20000:
print("file path:", file)
fileName = file.lstrip(moviePath)
print("file name:", fileName)
diction = createGifPath + fileName
print("mkdir:", diction)
newPath = Path(diction)
if newPath.exists():
print("folder exist")
else:
os.mkdir(diction)
# vc = cv2.VideoCapture("I:\132.mp4") # 读取视频文件
vc = cv2.VideoCapture(file) # 读取视频文件
frame_count = vc.get(cv2.CAP_PROP_FRAME_COUNT)
print("frameIndex:", frame_count)
c = 0
if vc.isOpened(): # 判断是否正常打开
rval = True
else:
rval = False
raise Exception("file error")
timeF = 1000 # 视频帧计数间隔频率
j = 0
i = int(frame_count / 5)
shotCount = 0
frameIndex = 0
imagePath = createGifPath + fileName + "\\"
while rval: # 循环读取视频帧
rval, frame = vc.read()
frameIndex += 1
if frameIndex >= i & i <= frame_count:
if (i - frameIndex) % 400 == 0:
print("current frame:", frameIndex)
cv2.imwrite(imagePath + str(frameIndex) + '.jpg', frame) # 存储为图像
shotCount += 1
if shotCount > 25:
break
i += 3000
cv2.waitKey(1)
vc.release()
print("==================================")
gifName = diction + '.gif'
GifUtil.images2Gif(imagePath, gifName)
file_count += 1
print("success")
@staticmethod
def getFrameCount(moviePath):
vc = cv2.VideoCapture(moviePath) # 读取视频文件
frame_count = vc.get(cv2.CAP_PROP_FRAME_COUNT)
return frame_count
@staticmethod
def videoToGif(moviePath=None, createGifPath=None,startFrameNumber=None, frameNumber=None, frameJump=None, frameTotal=None):
start = time.time()
fileName = os.path.basename(moviePath)
print("file name:", fileName)
diction = os.path.join(createGifPath, fileName)
print("mkdir:", diction)
newPath = Path(diction)
if os.path.exists(diction):
print("文件夹已存在")
else:
os.mkdir(diction)
# vc = cv2.VideoCapture("I:\123.mp4") # 读取视频文件
vc = cv2.VideoCapture(moviePath) # 读取视频文件
frame_count = vc.get(cv2.CAP_PROP_FRAME_COUNT)
print("frame_count:", frame_count)
if vc.isOpened(): # 判断是否正常打开
rval = True
else:
rval = False
raise Exception("file error")
# 开始截取的首帧
i = startFrameNumber
shotCount = 0
frameIndex = 0
# gif保存的路径
imagePath = os.path.join(createGifPath, fileName)
while rval: # 循环读取视频帧
rval, frame = vc.read()
frameIndex += 1
if frameIndex - i > frameNumber:
i = frameIndex + frameNumber
# print("current frame:{},i={}", frameIndex, i)
# 当前帧数大于需要截取的片段帧,并且小于总的帧数
if frameIndex >= i & i <= frame_count:
# 隔400帧截取一张
if (i - frameIndex) % frameJump == 0:
# print("shot,current frame:{},i={}", frameIndex, i)
# 截图的文件名
cv2.imwrite(os.path.join(imagePath, str(frameIndex)) + '.jpg', frame) # 存储为图像
print('截取帧:', frameIndex)
shotCount += 1
# 最大25张图片合成gif
if shotCount > frameTotal:
break
# 跳过片段再次截取图片
else:
continue
cv2.waitKey(1)
vc.release()
print("==================================耗时:", (time.time() - start) / 1000)
gifName = diction + '.gif'
GifUtil.images2Gif(imagePath, gifName)
print("success")
@staticmethod
def jpgToGif(moviePath=None, createGifPath=None):
fileName = os.path.basename(moviePath)
imagePath = os.path.join(createGifPath, fileName)
diction = os.path.join(createGifPath, fileName)
gifName = diction + '.gif'
GifUtil.images2Gif(imagePath, gifName)