800+ custom stores made since 2014 🚀

How to edit video features using python? by Aemie Jariwala

Aemie Jariwala
Apr 17, 2020, 11:40 AM

INTRODUCING THE WORLD OF CUSTOMIZATION WITH VIDEOS

Hello, everyone!
This post will provide you with information such that you won't require a video editing app or tool to speed up your video or convert your video to gif. That's 'cause we can easily achieve it based on our personal customization and not restricted to the ones provided by the app with FFmpeg and python.

What is FFmpeg?

FFmpeg is a collection of libraries and tools to process multimedia content such as audio, video, subtitles and related metadata. With FFmpeg, you can enter the amusing environment with custom options provided, you can speed up or slow down the video speed to any value provided ( NO RESTRICTIONS! ) and so much more.

Python is not only easy to learn but also the best language to give your code an artistic nature.

Thus, we will be using Python to run FFmpeg scripts in the command line. It is an easy process and you will fall in love with what all can be done with both FFmpeg and Python combined.

Libraries to dig

As we are using python as the primary language, you need to have pip and python version: 3.x installed on your system. Apart from that we will require the following libraries which will be installed using pip:

  1. subprocess
  2. shlex

That seems a pretty short list but it's enough, believe me!

Let's Code!

First, make sure you've all the libraries installed using pip. Let's say we've got a sample.mp4 (could be any video file) stored in your local system. We will define the file path and use FFmpeg commands using subprocess and shlex to speed up and slow down our videos.

Ok, that's just the gist idea of what we will code and now doing the essential things first, we import the libraries.

import subprocess, shlex

Now that the libraries are imported, we use the file path and assign it to a variable.

path = 'C://Users/Videos/sample.mp4' #Must include the path to your video file
output_path = 'C://Users/Videos/' #Output path required for later use

# Code to run the FFmpeg command in the command line     

We have accessed the file and now all we need to do is use FFmpeg commands using subprocess and shlex and then we are all set.

To speed up the video by x times, one needs to divide the fps by x. This will be clear with the code example where we want to increase the speed by let's say 4 times.

# Speed up video
cmd1 = 'ffmpeg -i '+file_name+' -filter:v setpts="0.25*PTS" '+output_path+'/output_fast.mp4'
subprocess.call(shlex.split(cmd1))

In case you want to slow down the video, you will have to use a multiplier greater than 1. For instance, we will slow down the video by 0.5x times.

# Slow down video 
cmd2 = 'ffmpeg -i '+file_name+' -filter:v setps="2*PTS" '+output_path+'/output_slow.mp4'
subprocess.call(shlex.split(cmd2))

Let's combine all the code into one.

path = 'C://Users/Videos/sample.mp4' #Must include the path to your video file
output_path = 'C://Users/Videos/' #Output path required for later use

# Code to run the FFmpeg command in the command line

# Speed up video
cmd1 = 'ffmpeg -i '+file_name+' -filter:v setpts="0.25*PTS" '+output_path+'/output_fast.mp4'
subprocess.call(shlex.split(cmd1))

# Slow down video 
cmd2 = 'ffmpeg -i '+file_name+' -filter:v setps="2*PTS" '+output_path+/output_slow.mp4'
subprocess.call(shlex.split(cmd2))

The setpts can be set according to your personal customization and as said before there are no restrictions. Apart from that, when you run the code, you'll have the output_slow.mp4 and output_fast.mp4 in your local system.

BONUS CODE

Let's not just end here, I can teach you how to easily convert a video into gif using FFmpeg without using online tools or photoshop.

cmd = 'ffmpeg -i '+file_name+' -vf "fps=10,scale=320:-1:flags=lanczos" -c:v pam -f image2pipe - | convert -delay 10 - -loop 0 -layers optimize '+output_path+'/output.gif' 
subprocess.call(shlex.split(cmd))

This will convert your video file to an optimized gif file. For a better understanding of this code, it will be explained in the next series.

Latest Posts

    Stay Connected

    Get misiki news delivered straight to your inbox

    © 2023 Misiki Technologies

    All Rights Reserved