Annotation-based Automatic Clipping of Video into Multiple Videos

Wim Pouw ( wim.pouw@donders.ru.nl )
James Trujillo ( james.trujillo@donders.ru.nl )
18-11-2021

Info documents

In this module we have a very simple script that allows you to clip a large video into a bunch of smaller videos based on annotations. For example, you might have some gestures or trials annotated in a video, and since you only care about those moments you may want to automatically render smaller videos that only contains those events, which can then be further submitted for motion tracking or analysis (e.g., gesture networks).

In [81]:
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"
In [82]:
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)
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful
Moviepy - Running:
>>> "+ " ".join(cmd)
Moviepy - Command successful