-
Notifications
You must be signed in to change notification settings - Fork 2
/
output_video.py
86 lines (71 loc) · 2.3 KB
/
output_video.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
"""
author: Jet Chien
GitHub: https://github.com/jet-chien
Create Date: 2021/1/14
"""
# coding: utf-8
import ntpath
from typing import Union
import cv2
from tqdm import tqdm
from imutils.paths import list_images
def mk_video(opt):
print('start')
vt = VideoTool()
origin_video = vt.get_video_meta(opt.input_video)
print(origin_video)
vt.images_to_video('results/frame', opt.output, 60)
class VideoTool:
CLIP_FILE_INDENT = 3
CLIP_DURATION = 60
OUTPUT_DEFAULT_FPS = 24
@staticmethod
def get_video_name(f: str) -> (str, str):
f_data = ntpath.basename(f).split('.')
return f_data[0], f_data[-1]
@staticmethod
def get_fourcc(fn: str) -> int:
_, ext = VideoTool.get_video_name(fn)
if ext == 'mp4':
return cv2.VideoWriter_fourcc(*'mp4v')
elif ext == 'avi':
return cv2.VideoWriter_fourcc(*'XVID')
else:
return cv2.VideoWriter_fourcc(*'mp4v')
@staticmethod
def images_to_video(img_list: Union[str, list], output_path: str, fps=OUTPUT_DEFAULT_FPS):
img_path_ls = list()
if isinstance(img_list, str):
img_path_ls = list(list_images(img_list))
img_path_ls.sort()
msg = "loading images..."
print(msg)
img_queue = list()
for img_path in tqdm(img_path_ls):
img_queue.append(cv2.imread(img_path))
fourcc = VideoTool.get_fourcc(output_path)
height, width, _ = img_queue[0].shape
size = (width, height)
out = cv2.VideoWriter(output_path, fourcc, fps, size)
msg = "writing images..."
print(msg)
for frame in tqdm(img_queue):
out.write(frame)
out.release()
@staticmethod
def get_video_meta(video_file: str) -> dict:
result = dict()
v = cv2.VideoCapture(video_file)
width = int(v.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(v.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = v.get(cv2.CAP_PROP_FPS)
result['width'] = width
result['height'] = height
result['fps'] = fps
return result
if __name__ == '__main__':
print('start')
vt = VideoTool()
origin_video = vt.get_video_meta('results/video_2.mp4')
print(origin_video)
vt.images_to_video('results/frame', 'output.mp4', 60)