复制代码
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
156import base64 import cv2 import numpy as np from io import BytesIO class ImageOperation(object): def __init__(self): pass @staticmethod def cv2_base64(image): """ cv2.imdecode()函数从指定的内存缓存中读取数据,并把数据转换(解码)成图像格式;主要用于从网络传输数据中恢复出图像。 cv2.imencode()函数是将图片格式转换(编码)成流数据,赋值到内存缓存中;主要用于图像数据格式的压缩,方便网络传输。 """ base64_str = cv2.imencode('.jpg', image)[1].tostring() base64_str = base64.b64encode(base64_str) return base64_str def base64_to_bytes(self, base64_str): """ base64 TO bytes """ return base64.urlsafe_b64decode(base64_str) @staticmethod def image_buffer_to_cv2_img(image_buffer): """ cv2.imencode()函数是将图片格式转换(编码)成流数据,赋值到内存缓存中;主要用于图像数据格式的压缩,方便网络传输。 # npmpy.frombuffer将data以流的形式读入转化成ndarray对象 # 第一参数为stream,第二参数为返回值的数据类型,第三参数指定从stream的第几位开始读入 """ img = cv2.imdecode(np.frombuffer(image_buffer, np.uint8), cv2.IMREAD_COLOR) return img def image_spilt_rect(self, img, face_rect): """ 调用切图并合成base64 """ img_base64_bytes = self.split_image(img, face_rect) rect_image_str = self.gen_base_64_str_by_bytes(img_base64_bytes) return rect_image_str @staticmethod def gen_base_64_str_by_bytes(img_base64_bytes): """ 合成图片的base64编码 """ image_str = f'data:image/png;base64,{str(img_base64_bytes, encoding="utf-8")}' return image_str def split_image(self, img, spilt_rect): """ 切图 """ # spilt_rect: [x,y,w,h] x, y, w, h = spilt_rect split_image = img[y:y + h, x:x + w] img_base64_str = self.cv2_base64(split_image) return img_base64_str @staticmethod def replace_img_rect(img, replace_img, rect_area): """ 替换图片中的某区域 """ x, y, w, h = rect_area # print(x, y, w, h, type(replace_img), type(img)) img[y:y + h, x:x + w] = replace_img return img def color_imgread(self, img_path): """ 彩色读取 """ # img = cv2.imread(img_path, cv2.IMREAD_COLOR) img = cv2.imread(img_path, 1) return img def grayscale_imgread(self, img_path): """ 灰度读取 """ # img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) img = cv2.imread(img_path, 0) return img def get_video_duration(filename): """ python获取视频文件时长 """ cap = cv2.VideoCapture(filename) if cap.isOpened(): rate = cap.get(5) frame_num =cap.get(7) duration = frame_num/rate return duration return -1 if __name__ == '__main__': pass # pic_ope = ImageOperation() # # # 二进制读取图片 # with open('./2.jpeg', 'rb') as f: # img_bytes = f.read() # print(f"img_bytes = {img_bytes}") # # # 将图片格式转换(编码)成流数据,赋值到内存缓存中 # cv2_img = pic_ope.image_buffer_to_cv2_img(img_bytes) # print(f"cv2_img = {cv2_img}") # # # 从图片中切下一部分 # big_area = [319, 55, 84, 117] # face_area = [340, 84, 42, 59] # split_res = pic_ope.image_spilt_rect(cv2_img, big_area) # print(f"split_res = {split_res}") # # # 读取另一张图,切图 # with open('./1.png', 'rb') as f: # img_bytes2 = f.read() # print(f"img_bytes2 = {img_bytes2}") # # # 将图片格式转换(编码)成流数据,赋值到内存缓存中 # cv2_img2 = pic_ope.image_buffer_to_cv2_img(img_bytes2) # print(f"cv2_img2 = {cv2_img2}") # # # 从图片中切下一部分 # big_area = [319, 55, 84, 117] # split_res2 = pic_ope.split_image(cv2_img2, big_area) # print(f"split_res2 = {split_res2}") # split_res2_bytes = pic_ope.base64_to_bytes(split_res2) # print(f"split_res2_bytes = {split_res2_bytes}") # # split_res2_cv2_img = pic_ope.image_buffer_to_cv2_img(split_res2_bytes) # print(f"split_res2_cv2_img = {split_res2_cv2_img}") # # # 替换图片的某部分 # img = pic_ope.replace_img_rect(cv2_img, split_res2_cv2_img, big_area) # print(f"img = {img}") # output_str = pic_ope.cv2_base64(img) # print(f"output_str = {output_str}") # # # 转换成base64编码 # res = pic_ope.gen_base_64_str_by_bytes(output_str) # print(f"res = {res}") file_path = './output (1).mp4' res = get_video_duration(file_path) print(f"视频时长: {res} s")
最后
以上就是彩色母鸡最近收集整理的关于python cv2 - 获取视频文件播放时长的全部内容,更多相关python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复