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

  1. Finding the right Python library (is it cumbersome to install, easy to use, takes too long to execute)
    • moviepyAudio and video library. API to use for analysis:Code sample
  2. Defining input and output
    1. Input: the address of an audio and video file, the time period to be cut out
    2. Output: Clip the file of the fragment
  3. Design the execution process and implement it step by step (define functions, depending on the use of specific apis)
    1. Read in and create a Clip object.
    2. Subclip, the input time parameter can be a string in time format.
    3. Export write_videofile.
  4. 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.

  5. Redesign and implementation, direct usemoviepy.video.io.ffmpeg_toolsFunction 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.
  6. Verdict: Much faster, almost in seconds.
    • But I don’t understand why so much faster
  7. Optimization: Process multiple time periods at once
    1. The input is changed from a start and end time to a group of start and end times
    2. Cycle through the start and end times for each group
    3. The output file names are concatenated in sequence
  8. Optimization: Each time with a name
    1. In addition to the start and end times of each group, enter the suffix name
    2. File name + suffix to get the output file name
  9. Optimization: Verify input and output validity
    1. Verify that the input address is a valid file
    2. Validation period (not necessary)
      1. It can’t be less than 0
      2. It cannot be longer than the video time
      3. 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