def show_video(self): directory = QDir(os.getcwd()) directory.setFilter(QDir.Files | QDir.NoDotDot | QDir.NoDotAndDotDot) directory.setSorting(QDir.Time) set_filter = ["*.avi"] directory.setNameFilters(set_filter) for show in directory.entryInfoList(): print("%s %s" % (show.size(), show.fileName())) out = show.fileName() break # print(1) if self.zone_cutted: # print(2) x1, y1, x2, y2 = get_zone_position() clip = VideoFileClip(out) cut_video = crop(clip, x1, y1, x2, y2) name_of_file = QFileInfo(out).baseName() + "_zone.avi" cut_video.write_videofile(name_of_file, codec='mpeg4', audio=False) else: # print(3) name_of_file = out # print(4) self.mediaPlayer.setMedia( QMediaContent(QUrl.fromLocalFile(os.getcwd() + "\\" + name_of_file)) ) # print(5) # self.mediaPlayer.setPlaybackRate() self.play()
def main(path: str) -> int: path = Path(path) if not path.exists(): print("Not a valid path") return (2) with VideoFileClip(str(path)) as clip: first_frame = clip.get_frame(clip.duration * .1) middle_frame = clip.get_frame(clip.duration * .5) # Not picking the exact first/last frame to avoid weird color values at the boundaries last_frame = clip.get_frame(clip.duration * .9) height = len(first_frame) width = len(first_frame[0]) LOGGER.info(f"Original resolution: {width}x{height}") # Magic of cache I call on you now, check these three frames one at a time first_left = get_left_edge(first_frame) first_right = get_right_edge(first_frame) first_top = get_top_edge(first_frame) first_bottom = get_bottom_edge(first_frame) middle_left = get_left_edge(middle_frame) middle_right = get_right_edge(middle_frame) middle_top = get_top_edge(middle_frame) middle_bottom = get_bottom_edge(middle_frame) last_left = get_left_edge(last_frame) last_right = get_right_edge(last_frame) last_top = get_top_edge(last_frame) last_bottom = get_bottom_edge(last_frame) # If the beginning of the video is a loading screen/fading into a scene, it may have many black pixels. Sample # three frames to try to figure out what size the video is supposed to be. Taking the median because video # encoding isn't perfect, and black pixels might not be entirely black left_edge = median([first_left, middle_left, last_left]) right_edge = median([first_right, middle_right, last_right]) top_edge = median([first_top, middle_top, last_top]) bottom_edge = median([first_bottom, middle_bottom, last_bottom]) LOGGER.info( f"Cropped resolution: {right_edge - left_edge + 1}x{top_edge - bottom_edge + 1}" ) # print(left_edge) # print(right_edge) # print(top_edge) # print(bottom_edge) cropped_clip = crop(clip, x1=left_edge, y1=bottom_edge, x2=right_edge, y2=top_edge) cropped_clip.write_videofile( str(path.with_suffix("")) + "_cropped" + str(path.suffix)) return (0)
def cropAndSaveFiles(file, newFileDir, totalSizeMP4, totalSizeWebM): cropFile = crop(file, x1=120, x2=720, y2=660) if (doMP4 == True): cropFile.write_videofile( newFileDir + ".mp4", fps=18, codec='libx264') totalSizeMP4 += getsize(newFileDir + ".mp4") if (doWebM == True): cropFile.write_videofile( newFileDir + ".webm", codec='libvpx') totalSizeWebM += getsize(newFileDir + ".webm") return totalSizeMP4, totalSizeWebM
def test_crop(): # x: 0 -> 4, y: 0 -> 3 inclusive clip = BitmapClip([["ABCDE", "EDCBA", "CDEAB", "BAEDC"]], fps=1) clip1 = crop(clip) target1 = BitmapClip([["ABCDE", "EDCBA", "CDEAB", "BAEDC"]], fps=1) assert clip1 == target1 clip2 = crop(clip, x1=1, y1=1, x2=3, y2=3) target2 = BitmapClip([["DC", "DE"]], fps=1) assert clip2 == target2 clip3 = crop(clip, y1=2) target3 = BitmapClip([["CDEAB", "BAEDC"]], fps=1) assert clip3 == target3 clip4 = crop(clip, x1=2, width=2) target4 = BitmapClip([["CD", "CB", "EA", "ED"]], fps=1) assert clip4 == target4 # TODO x_center=1 does not perform correctly clip5 = crop(clip, x_center=2, y_center=2, width=3, height=3) target5 = BitmapClip([["ABC", "EDC", "CDE"]], fps=1) assert clip5 == target5 clip6 = crop(clip, x_center=2, width=2, y1=1, y2=2) target6 = BitmapClip([["DC"]], fps=1) assert clip6 == target6
def prepare_short_video(src_video_path=r'../demo/mrbean.mp4'): """ Make a short demo video. https://drive.google.com/file/d/1MLuux3dJVmiPTYge1J6xy5Fnq3Vd_yq6/view?usp=sharing :param src_video_path: path to video :return: None """ video = mpy.VideoFileClip(src_video_path) video = video.subclip((4, 30), (4, 35)) (w, h) = video.size # crop out black boarders video = crop(video, width=600, height=450, x_center=w / 2, y_center=h / 2) video.write_videofile(join(dirname(src_video_path), 'short_video.mp4')) return
def test_crop(): with VideoFileClip("media/big_buck_bunny_432_433.webm") as clip: clip1 = crop(clip) #ie, no cropping (just tests all default values) clip1.write_videofile(os.path.join(TMP_DIR, "crop1.webm")) clip2 = crop(clip, x1=50, y1=60, x2=460, y2=275) clip2.write_videofile(os.path.join(TMP_DIR, "crop2.webm")) clip3 = crop(clip, y1=30) #remove part above y=30 clip3.write_videofile(os.path.join(TMP_DIR, "crop3.webm")) clip4 = crop(clip, x1=10, width=200) # crop a rect that has width=200 clip4.write_videofile(os.path.join(TMP_DIR, "crop4.webm")) clip5 = crop(clip, x_center=300, y_center=400, width=50, height=150) clip5.write_videofile(os.path.join(TMP_DIR, "crop5.webm")) clip6 = crop(clip, x_center=300, width=400, y1=100, y2=600) clip6.write_videofile(os.path.join(TMP_DIR, "crop6.webm"))
def test_crop(): with VideoFileClip("media/big_buck_bunny_432_433.webm") as clip: clip1=crop(clip) #ie, no cropping (just tests all default values) clip1.write_videofile(os.path.join(TMP_DIR, "crop1.webm")) clip2=crop(clip, x1=50, y1=60, x2=460, y2=275) clip2.write_videofile(os.path.join(TMP_DIR, "crop2.webm")) clip3=crop(clip, y1=30) #remove part above y=30 clip3.write_videofile(os.path.join(TMP_DIR, "crop3.webm")) clip4=crop(clip, x1=10, width=200) # crop a rect that has width=200 clip4.write_videofile(os.path.join(TMP_DIR, "crop4.webm")) clip5=crop(clip, x_center=300, y_center=400, width=50, height=150) clip5.write_videofile(os.path.join(TMP_DIR, "crop5.webm")) clip6=crop(clip, x_center=300, width=400, y1=100, y2=600) clip6.write_videofile(os.path.join(TMP_DIR, "crop6.webm"))
def test_crop(): clip = get_test_video() clip1 = crop(clip) # ie, no cropping (just tests all default values) clip1.write_videofile(os.path.join(TMP_DIR, "crop1.webm")) clip2 = crop(clip, x1=50, y1=60, x2=460, y2=275) clip2.write_videofile(os.path.join(TMP_DIR, "crop2.webm")) clip3 = crop(clip, y1=30) # remove part above y=30 clip3.write_videofile(os.path.join(TMP_DIR, "crop3.webm")) clip4 = crop(clip, x1=10, width=200) # crop a rect that has width=200 clip4.write_videofile(os.path.join(TMP_DIR, "crop4.webm")) clip5 = crop(clip, x_center=300, y_center=400, width=50, height=150) clip5.write_videofile(os.path.join(TMP_DIR, "crop5.webm")) clip6 = crop(clip, x_center=300, width=400, y1=100, y2=600) clip6.write_videofile(os.path.join(TMP_DIR, "crop6.webm")) close_all_clips(locals())
""" import os from moviepy.editor import VideoFileClip, clips_array, vfx, TextClip, CompositeVideoClip from moviepy.video.fx.crop import crop file1 = "input/video/test270/setangi_beach_270p.mp4" file2 = "output/video/setangi_beach_270p_finetune4.avi" file1_txt = "setangi_beach_270p" file2_txt = "finetuned" width = 480 * 4 height = 270 * 4 clip1 = VideoFileClip(file1, target_resolution=(height, width)).margin(10) clip2 = VideoFileClip(file2, target_resolution=(height, width)).margin(10) clip1 = crop(clip1, x_center=width / 2, y_center=height / 2, width=width / 4, height=height / 4).margin(10) clip2 = crop(clip2, x_center=width / 2, y_center=height / 2, width=width / 4, height=height / 4).margin(10) txt_clip1 = TextClip(file1_txt, fontsize=36, color="white").set_position("center").set_duration(5) txt_clip2 = TextClip(file2_txt, fontsize=36, color="white").set_position("center").set_duration(5) final_clip = clips_array([[ CompositeVideoClip([clip1, txt_clip1]), CompositeVideoClip([clip2, txt_clip2]) ]])
) med_res_clip = VideoFileClip( poop, audio=False, target_resolution=MID_RES_TARGET_RESOLUTION, resize_algorithm='fast_bilinear', ) high_res_clip = VideoFileClip( poop, audio=False, target_resolution=HI_RES_TARGET_RESOLUTION, resize_algorithm='fast_bilinear', ) P1_health_clip = crop(high_res_clip, x1=P1_HEALTH_BAR_TL[0], y1=P1_HEALTH_BAR_TL[1], x2=P1_HEALTH_BAR_TL[0] + HEALTH_BAR_DIM[0], y2=P1_HEALTH_BAR_TL[1] + HEALTH_BAR_DIM[1]) P2_health_clip = crop(high_res_clip, x1=P2_HEALTH_BAR_TL[0], y1=P2_HEALTH_BAR_TL[1], x2=P2_HEALTH_BAR_TL[0] + HEALTH_BAR_DIM[0], y2=P2_HEALTH_BAR_TL[1] + HEALTH_BAR_DIM[1]) # Find match start timestamps ########################################################################### sec_clip_frames_buffer = collections.deque() vs_sec_clip_frames = [] sec_matches = [] match_titles = [] next_sec = 0