掌握Python的X篇_29_使用python做视频剪辑_moviepy

从本篇开始,将会给大家介绍一些好用的第三方库。

文章目录

  • [1. moviepy简介](#1. moviepy简介)
    • 1.1安装
    • [1.2 文档:](#1.2 文档:)
    • [1.3 导入](#1.3 导入)
  • [2. 提取音频](#2. 提取音频)
  • [3. 剪辑视频](#3. 剪辑视频)

1. moviepy简介

moviepy是一个视频编辑库,如果只是剪辑、加些字幕等简单操作,使用这个库已经足够了

1.1安装

bash 复制代码
pip install moviepy

1.2 文档:

https://zulko.github.io/moviepy/

作为其引入介绍,主要介绍提取音频提取视频的方法

1.3 导入

moviepy有很多的子模块,以下是使用到的

bash 复制代码
from moviepy.editor import *

使用moviepy中的VideoFileClip的方法,可以通过文件构造对象。在构造得到的视频对象中,有很多方法

2. 提取音频

构造视频对象:sampleMP4.mp4是我放到文件夹的视频文件

bash 复制代码
clip = VideoFileClip("./sampleMP4.mp4")

clip中的各种方法和成员,对应了视频的属性与操作。

比如:

  • duration:视频长度clip.duration
  • audio:它代表音频,音频对象中又有方法对应了操作。

以下操作是将上述视频的音频进行了提取:

bash 复制代码
In [13]: clip.audio.write_audiofile("output.mp3")
MoviePy - Writing audio in output.mp3
MoviePy - Done.

3. 剪辑视频

视频对象中的subclip方法,可以返回视频片段:

bash 复制代码
In [15]: clip.subclip?
Signature: clip.subclip(t_start=0, t_end=None)
Docstring:
Returns a clip playing the content of the current clip
between times ``t_start`` and ``t_end``, which can be expressed
in seconds (15.35), in (min, sec), in (hour, min, sec), or as a
string: '01:03:05.35'.
If ``t_end`` is not provided, it is assumed to be the duration
of the clip (potentially infinite).
If ``t_end`` is a negative value, it is reset to
``clip.duration + t_end. ``. For instance: ::

    >>> # cut the last two seconds of the clip:
    >>> newclip = clip.subclip(0,-2)

If ``t_end`` is provided or if the clip has a duration attribute,
the duration of the returned clip is set automatically.

The ``mask`` and ``audio`` of the resulting subclip will be
subclips of ``mask`` and ``audio`` the original clip, if
they exist.
File:      e:\program files\python\python38\lib\site-packages\moviepy\clip.py
Type:      method

实现剪辑功能代码:

bash 复制代码
In [20]: mysubclip = clip.subclip(15.00,40.00) #获取片段对象
In [21]: mysubclip.write_videofile("./output.mp4") #将片段写入文件

moviepy还有一些其他的高级操作,例如合并字幕,进行剪辑...,本篇抛砖引玉。

4.学习视频地址: 使用python做视频剪辑_moviepy

相关推荐
郝学胜-神的一滴几秒前
QStyleOption:Qt样式系统之魂
开发语言·c++·qt·程序人生
木卫二号Coding1 分钟前
第七十二篇-V100-32G+WebUI+Flux.1-Schnell+Lora+文生图
开发语言·人工智能·python
墨笔之风2 分钟前
基于python 实现的小游戏
开发语言·python·pygame
予枫的编程笔记2 分钟前
Elasticsearch 全面解析:从原理到实战的分布式搜索引擎指南
java·开发语言·分布式·后端·elasticsearch·搜索引擎·全文检索
多米Domi0112 分钟前
0x3f 第24天 黑马web (安了半天程序 )hot100普通数组
数据结构·python·算法·leetcode
BoBoZz193 分钟前
AnatomicalOrientation 3D人体模型及三个人体标准解剖学平面展示
python·vtk·图形渲染·图形处理
枫叶丹43 分钟前
【Qt开发】Qt系统(四)-> Qt文件
c语言·开发语言·c++·qt
love530love4 分钟前
EPGF 新手教程 11在 PyCharm(中文版 GUI)中创建 uv 环境,并把 uv 做到“项目自包含”(工具本地化为必做环节)
ide·人工智能·python·pycharm·conda·uv·epgf
jackylzh5 分钟前
cmd或其它终端的dos命令 & events.out.tfevents文件怎么打开
python
gis_rc5 分钟前
python下shp转3dtiles
python·3d·cesium·3dtiles·数字孪生模型