Automatic Video Editing Using Python

A simple and elegant way to tackle

Kunal Bharadkar
4 min readJun 18, 2021

Video editing is both a creative and a technical part of the post-production process of filmmaking. The term is derived from the traditional process of working with film which increasingly involves the use of digital technology.

The film editor works with the raw footage, selecting shots and combining them into sequences which create a finished motion picture. Video editing is described as an art or skill, the only art that is unique to cinema, separating videomaking from other art forms that preceded it, although there are close parallels to the editing process in other art forms such as poetry and novel writing. Video editing is often referred to as the “invisible art” because when it is well-practiced, the viewer can become so engaged that they are not aware of the editor’s work

Business Problem:

From the whole video, you want to edit only some parts of the video

Solution:

So you extract the part which you want to be edit and have to preprocess as you want and then concatenate with the original one

FFmpeg is an amazing cross-platform tool capable of recording, converting, editing, and streaming digital audio and video in several formats.

It can be used to do most multimedia tasks quickly and easily but it is a command-line tool.This makes their use sometimes difficult and verbose.

MoviePy is a Python module for video editing, which can be used for basic operations (like cuts, concatenations, title insertions), video compositing (non-linear editing), video processing, or to create advanced effects. It can read and write the most common video formats, including GIF.

Note: This module automatically installs FFmpeg. However, you might prompt to install in some cases.

Installation:

!pip install moviepy

Getting subpart of the video which you want to edit

# getting some part of video 5 secondsclip = clip.subclip(10,15)

Concatenate video clips

# loading video
clip1 = VideoFileClip("part1.mp4")
clip3 = VideoFileClip("part3.mp4")
clip4 = VideoFileClip("part4.mp4")
# concatenating both the clips
final = concatenate_videoclips([clip1,clip3,clip4])

Save the edited video in the format which you want to be saved

# saving the clip
final.write_videofile("final.mp4")

Adding different audio in video

# Import everything needed to edit video clipsfrom moviepy.editor import *# loading video intro video
clip = VideoFileClip("final_new.mp4")
# loading audio file
audioclip = AudioFileClip("new.mp3")
# adding audio to the video clip
videoclip = clip.set_audio(audioclip)
videoclip.write_videofile("final_new1.mp4")

PIL( Python Imaging Library) is a free library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. However, its development has stagnated, with its last release in 2009. Fortunately, there is Pillow, an actively developed fork of PIL which is easier to install; runs on all major operating systems, and supports Python 3. The library contains basic image processing functionality, including point operations, filtering with a set of built-in convolution kernels, and color space conversions.

Resources

The documentation has instructions for installation as well as examples covering every module of the library.

# Importing the PIL library
from PIL import Image,ImageDraw,ImageFont
img = Image.open("output_folder/1.jpg")
#Relative Path
#Image which we want to paste
img2 = Image.open("test_photo.jpg")
newsize = (242, 300)
img_resized = img2.resize(newsize)
img.paste(img_resized, (110, 90))
# Call draw Method to add 2D graphis in an image
I1 = ImageDraw.Draw(img)
# Custom font style and font size
myFont = ImageFont.truetype("HELVETICAINSERATLTSTD-ROMAN.OTF", 60)
# Add Text to an image
I1.text((480, 230), "KUNAL\nBHARADKAR", font=myFont, fill =(245, 169, 6),align ="center")
# Save the edited image
img.save("done.jpg")
Edited Frame

In this task, I extracted photo frames from the video which I want be make changes and done all changes into each & every frame, then make a video using ImageSequenceClip with 25 frames/sec.

You can also use OpenCV for same here

Creating a video using moviepy ImageSequenceClip

# creating a Image sequence clip with fps = 25clip2 = ImageSequenceClip(clips1, fps =25)audioclip = AudioFileClip("audio.mp3").subclip(10,15)final = clip2.set_audio(audioclip)final.write_videofile("testing.mp4")

Below I showed part of the video which I edited

Original Video vs Edited Video

Conclusion:

So, here I just showed the basic example of the things that you can do without installing any heavy software or any online tool. Of course, you can do a lot more than I just showed here.

You can find the whole code here

Deployment of this app on the serverless platform

Thank you for reading this article…

--

--