Automate video trimming with FFMPEG and Python

Ffmpeg is a free software to manipulate videos. You can also record the screen with this software and the video output occupies not very much space and it’s fast. So, if you like me need to record fast your ideas and you do not need fancy effects and you do not like heavy files, this tool is for you. I never found a software fo screen video recording that loads in a second and that saves the files in a millisecond, so I found my choice. You will find all the links I made for the usage of ffmpeg for the basic stuff I need at the end of this post. Sometimes you may have the need to trim some parts of your video. The fastest way (but really fast) is this:

 ffmpeg -ss 00:00:00 -i 004.mp4 -to 00:02:00 -c copy num002.mp4

Copy this command into an editor and save it as a .bat file. You will see that this works like a charm. I had a little different commands in the video below where I use python to specify the start and end of the trimming.

Python comes in help

Let’s wee what happens if we want to make things easier trimming videos in this live coding below.

Using moviepy

This code works selecting the file in a folder and then giving the start like 0 seconds and the end like 10 seconds.

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
import glob


# Get the second you do not want in the movie
files = glob.glob("*.mp4")
print([(n, f) for n, f in enumerate(files)])
filenumber = int(input("File number: "))
start = int(input("Start: "))
end = int(input("End: "))
ffmpeg_extract_subclip(
    files[filenumber], start, end, targetname="output_selected.mp4")

Ffmpeg & Python for videos

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.