def test_release_of_file_via_close(): # Create a random video file. red = ColorClip((256, 200), color=(255, 0, 0)) green = ColorClip((256, 200), color=(0, 255, 0)) blue = ColorClip((256, 200), color=(0, 0, 255)) red.fps = green.fps = blue.fps = 10 # Repeat this so we can see no conflicts. for i in range(3): # Get the name of a temporary file we can use. local_video_filename = os.path.join( TMP_DIR, "test_release_of_file_via_close_%s.mp4" % int(time.time())) clip = clips_array([[red, green, blue]]).with_duration(0.5) clip.write_videofile(local_video_filename) # Open it up with VideoFileClip. video = VideoFileClip(local_video_filename) video.close() clip.close() # Now remove the temporary file. # This would fail on Windows if the file is still locked. # This should succeed without exceptions. os.remove(local_video_filename) red.close() green.close() blue.close()
def test_release_of_file_via_close(): # Create a random video file. red = ColorClip((1024, 800), color=(255, 0, 0)) green = ColorClip((1024, 800), color=(0, 255, 0)) blue = ColorClip((1024, 800), color=(0, 0, 255)) red.fps = green.fps = blue.fps = 30 # Repeat this so we can see no conflicts. for i in range(5): # Get the name of a temporary file we can use. local_video_filename = join(TMP_DIR, "test_release_of_file_via_close_%s.mp4" % int(time.time())) with clips_array([[red, green, blue]]) as ca: video = ca.set_duration(1) video.write_videofile(local_video_filename) # Open it up with VideoFileClip. with VideoFileClip(local_video_filename) as clip: # Normally a client would do processing here. pass # Now remove the temporary file. # This would fail on Windows if the file is still locked. # This should succeed without exceptions. remove(local_video_filename) red.close() green.close() blue.close()
def test_clips_array_duration(): # NOTE: anyone knows what behaviour this sets ? If yes please replace # this comment. red = ColorClip((256, 200), color=(255, 0, 0)) green = ColorClip((256, 200), color=(0, 255, 0)) blue = ColorClip((256, 200), color=(0, 0, 255)) video = clips_array([[red, green, blue]]).with_duration(5) with pytest.raises(AttributeError): # fps not set video.write_videofile(os.path.join(TMP_DIR, "test_clips_array.mp4")) # this one should work correctly red.fps = green.fps = blue.fps = 30 video = clips_array([[red, green, blue]]).with_duration(5) video.write_videofile(os.path.join(TMP_DIR, "test_clips_array.mp4")) close_all_clips(locals())
def test_release_of_file_via_close(): # Create a random video file. red = ColorClip((1024, 800), color=(255, 0, 0)) green = ColorClip((1024, 800), color=(0, 255, 0)) blue = ColorClip((1024, 800), color=(0, 0, 255)) red.fps = green.fps = blue.fps = 30 # Repeat this so we can see no conflicts. for i in range(5): # Get the name of a temporary file we can use. local_video_filename = join( TMP_DIR, "test_release_of_file_via_close_%s.mp4" % int(time.time())) with clips_array([[red, green, blue]]) as ca: video = ca.set_duration(1) video.write_videofile(local_video_filename) # Open it up with VideoFileClip. with VideoFileClip(local_video_filename) as clip: # Normally a client would do processing here. pass # Now remove the temporary file. # This would fail on Windows if the file is still locked. # This should succeed without exceptions. remove(local_video_filename) red.close() green.close() blue.close()
def test_failure_to_release_file(): """ This isn't really a test, because it is expected to fail. It demonstrates that there *is* a problem with not releasing resources when running on Windows. The real issue was that, as of movepy 0.2.3.2, there was no way around it. See test_resourcerelease.py to see how the close() methods provide a solution. """ # Get the name of a temporary file we can use. local_video_filename = join( TMP_DIR, "test_release_of_file_%s.mp4" % int(time.time()) ) # Repeat this so we can see that the problems escalate: for i in range(5): # Create a random video file. red = ColorClip((256, 200), color=(255, 0, 0)) green = ColorClip((256, 200), color=(0, 255, 0)) blue = ColorClip((256, 200), color=(0, 0, 255)) red.fps = green.fps = blue.fps = 30 video = clips_array([[red, green, blue]]).set_duration(1) try: video.write_videofile(local_video_filename) # Open it up with VideoFileClip. clip = VideoFileClip(local_video_filename) # Normally a client would do processing here. # All finished, so delete the clipS. clip.close() video.close() del clip del video except IOError: print( "On Windows, this succeeds the first few times around the loop" " but eventually fails." ) print("Need to shut down the process now. No more tests in" "this file.") return try: # Now remove the temporary file. # This will fail on Windows if the file is still locked. # In particular, this raises an exception with PermissionError. # In there was no way to avoid it. remove(local_video_filename) print("You are not running Windows, because that worked.") except OSError: # More specifically, PermissionError in Python 3. print("Yes, on Windows this fails.")
def test_failure_to_release_file(): """ This isn't really a test, because it is expected to fail. It demonstrates that there *is* a problem with not releasing resources when running on Windows. The real issue was that, as of movepy 0.2.3.2, there was no way around it. See test_resourcerelease.py to see how the close() methods provide a solution. """ # Get the name of a temporary file we can use. local_video_filename = join( TMP_DIR, "test_release_of_file_%s.mp4" % int(time.time())) # Repeat this so we can see that the problems escalate: for i in range(5): # Create a random video file. red = ColorClip((256, 200), color=(255, 0, 0)) green = ColorClip((256, 200), color=(0, 255, 0)) blue = ColorClip((256, 200), color=(0, 0, 255)) red.fps = green.fps = blue.fps = 30 video = clips_array([[red, green, blue]]).set_duration(1) try: video.write_videofile(local_video_filename) # Open it up with VideoFileClip. clip = VideoFileClip(local_video_filename) # Normally a client would do processing here. # All finished, so delete the clipS. clip.close() video.close() del clip del video except IOError: print( "On Windows, this succeeds the first few times around the loop" " but eventually fails.") print("Need to shut down the process now. No more tests in" "this file.") return try: # Now remove the temporary file. # This will fail on Windows if the file is still locked. # In particular, this raises an exception with PermissionError. # In there was no way to avoid it. remove(local_video_filename) print("You are not running Windows, because that worked.") except OSError: # More specifically, PermissionError in Python 3. print("Yes, on Windows this fails.")
def test_clips_array(util): red = ColorClip((1024, 800), color=(255, 0, 0)) green = ColorClip((1024, 800), color=(0, 255, 0)) blue = ColorClip((1024, 800), color=(0, 0, 255)) video = clips_array([[red, green, blue]]) with pytest.raises(ValueError): # duration not set video.fx(resize, width=480).write_videofile( os.path.join(util.TMP_DIR, "test_clips_array.mp4"))
def render(self): if self.display == "edges": self.clip = self._render_clip(self._frames(self.image.edges)) elif self.display == "original": self.clip = self._render_clip(self._frames(self.image)) elif self.display == "both": edge_clip = self._render_clip(self._frames(self.image.edges)) original_clip = self._render_clip(self._frames(self.image)) self.clip = clips_array([[edge_clip, original_clip]])
def __init__(self, clips: [VideoClip], choose: lambda: int, display_size: (int, int) = DISPLAY_SIZE, fps: int = 15): self.window = tkinter.Tk() self.window.title("Preview: Choose which version to use") self.display_size = display_size self._choose = choose self.fps = fps self.update_id = None self.is_full_screen = False self.num_versions = len(clips) # Create a canvas that can fit the above video source size self.canvas = ResizingCanvas(self.window, width=display_size[0], height=display_size[1], highlightthickness=0) self.canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES) # Add handlers to canvas for input for key in [str(k) for k in range(1, len(clips) + 1)]: self.window.bind(key, self.on_number_key) self.window.bind("<Button-1>", self.on_click) self.window.bind("<Escape>", self.exit) self.window.bind('f', self.toggle_full_screen) self.window.bind('r', self.refresh_sources) self.window.bind("<Left>", self.scrub_backward) self.window.bind("<Right>", self.scrub_forward) self.window.bind("<Key-space>", lambda e: self.pause() if self.is_playing else self.play()) # Prepare the clips by combining them into a composite clip self.square_size = math.ceil(math.sqrt(len(clips))) segments = [ [ resize(clips[i + j], 1 / self.square_size) for j in range(self.square_size) if i + j < len(clips) ] for i in range(0, len(clips), self.square_size) ] self.native_video = resize(clips_array(segments), self.display_size) # TODO: fix fullscreen # self.fullscreen_video = resize( # clips_array(segments), height=self.window.winfo_screenheight()) self.video = self.native_video self.dt = 1.0 / fps self.t = 0 self.is_playing = False self.display()
def test_setup(): """Test VideoFileClip setup.""" red = ColorClip((1024,800), color=(255,0,0)) green = ColorClip((1024,800), color=(0,255,0)) blue = ColorClip((1024,800), color=(0,0,255)) red.fps = green.fps = blue.fps = 30 video = clips_array([[red, green, blue]]).set_duration(5) video.write_videofile("/tmp/test.mp4") assert os.path.exists("/tmp/test.mp4") clip = VideoFileClip("/tmp/test.mp4") assert clip.duration == 5 assert clip.fps == 30 assert clip.size == [1024*3, 800]
def test_setup(): """Test VideoFileClip setup.""" red = ColorClip((1024, 800), color=(255, 0, 0)) green = ColorClip((1024, 800), color=(0, 255, 0)) blue = ColorClip((1024, 800), color=(0, 0, 255)) red.fps = green.fps = blue.fps = 30 video = clips_array([[red, green, blue]]).set_duration(5) video.write_videofile("/tmp/test.mp4") assert os.path.exists("/tmp/test.mp4") clip = VideoFileClip("/tmp/test.mp4") assert clip.duration == 5 assert clip.fps == 30 assert clip.size == [1024 * 3, 800]
def test_setup(): """Test VideoFileClip setup.""" red = ColorClip((256, 200), color=(255, 0, 0)) green = ColorClip((256, 200), color=(0, 255, 0)) blue = ColorClip((256, 200), color=(0, 0, 255)) red.fps = green.fps = blue.fps = 10 with clips_array([[red, green, blue]]).set_duration(5) as video: video.write_videofile(os.path.join(TMP_DIR, "test.mp4")) assert os.path.exists(os.path.join(TMP_DIR, "test.mp4")) clip = VideoFileClip(os.path.join(TMP_DIR, "test.mp4")) assert clip.duration == 5 assert clip.fps == 10 assert clip.size == [256 * 3, 200] close_all_clips(locals())
def test_setup(util): """Test VideoFileClip setup.""" filename = os.path.join(util.TMP_DIR, "test.mp4") red = ColorClip((256, 200), color=(255, 0, 0)) green = ColorClip((256, 200), color=(0, 255, 0)) blue = ColorClip((256, 200), color=(0, 0, 255)) red.fps = green.fps = blue.fps = 10 with clips_array([[red, green, blue]]).with_duration(5) as video: video.write_videofile(filename, logger=None) assert os.path.exists(filename) clip = VideoFileClip(filename) assert clip.duration == 5 assert clip.fps == 10 assert clip.size == [256 * 3, 200] assert clip.reader.bitrate == 2
def test_setup(): """Test VideoFileClip setup.""" red = ColorClip((1024,800), color=(255,0,0)) green = ColorClip((1024,800), color=(0,255,0)) blue = ColorClip((1024,800), color=(0,0,255)) red.fps = green.fps = blue.fps = 30 with clips_array([[red, green, blue]]).set_duration(5) as video: video.write_videofile(os.path.join(TMP_DIR, "test.mp4")) assert os.path.exists(os.path.join(TMP_DIR, "test.mp4")) with VideoFileClip(os.path.join(TMP_DIR, "test.mp4")) as clip: assert clip.duration == 5 assert clip.fps == 30 assert clip.size == [1024*3, 800] red.close() green.close() blue.close()
def test_setup(): """Test VideoFileClip setup.""" red = ColorClip((1024, 800), color=(255, 0, 0)) green = ColorClip((1024, 800), color=(0, 255, 0)) blue = ColorClip((1024, 800), color=(0, 0, 255)) red.fps = green.fps = blue.fps = 30 with clips_array([[red, green, blue]]).set_duration(5) as video: video.write_videofile(os.path.join(TMP_DIR, "test.mp4")) assert os.path.exists(os.path.join(TMP_DIR, "test.mp4")) with VideoFileClip(os.path.join(TMP_DIR, "test.mp4")) as clip: assert clip.duration == 5 assert clip.fps == 30 assert clip.size == [1024 * 3, 800] red.close() green.close() blue.close()
def newvideo(request): template = 'newvideo.html' context = {} last = VideoPost.objects.last() image(last.description) # out = Image.open('/home/gamer/hhtest/media/out.png', 'r') # bg_w, bg_h = out.size # img = out.crop((0, 848, bg_w, bg_h)) # img.save('/home/gamer/hhtest/media/out.png') video = mp.VideoFileClip('/home/gamer/hhtest/media/my_stack.mp4') # photo = Image.open('/home/gamer/hhtest/media/out.png', 'r') # photo = photo.resize((video.size[0], video.size[0] // 3)) # photo.save('/home/gamer/hhtest/media/out.png') img = mp.ImageClip('/home/gamer/hhtest/media/insta.jpg').set_duration( video.duration) final_clip = clips_array([[video], [img]]) final_clip.write_videofile('/home/gamer/hhtest/media/newvideo.mp4') return render(request, template, context)
def main(): parser = ArgumentParser() parser.add_argument('name') parser.add_argument('--threads', type=int, default=cpu_count()) parser.add_argument('--build', action='store_true') parser.add_argument('--duration', type=float, default=0.06, help='fast=0.05, slow=0.3') parser.add_argument('--final-duration', type=float, default=3) # overrides add_date_arg(parser, '--start') add_date_arg(parser, '--end') parser.add_argument('--dpi', type=int) parser.add_argument('--output', choices=['mp4', 'gif'], default='mp4') args = parser.parse_args() composition = compositions[args.name] overrides = {} for arg in 'start', 'end', 'dpi': value = getattr(args, arg) if value: overrides[arg] = value composition.override(overrides) if args.build: for part in composition.parts: part.build() part_to_frames = composition.organise() frame_counts = set(len(frames) for frames in part_to_frames.values()) assert len(frame_counts) == 1, repr(frame_counts) length, = frame_counts if composition.durations: durations = composition.durations(length) else: durations = [args.duration] * length if args.final_duration: durations[-1] = args.final_duration def frames_and_durations_for(p): return {f: d for (f, d) in zip(part_to_frames[p], durations)} final = clips_array([[ clips_array([[p.clip(frames_and_durations_for(p)) for p in row]], bg_color=white) ] for row in composition.rows], bg_color=white) final = final.set_duration(sum(durations)) # 4k = 3840 # 1080p = 1920 path = str(output_path / f"{args.name}.{args.output}") if args.output == 'mp4': final.write_videofile(path, fps=24, threads=args.threads, bitrate='10M') else: final.write_gif(path, fps=24) print('shrinking...') optimize(path)