9

I'm working on creating GIF from video clips automatically on the server using MoviePy. The library helped in various transformations and trimming of video to create GIFs.

In my current project, I have a video clip with lots of moving objects making it difficult to do automatic tracking of the region of interest. (A quick gif below shows the scene, though the background there can be easy to eliminate and do the tracking of object. But let's say tracking object is out of scope of the project).

As shown in the gif below the red rectangle is the region of interest which moves from left to right with time. I want to crop that region and create a GIF.

enter image description here

I'm using MoviePy where I cropped a rectangle from a video to create a gif. But the rectangle is fixed at its original coordinates position. I'm looking for a way to move that rectangle with time and crop it to create a GIF.

clip = (VideoFileClip("my_video.mp4")
         .subclip((1,10.1),(1,14.9))
         .resize(0.5)
         .crop(x1=145,y1=110,x2=400,y2=810)) 

clip.write_gif("my_gif.gif")

How to add time factor so that this coordinates change with the time.

Any suggestions welcome.

Anna23
  • 680
  • 2
  • 8
  • 16

1 Answers1

5

You're looking for the scroll function in moviepy. The docs for it are lacking, but they are here, and the source code here.

moviepy.video.fx.all.scroll(clip, h=None, w=None, x_speed=0, y_speed=0, x_start=0, y_start=0, apply_to='mask')

Parameters:

  • clip; the clip to be acted upon

  • h and w which determine the height and width of the final clip

  • x_speed and y_speed which determine the speed of scrolling. Not sure what these are measured in, so you may have to investigate the source code, or just trial and error it.

  • x_start and y_start which is the distances from (0,0) that it starts scrolling at.

  • apply_to; something to do with masks, you won't need it!

Final code:

clip = (VideoFileClip("my_video.mp4")
     .subclip((1,10.1),(1,14.9))
     .resize(0.5)
     .crop(x1=145,y1=110,x2=400,y2=810))

# You won't need to set `w` and `h` if you are separately cropping it
new_clip = clip.scroll(w=clip.w, h=var, x_speed=speed, y_start=height_to_top_of_wanted_bit)
new_clip.set_duration(1.0 / speed)

new_clip.write_gif("my_gif.gif")

Note, this code is not tested.

Tom Burrows
  • 2,225
  • 2
  • 29
  • 46