视频段随机拼接

# 视频拼接
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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值