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)
相关推荐
legend_jz25 分钟前
【Linux】线程控制
linux·服务器·开发语言·c++·笔记·学习·学习方法
drebander37 分钟前
使用 Java Stream 优雅实现List 转化为Map<key,Map<key,value>>
java·python·list
tangliang_cn1 小时前
java入门 自定义springboot starter
java·开发语言·spring boot
程序猿阿伟1 小时前
《智能指针频繁创建销毁:程序性能的“隐形杀手”》
java·开发语言·前端
新知图书1 小时前
Rust编程与项目实战-模块std::thread(之一)
开发语言·后端·rust
威威猫的栗子1 小时前
Python Turtle召唤童年:喜羊羊与灰太狼之懒羊羊绘画
开发语言·python
力透键背1 小时前
display: none和visibility: hidden的区别
开发语言·前端·javascript
bluefox19791 小时前
使用 Oracle.DataAccess.Client 驱动 和 OleDB 调用Oracle 函数的区别
开发语言·c#
ö Constancy1 小时前
c++ 笔记
开发语言·c++
墨染风华不染尘2 小时前
python之开发笔记
开发语言·笔记·python