1. Tutorials

Easily Clip/Split Large Videos With this Python Script

Sometimes you have may have a large video file, and you want to split that video into many smaller videos with start and end times that you specify yourself. And of course, you don’t want to do it manually with a video editor because it’s gonna take forever.

What we are talking about for example, is when you have a video of 10 minutes, and you want to create 3 smaller clips out of it such that the first one is between 1:20 and 2:20 for example, and the second one is between 3:00 and 4:00 and the last one is between 7:10 and 8:15. Such things are theoretically hard, but not with Python and its amazing tools!

Luckily, we are here to help you do the task in under a minute.

Requirements

First, make sure you have Python 3 installed on your Windows or Linux distribution, and also make sure that pip is installed. Then, run the following pip command to install the needed Python module to do the work, which is moviepy:

pip3 install moviepy --user

Then, create 2 files in your current working directory:

  • split.py: Which will contain the Python code later.
  • times.txt: Which should contain the start and end times you want for the clips, we’ll fill it later.

Let’s say that your original video that you are trying to clip is 20 minutes long, and you want to create 3 smaller videos, the first one is from the start and till 1 minute (from 0 to 60 seconds), the second one is between 1:00 and 1:20, and the 3rd one is between 1:40 and 2:00). Then, your times.txt file contents should be the following:

0-60
60-80
100-120

Notice how we needed to convert the format from 1:00 to 60 (always in seconds).

You can add whatever number of lines you like into that file. E.g if you want to create 100 smaller clip, then you can do it. Just make sure that you follow the exact same format: start_time-end_time (With no spaces in between, and each one in its own separate line).

The Python Script

Now, coming to the important part, here’s the code that you need to put in your split.py Python file:

#!/usr/bin/env python

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

# Replace the filename below.
required_video_file = "filename.mp4"

with open("times.txt") as f:
  times = f.readlines()

times = [x.strip() for x in times] 

for time in times:
  starttime = int(time.split("-")[0])
  endtime = int(time.split("-")[1])
  ffmpeg_extract_subclip(required_video_file, starttime, endtime, targetname=str(times.index(time)+1)+".mp4")

Replace the name of the file filename.mp4 with the name of the video file you have (It must be in the same current working directory). If you were trying to clip other formats such as .webm and .avi, then simply replace all the occurrences of .mp4 in the Python script with the format you desire. (There are just two of them; In the name of the file you entered and in the last line of the code).

Now, run the Python code with:

python3 split.py
python split.py # If the previous one didn't work.

And see the 3 video files are created and working!

Conclusion

Python is such an amazing and effective language to do any type of scripting you want. I saved myself – and possibly you too – tons of hours that I could’ve spent on doing all of this manually. This task can be helpful in managing your social media campaigns for example, or if you want to keep smaller pieces of larger videos you may like for whatever purpose.

If you have any issues with the code or any problem, then leave us a comment and we’ll be happy to help.

.
guest
8 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Ben Leverett

Something’s not right; I made sure had all the prerequisites, all the files were in the same folder, like you said to do, and it’s still not working; please HELP!!!!!!!!!!!!!!!!!!!!!!

Ben Leverett

Here’s the (and a bit more, for context) output of my terminal. root@Ubuntu-ThinkPad-T430:/media/EXTHDD/Bens old external HDD/Documents and Settings/Ben/My Videos/Movies# python3 split.py File “split.py”, line 3 3.from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip ^ SyntaxError: invalid syntax root@Ubuntu-ThinkPad-T430:/media/EXTHDD/Bens old external HDD/Documents and Settings/Ben/My Videos/Movies# ls 9.mp4 “Kiki’s Delivery Service.mp4” ‘Spy Kids 2.mp4’ Anastasia.mp4 ‘KungFu Panda 2.mp4’ ‘Spy Kids.mp4’ ‘Big Hero 6.mp4’ ‘Legend of the Guardians.mp4’ ‘Stuart Little’ ‘Carmen Sandiego: To Steal or Not to Steal.mp4’ ‘Miles From Tomorrow Land’ Tangled.mp4 ‘Cloudy With a Chance of Meatballs.mp4’ MSB ‘The Breadwinner.mp4’ Coraline.mp4 ‘My Life as a Zucchini.mp4’ ‘The Iron Giant.mp4’ EasePaint_Output NCIS ‘The Peanuts Movie.mp4’ ‘Frozen… Read more »

Ben Leverett

What is “pip” supposed to be, in Ubuntu 20.04? I’m not finding it.

D. Brouwer

Thank you so much. It works amazingly well.

sushmitha

  starttime = int (time.split(“-“)[0])
ValueError: invalid literal for int() with base 10: ”

Vaishnavi

Thank u so much for your guidance,
god bless you

Subscribe for $5

Instead of using your adblocker, join us now on Patreon to unlock a complete ad-free experience + access to private FOSS Post forum where many internals are discussed.

Ad

Email Newsletter

.