Reverse a video using FFMpeg
This article details the specific ffmpeg filtergraphs required to reverse media streams, tailored for the Ubuntu operating system environment. While standard video editors on Ubuntu (like Kdenlive or Shotcut) can reverse footage, doing so via the command line offers precision, automation potential, and a lack of rendering overhead.
Ubuntu Environment and Format Support
Before manipulating video streams, it is vital to ensure your Ubuntu installation can decode the input files and encode the desired output.
- Ubuntu comes with robust support for open standards. Containers like Matroska (.mkv) and WebM (.webm), and codecs like VP9 or AV1, are often supported out of the box.
- However, many users work with MP4 files containing H.264 video and AAC audio. On a fresh Ubuntu installation, playback and processing of these proprietary formats may require the "Ubuntu Restricted Extras" package to be installed.
- If you encounter errors regarding "unknown encoder" when trying to output to specific formats, you may need to install the extra multimedia support packages via the terminal or the Ubuntu Software Center.
- Ensure your working directory has write permissions. Ubuntu's default security permissions usually allow users full control over their /home/username/ directories (e.g., Videos or Documents folders), which is the ideal place to store your input files.
The Basic Reverse Filter
The core mechanism for reversing footage in FFmpeg is the reverse video filter. This command loads the entire video stream into memory, buffers it, and then writes it out in reverse order.
Warning regarding system resources: Because FFmpeg must buffer the frames to reverse them, reversing very long videos (e.g., an hour-long movie) requires massive amounts of RAM. For large files on a standard Ubuntu machine with 8GB or 16GB of RAM, it is highly recommended to split the video into smaller segments first.
Scenario 1: Reversing Video Only (Removing Audio)
If you strictly want the visual aspect reversed and do not require the audio track (or the input has no audio), use the -vf (video filter) flag.
- Open your terminal in the directory containing your video.
- Execute the following command. Note that -an tells FFmpeg to discard the audio stream entirely.
ffmpeg -i input.mp4 -vf reverse -an output_reversed.mp4
- This process decodes the input, applies the reverse filter, and re-encodes the stream.
Reversing Video and Audio Simultaneously
Simply reversing the video stream will not automatically reverse the audio. If you do not handle the audio stream explicitly, the resulting file will have the video playing backwards while the audio plays forwards, leading to severe desynchronization.
To handle sound, you must use the areverse (audio reverse) filter in conjunction with the video reverse filter.
- To achieve this, we use a complex filtergraph enabled by the -filter_complex flag. This allows us to create a chain of operations.
- The command syntax involves mapping the streams. We tell FFmpeg to take the video stream ([0:v]) and apply reverse, and take the audio stream ([0:a]) and apply areverse.
- Execute the command below:
ffmpeg -i input.mp4 -vf reverse -af areverse output_synced.mp4
- Alternatively, using the complex filter syntax for stricter control over stream mapping:
ffmpeg -i input.mp4 -filter_complex "[0:v]reverse[v];[0:a]areverse[a]" -map "[v]" -map "[a]" output_complete.mp4
High-Quality Reversal and Codec Selection
When you reverse a video, FFmpeg must re-encode the file. By default, it may choose a generic quality setting that could result in compression artifacts (blockiness or blur). On Ubuntu, maximizing compatibility with the default "Videos" player (Totem) often involves using the H.264 codec.
- To preserve quality, we use the -crf (Constant Rate Factor) flag. Lower numbers mean higher quality and larger file sizes. A value of 18 to 23 is generally considered visually lossless.
- We also specify the preset, which determines how hard the CPU works to compress the file. On Ubuntu, using preset slow or preset veryslow results in better compression efficiency.
- The following command reverses both streams with high visual fidelity:
ffmpeg -i input.mp4 -vf reverse -af areverse -c:v libx264 -crf 18 -preset slow -c:a aac output_hq.mp4
Trimming and Reversing a Specific Segment
Because reversing entire files is memory-intensive, it is often practical to trim a specific segment of interest and reverse only that portion.
- Use the -ss (start time) and -to (end time) flags.
- It is crucial to place these flags before the -i input flag to ensure "input seeking," which is faster and more accurate for this purpose on Ubuntu systems handling large files.
- Example: To reverse a clip starting at 00:00:10 and ending at 00:00:20 (a 10-second clip):
ffmpeg -ss 00:00:10 -to 00:00:20 -i input.mp4 -vf reverse -af areverse output_segment.mp4
Handling Large Files via Segmentation
If you absolutely must reverse a massive video file that exceeds your Ubuntu system's RAM capacity, the workflow requires breaking the file into chunks, reversing them individually, and concatenating them back together.
- Step 1: Segment the video. The segment muxer can split files automatically.
- Step 2: Reverse each segment using a bash loop (common in Ubuntu terminal workflows).
- Step 3: Concatenate the reversed segments. Note that you must concatenate them in reverse order (e.g., segment 3, then segment 2, then segment 1) to achieve a true full reversal.
- While the full script for this is complex, a simple loop command to reverse all .mp4 files in a folder (after you have manually split them) would look like this:
*for f in .mp4; do ffmpeg -i "$f" -vf reverse -af areverse "reversed_$f"; done
Extending Format Compatibility
Ubuntu is highly versatile regarding formats. If you are reversing a video intended for web use or open-source software, you might prefer the .webm container using the VP9 codec. This format is natively supported by almost all modern browsers and Linux-based media players.
- VP9 offers excellent compression but requires significant CPU power to encode.
- The audio codec typically used here is Opus or Vorbis.
- Command to reverse a video and convert it to a WebM format for Ubuntu-native playback:
ffmpeg -i input.mp4 -vf reverse -af areverse -c:v libvpx-vp9 -b:v 0 -crf 30 -c:a libopus output.webm
Online Tools
You can also reverse video and audio files using the online webm video conversion service https://webmconverter.app and select one of the audio or video converters located on the following pages. For video and audio reversal pick one of the video converters located at https://webmconverter.app/video-converters or for only audio reversal pick one of the audio converters located at https://webmconverter.app/audio-converters