用python进行视频剪辑源码

需求

利用moviepy和pydub将一段视频进行区间切割

源码

python 复制代码
# -*- encoding: utf-8 -*-
import os
from moviepy.video.io.VideoFileClip import VideoFileClip
from pydub import AudioSegment


def clip_video(source_file, target_file, start_time, stop_time):
    """
    利用moviepy进行视频剪切
    :param source_file: 原视频的路径,mp4格式
    :param target_file: 生成的目标视频路径,mp4格式
    :param start_time: 剪切的起始时间点(第start_time秒)
    :param stop_time: 剪切的结束时间点(第stop_time秒)
    :return:
    """
    validate_file(source_file)
    source_video = VideoFileClip(source_file)
    video = source_video.subclip(int(start_time), int(stop_time))  # 执行剪切操作
    video.write_videofile(target_file)  # 输出文件


def clip_audio(source_file, target_file, start_time, stop_time):
    """
    利用pydub进行音频剪切。pydub支持源文件为 mp4格式,因此这里的输入可以与视频剪切源文件一致
    :param source_file: 原视频的路径,mp4格式
    :param target_file: 生成的目标视频路径,mp4格式
    :param start_time: 剪切的起始时间点(第start_time秒)
    :param stop_time: 剪切的结束时间点(第stop_time秒)
    :return:
    """
    validate_file(source_file)
    audio = AudioSegment.from_file(source_file, "mp4")
    audio = audio[start_time * 1000: stop_time * 1000]
    audio_format = target_file[target_file.rindex(".") + 1:]
    audio.export(target_file, format=audio_format)


def combine_video_audio(video_file, audio_file, target_file, delete_tmp=False):
    """
    利用 ffmpeg将视频和音频进行合成
    :param video_file:
    :param audio_file:
    :param target_file:
    :param delete_tmp: 是否删除剪切过程生成的原视频/音频文件
    :return:
    """
    validate_file(video_file)
    validate_file(audio_file)
    # 注:需要先指定音频再指定视频,否则可能出现无声音的情况
    command = "ffmpeg -y -i {0} -i {1} -vcodec copy -acodec copy {2}".format(audio_file, video_file, target_file)
    os.system(command)
    if delete_tmp:
        os.remove(video_file)
        os.remove(audio_file)


def clip_handle(source_file, target_file, start_time, stop_time, tmp_path=None, delete_tmp=False):
    """
    将一个视频文件按指定时间区间进行剪切
    :param source_file: 原视频文件
    :param target_file: 目标视频文件
    :param start_time: 剪切的起始时间点(第start_time秒)
    :param stop_time: 剪切的结束时间点(第stop_time秒)
    :param tmp_path: 剪切过程的文件存放位置
    :param delete_tmp: 是否删除剪切生成的文件
    :return:
    """
    # 设置临时文件名
    if tmp_path is None or not os.path.exists(tmp_path):
        # 如果没有指定临时文件路径,则默认与目标文件的位置相同
        tmp_path = target_file[: target_file.rindex("/") + 1]
    target_file_name = target_file[target_file.rindex("/") + 1: target_file.rindex(".")]
    tmp_video = tmp_path + "v_" + target_file_name + ".mp4"
    tmp_audio = tmp_path + "a_" + target_file_name + ".mp4"

    # 执行文件剪切及合成
    clip_video(source_file, tmp_video, start_time, stop_time)
    clip_audio(source_file, tmp_audio, start_time, stop_time)
    combine_video_audio(tmp_video, tmp_audio, target_file, delete_tmp)


def validate_file(source_file):
    if not os.path.exists(source_file):
        raise FileNotFoundError("没有找到该文件:" + source_file)


def test_example():
    """
    测试例子
    :return:
    """
    root_path = 'XXX/videos/'
    video_name = "test.mp4"
    source_file = root_path + video_name
    start_time = 5
    stop_time = 6

    # 设置目标文件名
    target_name = str(start_time) + "_" + str(stop_time)
    target_file = root_path + "c_" + target_name + ".mp4"
    # 处理主函数
    clip_handle(source_file, target_file, start_time, stop_time)


if __name__ == "__main__":
    test_example()

三、遇到的问题

  1. moviepy切割后的视频没有声音

解决方案:通过pydub切割后再合并

  1. 合并时,不支持mp3、 wav等格式

解决方案:统一保存为mp4

相关推荐
Ulyanov几秒前
打造现代化雷达电子对抗仿真界面 第二篇:雷达电子对抗仿真系统核心功能实现
前端·python·信息可视化·数据可视化·系统仿真·雷达电子战
财经资讯数据_灵砚智能3 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月12日
人工智能·python·信息可视化·自然语言处理·ai编程
测试秃头怪22 分钟前
python&selenium自动化测试实战项目详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
踏着七彩祥云的小丑28 分钟前
Python——字符串常用操作
开发语言·python
阿钱真强道31 分钟前
05 ComfyUI + SVD 系列(三):最小图生视频工作流拆解——节点含义、输入输出、参数作用与核心实验
python·aigc·stable-diffusion·svd·comfyui·工作流·图生视频
xcjbqd032 分钟前
Python中Pandas如何将DataFrame写入MySQL_使用to_sql函数
jvm·数据库·python
蓝色的杯子35 分钟前
Python面试30分钟突击掌握-LeetCode3-Linked list
python·leetcode·面试
ZC跨境爬虫1 小时前
海南大学交友平台开发实战 day10(后端向前端输出_前端读取数据全流程联调+日志调试落地)
前端·python·sqlite·html·状态模式
郝学胜-神的一滴1 小时前
从链表到二叉树:树形结构的入门与核心性质解析
数据结构·c++·python·算法·链表
yongyoudayee1 小时前
2026中国企业出海CRM:五大平台技术能力对比
后端·python·flask