python Implementation of Audio and Video Editing Based on moviepy
- 2021-11-10 10:19:11
- OfStack
Complete code
Reference
Practical steps
1. Find a suitable Python library (is it troublesome to install, easy to use, and will it take too long to execute)
moviepy Audio and Video Library. API needed for analysis: code example2. Define input and output
Input: The address of 1 audio and video file and the time period to be cut out Output: Files of clips3. Design the execution process and implement it step by step (define the function, which is related to using the specific API)
Read in and create an clip object. Clip subclip, the input time parameter can be a string in time format. Exports write_videofile.4. Conclusion: If the time is too long, it will take as long as the fragment is; CPU is full.
There is an answer in stackoverflowConcat videos too slow using Python MoviePY saying that it is faster to call the ffmpeg function encapsulated in the package:
You have some functions that perform direct calls to ffmpeg: github. com/Zulko/movie … And are therefore extremely efficient, for ES60tasks such as yours.
5. Redesign and implement, directly using the function in moviepy. video. io. ffmpeg_tools: ffmpeg_extract_subclip (source audio and video file, start, end, output name).
The starting and ending time parameters entered in this function can only be numbers, not strings, while the interface functions used by the library basically pass in strings. Look at the source found that there is a time string into a number of decorators, step by step to find that conversion function.6. Conclusion: Time is much faster, almost in a few seconds.
But I don't understand why it's so fast7. Optimize: Process multiple time periods at a time
Enter from 1 starting and ending time to 1 group starting and ending time Cyclic processing of starting and ending time of each group Output file names are spliced in sequence8. Optimize: Add a name every time
Enter the suffix name in addition to the start and end time of each group File name + suffix to get the output file name9. Optimization: Validation of input and output
Verify that the input address is a legal file Verification time period (not necessary)Can't be less than 0 Can't be longer than video time Starting less than ending
Complete code
Need
pip install moviepy
Simple use
from moviepy.editor import VideoFileClip, concatenate_videoclips
clipOri = VideoFileClip("E:/2020-03-29 19-31-39.mkv")
# Intercept two subclip Re-splicing
#time_length = int(clipOri.duration) This sentence can get the time of the film
# If it exceeds the time limit, an error will be reported. The time limit is seconds by default, or it can be written in more detail. (00:03:50.54)->3 Points 50 Seconds
cut1 = clipOri.subclip(0, 7053)
cut2 = clipOri.subclip(7059, 8941)
finalClip = concatenate_videoclips([cut1,cut2])
finalClip.write_videofile("E:/acut.mp4")
import os
import moviepy.video.io.ffmpeg_tools as fftool
from moviepy.tools import cvsecs
def add_suffix(file_name, suffix): # File name splicing suffix
index = file_name.rfind('.') # Finally 1 Point number
res = file_name[:index] + '_' + suffix + file_name[index:]
return res
# Input
file_name = r"./XXX.mkv"
output_arr = [
('04:20','05:07', ' Introduce oneself '),
('05:07','17:47', ' Project experience '),
('17:37','24:40', 'HTTPS'),
('24:40','28:10', ' Implement read-write locks '),
]
if not os.path.isfile(file_name): # Calibration
print(" Illegal input ", file_name)
for startStr, endStr, suffix in output_arr:
start = cvsecs(startStr)
end = cvsecs(endStr)
if start < 0 or start >= end: # Calibration
print(" Illegal time ",startStr, endStr)
continue
full_output_name = add_suffix(file_name, suffix)
print(' Processing files: ', file_name, ' Time: ', startStr, '-', endStr)
fftool.ffmpeg_extract_subclip(file_name,start,end,full_output_name) # Clip and output
print(' The processing work is successful, and the output is: ',full_output_name)
Reference
Documentation for moviepy
moviepy Chinese document English document GitHub address
Blog post: Cut out 1 paragraph of video with moviepy
stack overflow Concat videos too slow using Python MoviePY
The above is the python based on moviepy audio and video clipping details, more about python moviepy audio and video clipping information please pay attention to other related articles on this site!