-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideio_spilit_global-通用-多进程.py
149 lines (113 loc) · 4.46 KB
/
videio_spilit_global-通用-多进程.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 2 19:29:11 2021
@author: yejohnny
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import cv2
import os
import time
from multiprocessing import Pool
from multiprocessing import Process
import pandas as pd
from tqdm import tqdm
##############################################################################
def video_split(file, start_frame, end_frame, outpath):
cap = cv2.VideoCapture(file)
fps = cap.get(cv2.CAP_PROP_FRAME_COUNT)
start_frame = start_frame # 开始帧,从1开始计数
end_frame = end_frame # 结束帧
# 获取视频分辨率
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# 输出文件编码,Linux下可选X264
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
# 视频帧率
fps = cap.get(cv2.CAP_PROP_FPS)
success, image = cap.read()
count = 0
success = True
# 从视频开头获取每一帧,直到到达开始帧
while success:
success, image = cap.read()
count += 1
if (count == start_frame):
success = False
# 开始帧的时间(单位ms),相当于ffmpeg的ss参数
ss = int(cap.get(cv2.CAP_PROP_POS_MSEC))
# 输出
out = cv2.VideoWriter(outpath, fourcc, fps, size)
# 读取开始帧到结束帧的每一帧并写入新视频
while (count < end_frame):
success, image = cap.read()
out.write(image)
count += 1
# 结束帧的时间,相对于ffmpeg的to参数
to = int(cap.get(cv2.CAP_PROP_POS_MSEC))
cap.release()
out.release()
info_df = pd.read_csv(
'H:/Z01_3D_behavior/SP_square_box/result/df_nearest1.csv')
raw_video_dir = 'H:/Z01_3D_behavior/SP_square_box/split_video' # 输入视频存放的文件夹
output_dir = 'H:/Z01_3D_behavior/SP_square_box/video_resplit'
### 获取输入文件夹下待切割视频路径
raw_video_path = {}
file_name_list = info_df['file'].unique()
for file_name in file_name_list:
raw_video_path.setdefault(file_name, [])
for v_file_name in os.listdir(raw_video_dir):
if v_file_name.endswith('.avi'):
if v_file_name.startswith(file_name):
raw_video_path[file_name].append(raw_video_dir +'/'+ v_file_name)
if __name__ == '__main__':
t1 = time.time()
pool = Pool(5)
for i in info_df.index:
label = info_df.loc[i, 'movement_label']
boundary = info_df.loc[i, 'boundary']
file_name = info_df.loc[i, 'file']
start_frame = int(boundary.split(',')[0].strip('('))
end_frame = int(boundary.split(',')[1].strip(')')) + 1
video_list = raw_video_path[file_name]
video_list.sort()
input_file = video_list[2]
sub_output_dir = output_dir + '/{0}/{1}'.format(label, input_file[-12:-4])
folder = os.path.exists(sub_output_dir)
if not folder:
os.makedirs(sub_output_dir)
#print('--- establish {} ---\n'.format(sub_output_dir))
output_file = sub_output_dir + '/{0}_{1}_{2}.avi'.format(file_name, start_frame, end_frame)
pool.apply_async(video_split, args=(input_file, start_frame, end_frame, output_file))
pool.close()
pool.join()
t2 = time.time()
print('\n finish! Total time:{:.2f} min \n'.format((t2 - t1) / 60))
'''
### 开始循环处理视频
print('processing data ......\n')
p = Pool(6)
for i in info_df.index:
label = info_df.loc[i, 'movement_label']
boundary = info_df.loc[i, 'boundary']
file_name = info_df.loc[i, 'file']
start_frame = int(boundary.split(',')[0].strip('('))
end_frame = int(boundary.split(',')[1].strip(')')) + 1
video_list = raw_video_path[file_name]
for video_path in video_list[2:4]:
input_file = video_path
sub_output_dir = output_dir + '/{0}/{1}'.format(label, video_path[-12:-4])
folder = os.path.exists(sub_output_dir)
if not folder:
os.makedirs(sub_output_dir)
#print('--- establish {} ---\n'.format(sub_output_dir))
output_file = sub_output_dir + '/{0}_{1}_{2}.avi'.format(file_name, start_frame, end_frame)
#video_split(input_file, start_frame, end_frame, output_file)
p.apply_async(video_split, args=(input_file, start_frame, end_frame, output_file))
print(video_path,output_file)
p.close()
p.join()
t2 = time.time()
print('\n finish! Total time:{:.2f} min \n'.format((t2 - t1) / 60))
'''