剪切视频

from moviepy.video.io.VideoFileClip import VideoFileClip
import sys
from functools import reduce


def to_seconds(x, y):
    return 60 * x + y


def main(source):
    print('要剪切的视频是', source[1])  # 获取需要剪切的文件
    source = source[1]
    time = input('请输入开始和结束的时间:\n')  # 获取开始剪切时间
    time = time.split()
    times = []
    while time != []:

        start_time = time.pop(0)
        stop_time = time.pop(0)
        times.append([start_time, stop_time])

    print("子视频命名方式为原名称_段数.mp4")
    temp = source.split('.')
    target = []
    num_of_cuts = len(times)
    for i in list(range(1, num_of_cuts + 1)):
        target.append(temp[0] + str(i) + '.' + temp[1])

    # print('**--**',target)
    video = VideoFileClip(source)
    for i in list(range(0, num_of_cuts, 1)):
        current_time = times[i]
        print('cutting...\n')

        temp = list(map(int, current_time[0].split(":")))
        start_time = reduce(to_seconds, temp)
        temp = list(map(int, current_time[1].split(":")))
        stop_time = reduce(to_seconds, temp)
        new_video = video.subclip(start_time, stop_time)  # 执行剪切操作
        new_video.to_videofile(target[i], fps=29, remove_temp=True)  # 输出文件

    video.close()
    print('done ^_^\n')


if __name__ == "__main__":
    main(sys.argv)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 天价的藏獒 我家一直都没有养过小猫小狗这一类的动物,带毛的在我印象里只有小时候养过的鸽子、鹦鹉和鸡。 可能是一直没...
    自我解析实验室阅读 339评论 0 0
  • 当今社会,大学生多如牛毛,读研成了很多大学生的第一选择,硕士研究生学历在很多情况下成为了找工作的敲门砖,甚至在有些...
    彧荻阅读 1,021评论 10 9
  • 前言 很多工程师做开发3年甚至5年,到现在都无法自己写出架构代码。归根原因是 1. 架构基础知识匮乏 2. 没有...
    热血沸腾阅读 609评论 0 0