One of the video processing modules in python is called moviepy, and today we're going to talk about it.
The module can be installed using the following command
pip install moviepy
Next, to complete the first Demo, to improve confidence for subsequent learning, capture a section of the video.
Intercept video
The following code focuses on the use ofsubclip
method, but you need to declare aVideoFileClip
Object.VideoFileClip
Class constructors are described after the code.
from import * import time clip = VideoFileClip('./1644974996.mp4').subclip(10, 20) new_file = str(int(())) + '_subclip.mp4' clip.write_videofile(new_file)
The result of running the code is shown below:
subclip(t_start,t_end)
The time parameter in the method can be used with the(t_start=10)
, which is expressed in seconds of time, can also be expressed as a(t_start=(1,20))
, in the form of 1 minute and 20 seconds, can also be(t_start=(0,1,20))
or(t_start=(00:01:20))
, byHours: Minutes: Secondsis expressed in the form of thet_end
The default value is the length of the video
VideoFileClip
The constructor for the class is shown below:
__init__(self, filename, has_mask=False, audio=True, audio_buffersize=200000, target_resolution=None, resize_algorithm='bicubic', audio_fps=44100, audio_nbytes=2, verbose=False, fps_source='tbr')
Of these, onlyfilename
are required fields, the rest are optional.
-
filename
: Video file name, generally common formats are supported; -
has_mask
: Whether the mask is included or not; -
audio
: Whether or not to load audio; -
audio_buffersize
: Audio buffer size; -
target_resolution
: The resolution to transform to after loading; -
resize_algorithm
: Algorithm for adjusting resolution, defaultbicubic
Thebilinear
,fast_bilinear
; -
audio_fps
: The sampling frequency of the sound; -
audio_nbytes
: The number of bits sampled; -
verbose
: Whether to output processing information.
subclip(t1,t2)
The meaning of the method is to intercept the segment in the time period t1 to t2.write_videofile()
method for video output.
Extract the audio from video A and inject it into video B
from import * # Read 2 video files videoclip_a = VideoFileClip("1644974996.mp4") videoclip_b = VideoFileClip("1644974998.mp4") # Extract the audio portion of the A video file audio_a = videoclip_a.audio # Set the audio for B. Note that the final size of the video composite will be based on the length of the video videoclip_c = videoclip_b.set_audio(audio_a) # Output a new video file videoclip_c.write_videofile("videoclip_c.mp4")
utilizationVideoFileClip
targetaudio
property to get the audio portion of the video and then call theset_audio()
method for audio settings on the file, one thing to note here is that the synthesized audio and video are equal to the length of theelderThe.
Remove video sound
from import * video = VideoFileClip('1644974996.mp4') video = video.without_audio() video.write_videofile('cc.mp4')
without_audio()
The method is known by its name - removing the sound.
Get video information
The resolution and time of the video can be read directly from the properties
from import * video = VideoFileClip('1644974996.mp4') print(dir(video)) print() # Get resolution print() # Get the total duration of the video
Getting the file size is relatively simple, using theos
modular()
method will do, and you get the byte size.
from import * video = VideoFileClip('1644974996.mp4') size = ('1644974996.mp4') print(size)
Setting up video multiplier playback
Read the video, call thespeedx()
method, which sets the multiplier to accelerate to.
from import * clip = VideoFileClip('./1644974996.mp4') video_1 = (2) video_1.write_videofile('sss.mp4')
Screenshot video cover
Many times we need to generate the cover of the video, directly using the following lines of code, can be achieved.
from import * clip = VideoFileClip('./1644974996.mp4') clip.save_frame("") # Save frame 1 clip.save_frame("", t=2) # save (a file etc) (computing)2sMoment of that1one of a pair (scrolls)
Extract audio content from videos
If you want to intercept a certain audio segment from the target video, you can use the following code
from import * clip = VideoFileClip('./1644974996.mp4').subclip(10, 20) audioclip1 = # Extract audio from video objects audioclip1.write_audiofile('a.mp3') # Writing audio files
Take a clip from a video and save it as a gif
from import * clip = VideoFileClip('./1644974996.mp4').subclip(10, 20) # clip.write_gif('',fps=15) # file size after generation clip.write_gif('',fps=5) # The generated file is small
Write it on the back.
By now, I'm sure you'll be able to learn more advanced uses of moviepy along the way.
to this article on the use of python moviepy introductory article is introduced to this, more related to the use of python moviepy content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!