Turn your code into any language with our Code Converter. It's the ultimate tool for multi-language programming. Start converting now!
Digital media such as videos created a lot of creative options where we can process video and audio files and create awesome effects with them. A lot of media processing programs and tools offer the reversing feature.
In this tutorial, you will learn how you can reverse a video in Python using the MoviePy library.
The basic idea behind the code you'll see in this tutorial, is we extract all the frames from the video using a configurable fps parameter, then we load these frames back into a video in the reverse order.
To get started, let's install the required libraries:
Let's start now by importing our modules:
Next, to avoid redundancy, I'm bringing the below code from this tutorial:
You'll get a lot of details in the extracting frames tutorial. However, in brief, the extract_frames()
function accepts the video file path as a parameter and extracts frames with the corresponding duration into a folder named after the original video file name. Finally, it returns that folder name.
Now, let's make a function that reads these extract frames in the reverse order and save them as an inverted video:
The reverse_video()
function expects the folder name that contains the video frames extracted by the previous function as an argument. We use the glob()
function from the glob module to get all the file names of the frames.
Next, we sort those frame files by the duration in descending order. After that, we pass these frames in the reversed order to the ImageSequenceClip()
object from MoviePy, and set the FPS to the minimum of SAVING_FRAMES_PER_SECOND
we used in the frame extraction process and the original video FPS, the reason is when we set a higher FPS than the original video FPS, the resulting video will be speeded.
We then use the write_videofile()
method to save the reversed video to a video file on the disk.
If you set the remove_extracted_frames
to True
(as the default), the folder where the extracted frames are located will be deleted along with its contents.
Finally, let's use these functions to accomplish our task:
We're done! Let's try it out with a YouTube video of a scene from TENET movie:
This is the output of the code:
And the reversed video appeared in the current directory!
If your video is quite long, then make sure to reduce the FPS (the SAVING_FRAMES_PER_SECOND
parameter), I have set it to as low as 10, you can increase it if you feel the lag in the output video, this will increase the size of the video, as well as the execution time of the program to extract frames and load them in the reverse order.
Obviously, the output video will have no sound, you can use AudioClip()
loaded from audio (maybe the audio extracted from the original video), and simply set image_sequence_clip.audio
to this newly created AudioClip()
object, and then continue the same process of saving the video.
You can get the demo video here, and the full code here.
Learn also: How to Combine a Static Image with Audio in Python
Happy coding ♥
Take the stress out of learning Python. Meet our Python Code Assistant – your new coding buddy. Give it a whirl!
View Full Code Generate Python Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!