Search code examples
pythonvideo-processingmoviepy

Remove a specific portion from video - python


All the methods I've seen so far explains how to cut and save the part you just cut, or how to cut from the beginning or the end, I have a 60-minute video. I want to cut (REMOVE) just the minute 31 and save the whole video again without that part. I have a lot of videos so I really need the simplest and the easiest way possible to do this with python. I've tried splitting the video then trim from the end and then merging the two halves again. But that is just so bad.


Solution

  • this is the fastest way i found ... it's actully pretty fast and accurate

    i'm a beginner don't judge

    okay, ffmpeg is doing most of the job .. but yeah .. it's automated now with python lol

    import subprocess
    import os
    from moviepy.editor import *
    
    
    x = input("  Scene Start? ")
    y = input("  Scene End? ")
    
    
    cliplen = VideoFileClip("video.mp4").duration
    clip = str(cliplen / 60).split(".")[0]
    
    
    print(x)
    cut = x.split(".")[0]
    cutSeconds = x.split(".")[1]
    
    
    print(y)
    end = y.split(".")[0]
    endSeconds = y.split(".")[1]
    
    
    os.system("(echo file '1.mp4' & echo file '2.mp4' )>list.txt")
    
    
    first = "ffmpeg -i video.mp4 -vcodec copy -acodec copy -ss 00:00:00 -t 00:{}:{}.00 1.mp4".format(cut, cutSeconds)
    second = "ffmpeg -i video.mp4 -vcodec copy -acodec copy -ss 00:{}:{}.00 -t 00:{}:00 2.mp4".format(end, endSeconds, clip)
    
    end = "ffmpeg -safe 0 -f concat -i list.txt -c copy final.mp4"
    
    
    wr = "(echo {} & echo {} & echo {} & echo del 1.mp4 & echo del 2.mp4 )>list.cmd"
    os.system(wr.format(first, second, end))
    
    e = input("run now?[y/n] ")
    if e == "n":
        exit
    elif e == "y":
        os.system("list.cmd")
    else:
        exit