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)
相关推荐
行路见知13 分钟前
1.5 Go切片使用
开发语言·golang
子燕若水2 小时前
uv 安装包
开发语言·chrome·python
weixin_307779132 小时前
Python获取能唯一确定一棵给定的树的最少数量的拓扑序列
数据结构·python
A.sir啊2 小时前
爬虫基础(六)代理简述
爬虫·python·网络协议
weixin_307779132 小时前
PySPARK带多组参数和标签的SparkSQL批量数据导出到S3的程序
大数据·数据仓库·python·sql·spark
不会打代码呜呜呜呜3 小时前
小白零基础--CPP多线程
开发语言·c++·算法
涛ing3 小时前
【5. C++ 变量作用域及其深入探讨】
java·linux·c语言·开发语言·c++·ubuntu·vim
Hi Man4 小时前
Python之如何在Visual Studio Code 中写的python程序打包成可以在Windows系统下运行的.exe程序
开发语言·vscode·python
Return-Log4 小时前
Matplotlab显示OpenCV读取到的图像
python·opencv
程序趣谈4 小时前
算法随笔_36: 复写零
数据结构·python·算法