background
In order to facilitate learning and review, it is necessary to cut some long audio and video clips into some small clips. This article shows you how to use Python to cut out certain snippets of audio and video.
Practical steps
- Finding the right Python library (is it cumbersome to install, easy to use, takes too long to execute)
moviepy
Audio and video library. API to use for analysis:Code sample
- Defining input and output
- Input: the address of an audio and video file, the time period to be cut out
- Output: Clip the file of the fragment
- Design the execution process and implement it step by step (define functions, depending on the use of specific apis)
- Read in and create a Clip object.
- Subclip, the input time parameter can be a string in time format.
- Export write_videofile.
- Conclusion: Time is too long, fragment how long it takes time; The CPU is full.
- stackoverflow Concat videos too slow using Python MoviePYThere is an answer that says it is faster to call the ffmpeg function wrapped in the package:
You have some functions that perform direct calls to ffmpeg: github.com/Zulko/movie… And are therefore extremely efficient, for simple tasks such as yours.
- stackoverflow Concat videos too slow using Python MoviePYThere is an answer that says it is faster to call the ffmpeg function wrapped in the package:
- Redesign and implementation, direct use
moviepy.video.io.ffmpeg_tools
Function in:Ffmpeg_extract_subclip (source audio and video file, start, stop, output name)
.- The start and end time arguments entered in this function must be numbers, not strings, as the basic library interface functions pass in strings. Look at the source found that there is a time string converted into a number of decorators, step by step to find that conversion function.
- Verdict: Much faster, almost in seconds.
- But I don’t understand why so much faster
- Optimization: Process multiple time periods at once
- The input is changed from a start and end time to a group of start and end times
- Cycle through the start and end times for each group
- The output file names are concatenated in sequence
- Optimization: Each time with a name
- In addition to the start and end times of each group, enter the suffix name
- File name + suffix to get the output file name
- Optimization: Verify input and output validity
- Verify that the input address is a valid file
- Validation period (not necessary)
- It can’t be less than 0
- It cannot be longer than the video time
- From the less than
The complete code
PIP install Moviepy is required
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('. ') # Last dot
res = file_name[:index] + '_' + suffix + file_name[index:]
return res
# enter
file_name = r"./XXX.mkv"
output_arr = [
('04:20'.'05:07'.'Introduce yourself'),
('05:07'.'saveth'.'Project Experience'),
('"'.'24:40'.'HTTPS'),
('24:40'.'so'.'Implement read/write lock'),]if not os.path.isfile(file_name): # check
print("Illegal input", file_name)
for startStr, endStr, suffix in output_arr:
start = cvsecs(startStr)
end = cvsecs(endStr)
if start < 0 or start >= end: # check
print("Illegal time.",startStr, endStr)
continue
full_output_name = add_suffix(file_name, suffix)
print('File processing:', file_name, 'Time:', startStr, The '-', endStr)
fftool.ffmpeg_extract_subclip(file_name,start,end,full_output_name) # Clip and output
print('Processing work successful, output:',full_output_name)
Copy the code
reference
- Moviepy document
- Moviepy
- English document
- Making the address
- Blog post: Use Moviepy to cut a part of the video
- stack overflow Concat videos too slow using Python MoviePY