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)
相关推荐
web1368856587121 分钟前
rust教程 第一章 —— 初识rust
开发语言·后端·rust
songroom22 分钟前
Rust : tokio中select!
开发语言·后端·rust
dubochao_xinxi24 分钟前
QT5 在某些系统出现qt.qpa.xcb: could not connect
开发语言·qt
blueman888826 分钟前
QWidget应用封装为qt插件,供其他qt应用调用
开发语言·qt
qq_2739002326 分钟前
PyTorch Lightning Callback介绍
人工智能·pytorch·python
qincjun27 分钟前
Qt仿音乐播放器:设置窗口、部件属性
开发语言·qt
编码小哥36 分钟前
C++线程同步和互斥
开发语言·c++
qincjun38 分钟前
Qt仿音乐播放器:动画类
开发语言·qt
L.S.V.1 小时前
Java 溯本求源之基础(三十)——封装,继承与多态
java·开发语言
游客5201 小时前
设计模式-创建型-工厂方法模式
开发语言·python·设计模式·工厂方法模式