Many of us use recorder software to capture screen activities as video files. Different recorders may support different video formats such as AVI, MP4, GIF, etc. The output videos are often not exactly what we want. Probably we need another editor to crop and trim the videos or convert them to the formats we need.

To streamline this process, this article presents a simple Python solution for a highly-customisable DIY tool with all these functions, i.e., screen recording, simple video editing and format converting.
The author walks you through how to create such a tool in the following sections. All the codes in this article can be found in my git repository.
Screen Recording
The python libraries, i.e., Pynput, PyAutoGUI, and OpenCV are used to record screen activities.
Select region to record
The Pynput used here is to help the user to select a region on the screen by mouse click (left button). It records the mouse location on mouse click events.
A selected rectangle region may be defined using the upper-left point (x, y), the width w and the height h, as shown in the following figure.

Given the mouse locations on press and release events, the below function returns the parameters, i.e., x, y, w and h for the selected region.
If you need to capture the full screen, you can use the following PyAutoGui command to get the region.
wh = pyautogui.size()
region = [0, 0, wh.width, wh.height]
Record screen activities in the region
PyAutoGUI is a cross-platform mouse and keyboard control and a simple API. PyAutoGUI gets individual screenshots or frames, where the region is the rectangle selected above.
OpenCV is then used to save continuous screenshots as videos. It supports different video formats such as AVI and MP4. Other parameters, e.g., frame per second (fps) can also be set. In OpenCV, converting an image from BRG (BlueRed Green ) to RGB (Red Green Blue) colour model is also necessary.
The code for this screen recording function is as follows:
An example of a recorded video is shown below. This video contains all the screen activities in the region. It is necessary to remove the unwanted segments. The following section will present how to edit this video, i.e., video trimming and cropping.

Video Editing
MoviePy is a popular Python module for video editing. It supports basic operations (like trim, crop, concatenate, title insertions), video processing, or creating advanced effects. It can read and write the most common video formats, including GIF.
- Video trimming
We can use the following commands to trim the unwanted segments in the above video:
clips = [
[(0, 2), (0, 8)],
[(0, 24), (0, 32)]
]
video = VideoFileClip("test_recording.mp4")
clipped = [video.subclip(*clip) for clip in clips]
final_clip = concatenate_videoclips(clipped)
Where clips are the two video segments (0m2s-0m8s) and (0m24s, 0m32s) to keep in the video. After running these lines of code, other clips outside the two segments are removed.
- Video Cropping
Given a rectangle with the two diagonal corner locations, i.e., upper left and bottom right corners, cropping the video can be completed using the following MoviePy command.
x1, y1 = (10, 240)
x2, y2 = (610, 680)
cropped = vfx.crop(video, x1=x1, y1=y1, x2=x2, y2=y2)
- Format Converting
MoviePy can export a video to GIF picture or to a file format such as MP4, as follows:
video.write_gif("saved.gif", fps=10)
video.write_videofile("saved.mp4", fps=10)
Finally, after trimming and cropping, the video is shown as follows:

Create video from images
Using MoviePy, we can also create a video for a list of images.
Here we have 30 same-sized images with names _img0.png, _img1.png, …, _img29.png. To concatenate those images into the top GIF picture with the title Peace, we can simply run the following piece of code.
images = ["images/img_{}.png".format(i) for i in range(30)]
clips = [ImageClip(m).set_duration(1) for m in images]
concat_clip = concatenate_videoclips(clips, method="compose")
concat_clip.write_gif(out_filename, fps=1)
The duration of each image clip is set to 1 second, with the corresponding fps=1, in the above code.
Conclusions
This article presented a simple solution for a DIY video editing tool with additional screen recording. The tool is built on open-source Python libraries Pynput, PyAutoGui, OpenCV and MoviePy. You can use it at your convenience.
To further customize your tool, more MoviePy or OpenCV features such as object detection and tracking, etc. may be added.
Thank you for reading. Feel free to leave comments. If you like this article or this DIY tool, please share it with your friends, and follow me.
