Trim mp4 video with python-ffmpeg

Thing you want to do

I want to cut out a specific area of an mp4 format video.

means

I used python's ffmpeg library.

code

If you specify the input file path and trimming area, a trimmed file will be generated in the same hierarchy as the input file.

trimming.py


import ffmpeg

def video_trimming(input_file_path, start_x, start_y, w, h):
    """
    input_file_path #Absolute path of file
    start_x = 850  #X coordinate (px) of the section you want to cut
    start_y = 500  #Y coordinate (px) of the section you want to cut
    width = 700  #Width of the section you want to cut (px)
    height = 580  #Height of the section you want to cut (px)
    """

    #Trimming file parameter specification
    stream = ffmpeg.input(input_file_path)
    stream = ffmpeg.crop(stream, start_x, start_y, w, h)

    #Output file name generation
    output_file_str = input_file_path.split(".")
    output_file_name = output_file_str[0] + "_trimed." + output_file_str[1]
    stream = ffmpeg.output(stream, output_file_name)

    #Run(Overwrite if there is a file)
    ffmpeg.run(stream, overwrite_output=True)

    return

Recommended Posts

Trim mp4 video with python-ffmpeg
YouTube video management with Python 3
Loop video loading with opencv
Receive video streaming with Tello
Get video file information with ffmpeg-python
MP3 to WAV conversion with Python
Detect video objects with Video Intelligence API
Easy animation with matplotlib (mp4, gif)
Trimming with OpenCV-Making a peeping video-