location code: https://github.com/WimPouw/EnvisionBootcamp2021/tree/main/Python/AnnotationBasedVideoClippings
citation: Pouw, W., & Trujillo, J.P. (2021-11-18). Annotation-based Automatic Clipping of Video into Multiple Videos [day you visited the site]. Retrieved from: https://wimpouw.github.io/EnvisionBootcamp2021/AnnotationBasedVideoClippings.html
import os #basic foldering functions
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip #this is the video clipping function
import pandas #used here for data reading (csv)
import numpy as np #for saving data in array format
#foldering
curfol = os.path.dirname(os.getcwd()) #current folder
annotationfol = curfol + "\\ANNOTATIONS\\" #folder of the annotations
videofol = curfol + "\\VIDEOS\\"#video folder
outputfol = curfol + "\\OUTPUT_CLIPPED_VIDEOS\\" #where do you want to save your clipped videos?
#list annotationfiles
annotations = os.listdir(annotationfol) #list all the annotationfile locations in the annotationfolder (begintime, endtime, annotation)
#what video file .mov .mp4 .avi
vidformat = ".mp4"
for annot in annotations: #loop through your annoation files
ID = annoindex[0:3] #the first code will be ID that you can match with your video
video = videofol+ID+vidformat #get the video that has a matching code
data = np.array(pandas.read_csv(annotationfol+annot)) #save into an numpy array (but first read.csv using pandas)
for ch in range(0, data.shape[0]): #now loop through each row of your annotions
begintime = data[ch,0] #extract begintime
endtime = data[ch,1] #extract end time
annotlabel = data[ch,2] #extract your annotation
#now take the video and select the relevant chunk and write to the outputfolder
ffmpeg_extract_subclip(video, begintime/1000, endtime/1000, targetname=outputfol+ID+ "_" +str(ch)+"_" +annotlabel+vidformat)