pythonvideovideo-processing

Python video editing -- How to trim videos


Is there an easy way to trim a video file in python. If I know the start time and end time, I want to save the new file as multiple shorter files based on input.

example pseudo code:

function cut_video(original_vid, start, stop):
    original_vid.start_time(start)
    original_vid.end_time(stop)
    original_vid.trim() #based on times
    new_vid = original_vid
    return new_vid

Solution

  • Found a solution:

    def get_ffmpeg_bin():
        ffmpeg_dir = helper_functions.get_ffmpeg_dir_path()
        FFMPEG_BIN = os.path.join(ffmpeg_dir, "ffmpeg.exe")
        return FFMPEG_BIN
    
    
    def split_vid_from_path(video_file_path, start_time, durration):
        ffmpeg_binary =  get_ffmpeg_bin()
        output_file_name = get_next_file_name(video_file_path)
        pipe = sp.Popen([ffmpeg_binary,"-v", "quiet", "-y", "-i", video_file_path, "-vcodec", "copy", "-acodec", "copy",
                     "-ss", start_time, "-t", durration, "-sn", output_file_name ])
    
    
        pipe.wait()
        return True
    
    
    sample_vid = os.path.join(get_sample_vid_dir_path(), "Superman-01-The_Mad_Scientist.mp4")
    split_vid_from_path(sample_vid, "00:00:00", "00:00:17")
    

    https://www.ffmpeg.org/ffmpeg.html -> this documentation allows you to add your own custom flags to your ffmpeg wrapper.

    Something to note, you may want to verify the user is giving you valid data.