1. 使用实例
2. 安装库
pip install moviepy
实质访问的下载网站是https://pypi.Python.org/simple/,这是一个国外网站,速度比较慢
如果使用国外的源下载速度慢,我们可以使用国内源提速:
- 豆瓣源
pip3 install moviepy -i https://pypi.douban.com/simple
- 清华大学源
pip3 install moviepy -i https://pypi.tuna.tsinghua.edu.cn/simple
3. 导入库
from moviepy.editor import VideoFileClip
主页:https://pypi.org/project/moviepy/
4. 读取视频到内存
video = VideoFileClip("a.mp4")
5. 打印视频的属性
print(video.fps)
print(video.h)
print(video.w)
6. 保存视频
video.write_videofile("b.mp4", fps=15)
Help on method write_videofile in module moviepy.video.VideoClip:
write_videofile(filename, fps=None, codec=None, bitrate=None, audio=True, audio_fps=44100, preset='medium', audio_nbytes=4, audio_codec=None, audio_bitrate=None, audio_bufsize=2000, temp_audiofile=None, rewrite_audio=True, remove_temp=True, write_logfile=False, verbose=True, threads=None, ffmpeg_params=None, logger='bar') method of moviepy.video.io.VideoFileClip.VideoFileClip instance
Write the clip to a videofile.
Parameters
-----------
filename
Name of the video file to write in.
The extension must correspond to the "codec" used (see below),
or simply be '.avi' (which will work with any codec).
fps
Number of frames per second in the resulting video file. If None is
provided, and the clip has an fps attribute, this fps will be used.
codec
Codec to use for image encoding. Can be any codec supported
by ffmpeg. If the filename is has extension '.mp4', '.ogv', '.webm',
the codec will be set accordingly, but you can still set it if you
don't like the default. For other extensions, the output filename
must be set accordingly.
Some examples of codecs are:
``'libx264'`` (default codec for file extension ``.mp4``)
makes well-compressed videos (quality tunable using 'bitrate').
``'mpeg4'`` (other codec for extension ``.mp4``) can be an alternative
to ``'libx264'``, and produces higher quality videos by default.
``'rawvideo'`` (use file extension ``.avi``) will produce
a video of perfect quality, of possibly very huge size.
``png`` (use file extension ``.avi``) will produce a video
of perfect quality, of smaller size than with ``rawvideo``.
``'libvorbis'`` (use file extension ``.ogv``) is a nice video
format, which is completely free/ open source. However not
everyone has the codecs installed by default on their machine.
``'libvpx'`` (use file extension ``.webm``) is tiny a video
format well indicated for web videos (with HTML5). Open source.
audio
Either ``True``, ``False``, or a file name.
If ``True`` and the clip has an audio clip attached, this
audio clip will be incorporated as a soundtrack in the movie.
If ``audio`` is the name of an audio file, this audio file
will be incorporated as a soundtrack in the movie.
audiofps
frame rate to use when generating the sound.
temp_audiofile
the name of the temporary audiofile to be generated and
incorporated in the the movie, if any.
audio_codec
Which audio codec should be used. Examples are 'libmp3lame'
for '.mp3', 'libvorbis' for 'ogg', 'libfdk_aac':'m4a',
'pcm_s16le' for 16-bit wav and 'pcm_s32le' for 32-bit wav.
Default is 'libmp3lame', unless the video extension is 'ogv'
or 'webm', at which case the default is 'libvorbis'.
audio_bitrate
Audio bitrate, given as a string like '50k', '500k', '3000k'.
Will determine the size/quality of audio in the output file.
Note that it mainly an indicative goal, the bitrate won't
necessarily be the this in the final file.
preset
Sets the time that FFMPEG will spend optimizing the compression.
Choices are: ultrafast, superfast, veryfast, faster, fast, medium,
slow, slower, veryslow, placebo. Note that this does not impact
the quality of the video, only the size of the video file. So
choose ultrafast when you are in a hurry and file size does not
matter.
threads
Number of threads to use for ffmpeg. Can speed up the writing of
the video on multicore computers.
ffmpeg_params
Any additional ffmpeg parameters you would like to pass, as a list
of terms, like ['-option1', 'value1', '-option2', 'value2'].
write_logfile
If true, will write log files for the audio and the video.
These will be files ending with '.log' with the name of the
output file in them.
logger
Either "bar" for progress bar or None or any Proglog logger.
verbose (deprecated, kept for compatibility)
Formerly used for toggling messages on/off. Use logger=None now.
Examples
========
>>> from moviepy.editor import VideoFileClip
>>> clip = VideoFileClip("myvideo.mp4").subclip(100,120)
>>> clip.write_videofile("my_new_video.mp4")
>>> clip.close()