Skip to content

Instantly share code, notes, and snippets.

@alizahidraja
Created December 7, 2022 21:26
Show Gist options
  • Save alizahidraja/1fda53ecb6238d477d742147aa2bbe62 to your computer and use it in GitHub Desktop.
Save alizahidraja/1fda53ecb6238d477d742147aa2bbe62 to your computer and use it in GitHub Desktop.
Split a YouTube Video using Chapters in Python
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
def convert_time_to_seconds(time):
# ChatGPT wrote this function
try:
hour, minute, second = map(int, time.split(":"))
return hour * 3600 + minute * 60 + second
except:
minute, second = map(int, time.split(":"))
return minute * 60 + second
chapters = """00:00 - Chapter 1
00:35 - Chapter 2
01:59 - Chapter 3
02:41 - Chapter 4
04:47 - Chapter 5
05:22 - Chapter 6
07:09 - END""".split("\n")
path = "VIDEO.mp4"
for i in range(len(chapters) - 1):
starttime = convert_time_to_seconds(chapters[i].split(" - ")[0])
endtime = convert_time_to_seconds(chapters[i + 1].split(" - ")[0])
ffmpeg_extract_subclip(path, starttime, endtime, targetname=str(chapters[i].split(" - ")[1])+".mp4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment