# 视频拼接
import random
import os
from moviepy.editor import *
def someof(p_path, n_path):
"""
随机选取两个视频
:param p_path: p的path
:param n_path: n的path
:return: one_video
"""
p_list = [p_path + "\\" + i for i in os.listdir(p_path)]
n_list = [n_path + "\\" + i for i in os.listdir(n_path)]
# p_list = os.listdir(p_path)
# n_list = os.listdir(n_path)
# k:总个数,m:p的个数
res_list = []
res_file_list = []
c = []
while (len(p_list) > 5 and len(n_list) > 5):
k = random.randrange(3, 6)
m = random.randrange(1, k)
a = random.sample(p_list, m) # a:p的个数
b = random.sample(n_list, k - m) # n的个数
for i in a:
p_list.remove(i)
for i in b:
n_list.remove(i)
c = a + b
res_file_list.append(c)
d = []
random.shuffle(c)
for i in c:
if i in a:
d.append(1)
else:
d.append(-1)
res_list.append(d)
return res_file_list, res_list
def Splice_video(res_file_list, res_list, ):
with open("vf.txt1", 'w') as f:
for abc, res_file in enumerate(res_file_list):
if abc == 3:
break
all_index = []
temp = 1
L = []
print(res_list[abc])
for index,i in enumerate(res_list[abc]):
if index==0:
for _i in range(0, 4):
all_index.append(i)
temp = i
continue
if temp == i:
all_index.append(i)
else:
all_index.append(0)
for _i in range(0, 4):
all_index.append(i)
temp = i
print(all_index)
f.write(str(all_index)[1:-1]+"\n")
for _file in res_file:
clipVideo = VideoFileClip(_file)
L.append(clipVideo)
concatenate_videoclips(L, 'compose').write_videofile(r'output/{}.mp4'.format(str(abc).zfill(4)))
if __name__ == '__main__':
p_path = r"H:\datasets\Positive\video"
n_path = r"H:\datasets\Negative\video"
res_file_list, res_list = someof(p_path=p_path, n_path=n_path)
Splice_video(res_file_list, res_list)
10-11
1388
06-18
494
11-07
545
03-14
9546
03-22
546