def dumpVideoAtRanges(video_fname, sec_ranges, odir): if not os.path.exists(odir): os.makedirs(odir) set_no = 0 for (low, high) in sec_ranges: clip = VideoFileClip(video_fname) clip = clip.cutout(high, clip.duration) clip = clip.cutout(0, low) fname = os.path.join(odir, "frame_s_{}_%03d.jpg".format(set_no)) clip.write_images_sequence(fname) set_no += 1
def loadclip(fname=None, ratenterframe=2000): """ Load a video file form HFSP Dataest. Input: fname : Path to video file. Leave as None to load default file. ratenterframe : Clip the video file to remove frames before this frame #. Return: VideoFileClip object of moviepy package. """ if fname is None: clip = VideoFileClip( '/mnt/HFSP_Data/iterations_HFSP_Data_SfN_2019/R19001_0115_session_fullvid_0.avi' ) ratenterframe = 300 clip = clip.cutout(0, ratenterframe / clip.fps) return clip else: clip = VideoFileClip( '/mnt/HFSP_Data/iterations_HFSP_Data_SfN_2019/R19001_0115_session_fullvid_0.avi' ) clip.cutout(0, ratenterframe / clip.fps) return clip
class SilenceTrimmer: def __init__( self, path: str, thresh_min=0.02, thresh_max=0.1, thresh_duration=0.05, thread_nb=3, out_name="out", ): """ Enter a threshold value and duration (in seconds 1.3) """ self.path = path self.cpv = VideoFileClip(path) self.tmin = thresh_min self.tmax = thresh_max self.tdur = thresh_duration self.thread_nb = thread_nb self.oname = out_name self.spath = "" def cut_silence(self, start: float, end: float) -> bool: "go to next chunk" if end >= self.cpv.duration: return False else: sclip = self.cpv.subclip(start, end) vol = sclip.audio.max_volume() if vol <= self.tmax: # pdb.set_trace() self.cpv = self.cpv.cutout(start, end) return True return False def trim_clip(self): "trim clip using given threshold values" counter = 0 check = True while check: cend = self.cpv.end if counter >= cend: break send = counter + self.tdur if send >= cend: check = False send = cend isCut = self.cut_silence(counter, send) else: isCut = self.cut_silence(counter, send) if not isCut: # if the cut did not occur I should move to next # chunk if it did I should stay in the same place # pdb.set_trace() counter = send # pdb.set_trace() def overwrite_clip(self): "overwrite the given clip path" self.cpv.write_videofile(self.path, threads=self.thread_nb) def save_clip(self) -> None: "save clip to path" dname = os.path.dirname(self.path) ext = os.path.basename(self.path).split(".").pop() # pdb.set_trace() date = dt.today() fmt = date.strftime("%Y-%m-%d-%H-%M-%S-%f") sname = "out-" + fmt + ".mp4" self.spath = os.path.join(dname, sname) self.cpv.write_videofile(self.spath, threads=self.thread_nb, preset="fast") def trim(self): "trim and save the clip" self.trim_clip() self.save_clip()
# sample some 10 stills from the video: nframes = cap.get(cv2.CAP_PROP_FRAME_COUNT) interval = nframes/10 for i in range(0,10): cap.set(cv2.CAP_PROP_POS_FRAMES,i*interval+30) ret, image = cap.read() cv2.imwrite('frame_{:02d}.png'.format(i), image) filename = 'video_files/skytrain.mp4' clip = VideoFileClip(filename) clip1 = clip.cutout(0, 4) clip1 = clip1.cutout(12,clip1.duration) clip1.write_videofile("test.mp4") # CURVE: filename = 'skytrain.mp4' clip = VideoFileClip(filename) clip1 = clip.cutout(0, 135) clip1 = clip1.cutout(35,clip1.duration) clip1.write_videofile("test2.mp4") """ Notes [1280 720] vetical cutoff: