In this tutorial, we investigate the NVIDIA GPU Acceleration mechanism to post-process a video. Although terminal execution is pretty easy and straightforward, for educational purposes, the pure Pythonic approach has been chosen.

Introduction

FFmpeg is one of the most famous multimedia frameworks which is widely used for processing videos. In order to encode the video, certainly, a video encoder must be used. The popular x264 is the one which is widely used however it is not super fast! The latest NVIDIA GPUs contain a hardware-based video encoder called NVENC which is much faster than traditional ones. In order to be able to utilize this GPU-accelerated encoder, FFmpeg must be installed with NVENC support. The full documentation of FFmpeg integrated with NVIDIA can be found at here. documentation on NVENC can be found here. Moreover, The NVENC programming guide can be found here. OpenCV can be used as well for video rotation (please refer to this post to see how ). However, using FFmpeg along with GPU make things much faster. The full code for this tutorial is available at this github repository.

In this tutorial, the main goal is to show how to do video rotation with GPU-accelerated libraries in Linux. In this tutorial, we do not use the terminal commands directly for employing the FFmpeg with NVENC support. Instead, the python interface is being used to run commands in the terminal. This can be done using the sub-process python module. This module is employed for execution and dealing external commands, intended to supersede the os.sys module. The trivial method os its usage will be explained in this tutorial. Please refer to this documentation for further details.

The assumption of this tutorial is that the FFmpeg is already installed with NVENC support. The installation guide can be found in the FFMPEG WITH NVIDIA ACCELERATION ON UBUNTU LINUX documentation provided by NVIDIA.

Data Indicator

This tutorial is customized for processing multiple videos. The assumption is that the full path of each video is stored in a .txt file in a line-by-line format. The example of the “.txt” file is as below:

The txt file format
The format of .txt file.

As a guidance if a recursive search for specific files in a directory and its sub-directories with extension “.png” is desired, the following method in command line is useful and it saves the output as a “.txt” file:

Search for files

Video Rotation

From now on the assumption is that the “.txt” file is ready and well-formatted. The python script for processing videos is as below:

Code

Overall Code Description

The videos.txt file is saved in the absolute path. Lines 8–13 of the code reads the “.txt” file and stores each line as an item of a list called files_list. The loop starts at line 17 process each file with the subprocess.callcommand. In each loop, the folder of the input file is found and the output file will be stored in the same directory but with the different naming convention which is explained by the comments in the code. Each, in the subprocess.call command in the Python, is correspondent to an empty space in the terminal. As an example the correspondent shell command is as below:

Building essential tools

FFMPEG Encoder

The command executed by FFmpeg needs to be described. Each of the elements started by — are calling specific operations and the command follows by them execute the desired operation. For example -vcodec indicator will specify the codec to be used by FFmpeg and nvenc which follows by that point to the codec. More details can be found at FFmpeg Filters Documentation. The following Table, summarize the indicators:

video rotation Arguments

The -vf is the main command which its full documentation is available at here and it has the filter options.

Code Execution

In order to run the python file we go to the terminal and execute the following:

Building essential

As a consideration, if we are working on any specific virtual environment it has to be activated at first.


Conclusion

In this post, we investigated the NVIDIA GPU Acceleration mechanism to post-process a video. Although terminal execution is pretty easy and straightforward, for educational purposes, the pure Pythonic approach has been chosen.

Leave a Comment

Your email address will not be published. Required fields are marked *

Tweet
Share
Pin
Share