用python进行视频剪辑

 更新时间:2020年11月02日 16:03:03   作者:huapyuan  
这篇文章主要介绍了如何用python进行视频剪辑,帮助大家更好的利用python处理视频,感兴趣的朋友可以了解下

一、目标

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

二、源码

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切割后再合并

2. 直接利用ffmpeg切割后,视频会出现黑屏、时间区间不准确、分辨率低

解决方案:用了各种命令也没有成功,所以放弃。。。

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

解决方案:统一保存为mp4

以上就是用python进行视频剪辑的详细内容,更多关于python 视频剪辑的资料请关注脚本之家其它相关文章!

相关文章

  • 常用python数据类型转换函数总结

    常用python数据类型转换函数总结

    这篇文章主要介绍了常用的python数据类型转换函数,并用实际例子说明了这些函数的用法,需要的朋友可以参考下
    2014-03-03
  • PyCharm中Python解释器如何选择详析

    PyCharm中Python解释器如何选择详析

    这篇文章主要给大家介绍了关于PyCharm中Python解释器如何选择的相关资料,文中详细分析了四种常见的Python环境管理工具,分别是venv、conda、pipenv和poetry,需要的朋友可以参考下
    2024-11-11
  • Python采用Django制作简易的知乎日报API

    Python采用Django制作简易的知乎日报API

    这篇文章主要为大家详细介绍了Python采用Django制作简易的知乎日报API,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • 使用Gitee自动化部署python脚本的详细过程

    使用Gitee自动化部署python脚本的详细过程

    小编最近在自学python,在学习过程中有好多意向不到的收获,真的很开心,今天重点给大家分享使用Gitee自动化部署python脚本的详细过程,包括安装环境搭建及一些注意事项,感兴趣的朋友跟随小编一起看看吧
    2021-05-05
  • Python中range函数的基本用法完全解读

    Python中range函数的基本用法完全解读

    range函数大多数时常出现在for循环中,在for循环中可做为索引使用,下面这篇文章主要给大家介绍了关于Python中range函数的基本用法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-01-01
  • Python中 map()函数的用法详解

    Python中 map()函数的用法详解

    map( )函数在算法题目里面经常出现,map( )会根据提供的函数对指定序列做映射,在写返回值等需要转换的时候比较常用。这篇文章主要介绍了Python中 map()的用法,需要的朋友可以参考下
    2018-07-07
  • 数据驱动测试DDT之Selenium读取Excel文件

    数据驱动测试DDT之Selenium读取Excel文件

    这篇文章主要为大家介绍了数据驱动测试DDT之Selenium读取Excel文件,
    2021-11-11
  • Python OpenCV一个窗口中显示多幅图像

    Python OpenCV一个窗口中显示多幅图像

    大家好,本篇文章主要讲的是Python OpenCV一个窗口中显示多幅图像,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2022-01-01
  • python beautiful soup库入门安装教程

    python beautiful soup库入门安装教程

    Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。今天通过本文给大家分享python beautiful soup库入门教程,需要的朋友参考下吧
    2021-08-08
  • Python之sklearn数据预处理中fit(),transform()与fit_transform()的区别

    Python之sklearn数据预处理中fit(),transform()与fit_transform()的区别

    这篇文章主要介绍了Python之sklearn数据预处理中fit(),transform()与fit_transform()的区别及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02

最新评论