python 拆分视频并且整合(速度慢,会保存每个分段视频)

复制代码
# coding=utf-8
__author__ = 'Administrator'

# pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

import os
import cv2
import time
from moviepy.editor import*

def cut_movie(source_path,desc_path,start,end):
    print(source_path,desc_path,start,end)
    video =CompositeVideoClip([VideoFileClip(source_path).subclip(start,end)])
    video.write_videofile(desc_path)

def get_movie_length(path):
    cap = cv2.VideoCapture(path)
    if cap.isOpened():
        rate = cap.get(5)
        frame_num = cap.get(7)
        duration = frame_num / rate
        item = int(duration)
    return item

def get_str_for_num(num):
    return '%04d' % num

def cut_one_movie(source_path,out_dir_path,time_list):
    desc_path_list=[]
    start_time=time.time()

    index= source_path.rindex("/")
    dir_path= source_path[0:index]
    filename= source_path[index+1:]
    print(dir_path)
    print(filename)
    single_index = filename.index(".")
    filename0=filename[0:single_index]
    filename1=filename[single_index:]
    print(filename0)
    print(filename1)

    for i in range(1,len(time_list)+1):
        st= time_list[i-1][0]
        end= time_list[i-1][1]
        desc_path=out_dir_path+filename0+"_"+get_str_for_num(i)+filename1
        cut_movie(source_path,desc_path,st,end)
        desc_path_list.append(desc_path)
        print("desc_path:" + desc_path)

    end_time=time.time()
    cost_time = end_time-start_time
    print("cost_time",cost_time)
    return desc_path_list


def main_step(source_dir,out_dir_path):
    files = os.listdir(source_dir)
    for f in files:
        # full_file_name = os.path.join(source_dir,f)
        full_file_name = source_dir+r"/"+f
        print(full_file_name)
        cut_one_movie(full_file_name,out_dir_path)

def combine_list_video(list_path, out_path):

    clip_list = []
    for i in list_path:
        clip_list.append(VideoFileClip(i))
    # clip1 = VideoFileClip(path1)
    # clip2 = VideoFileClip(path2)
    # 结合剪辑,你甚至能够完全自动化剪辑拼接视频的操作
    # clip2 =VideoFileClip("myvideo2.mp4").subclip(50,60)

    final_clip = concatenate_videoclips(clip_list)
    # final_clip = concatenate_videoclips([clip1,clip2,clip3])
    final_clip.write_videofile(out_path)

def print_time():
    now = time.time()
    time_str = time.strftime("%Y-%m-%D %H:%M:%S", time.localtime(now))
    print(time_str)

if __name__ == '__main__':
    out_dir_path=r"D:/1/biaoqing/"
    source_path = r'D:/1/biaoqing/video.mp4'

    out_file = r'D:/1/biaoqing/result.mp4'
    # time_list=[(1260,1275),(1140,1320)]
    # time_list=[(1850,1870),(1790,1997)]
    time_list=[(0,16),(37,45),(66,71)]
    desc_path_list = cut_one_movie(source_path,out_dir_path,time_list)
    combine_list_video(desc_path_list,out_file)
相关推荐
莓事哒2 分钟前
selenium和pytessarct提取古诗文网的验证码(python爬虫)
爬虫·python·selenium·测试工具·pycharm
-代号952719 分钟前
【JavaScript】十三、事件监听与事件类型
开发语言·javascript·ecmascript
virelin_Y.lin25 分钟前
系统与网络安全------Windows系统安全(7)
windows·web安全·系统安全·ftp
q5673152334 分钟前
使用puppeteer库编写的爬虫程序
爬虫·python·网络协议·http
mosquito_lover139 分钟前
Python数据分析与可视化实战
python·数据挖掘·数据分析
写代码的小王吧41 分钟前
【Java可执行命令】(十)JAR文件签名工具 jarsigner:通过数字签名及验证保证代码信任与安全,深入解析 Java的 jarsigner命令~
java·开发语言·网络·安全·web安全·网络安全·jar
eqwaak044 分钟前
量子计算与AI音乐——解锁无限可能的音色宇宙
人工智能·爬虫·python·自动化·量子计算
SylviaW081 小时前
python-leetcode 63.搜索二维矩阵
python·leetcode·矩阵
小卡皮巴拉1 小时前
【力扣刷题实战】矩阵区域和
开发语言·c++·算法·leetcode·前缀和·矩阵
努力搬砖的咸鱼1 小时前
Qt中的数据解析--XML与JSON处理全攻略
xml·开发语言·qt·json