encode video in reverse?
Asked Answered
C

3

18

Does anyone know if it is possible to encode a video using ffmpeg in reverse? (So the resulting video plays in reverse?)

I think I can by generating images for each frame (so a folder of images labelled 1.jpg, 2.jpg etc), then write a script to change the image names, and then re-encode the ivdeo from these files.

Does anyone know of a quicker way?

This is an FLV video.

Thank you

Consul answered 31/3, 2010 at 13:50 Comment(0)
E
19

No, it isn't possible using ffmpeg to encode a video in reverse without dumping it to images and then back again. There are a number of guides available online to show you how to do it, notably:

and

The latter of which follows:

Dump all video frames

$ ffmpeg -i input.mkv -an -qscale 1 %06d.jpg

Dump audio

$ ffmpeg -i input.mkv -vn -ac 2 audio.wav

Reverse audio

$ sox -V audio.wav backwards.wav reverse

Cat video frames in reverse order to FFmpeg as input

$ cat $(ls -r *jpg) | ffmpeg -f image2pipe -vcodec mjpeg -r 25 -i - -i backwards.wav -vcodec libx264 -vpre slow -crf 20 -threads 0 -acodec flac output.mkv

Use mencoder to deinterlace PAL dv and double the frame rate from 25 to 50, then pipe to FFmpeg.

$ mencoder input.dv -of rawvideo -ofps 50 -ovc raw -vf yadif=3,format=i420 -nosound -really-quiet -o - | ffmpeg -vsync 0 -f rawvideo -s 720x576 -r 50 -pix_fmt yuv420p -i - -vcodec libx264 -vpre slow -crf 20 -threads 0 video.mkv
Elviaelvie answered 15/11, 2011 at 14:12 Comment(9)
Cheers, very helpful. A small change -- using $(ls -r *jpg) instead of $(ls -t *jpg) is better (for me at least) as the modification times are too coarse to give the correct orderingLodie
@ChaitanyaChandurkar: Depends on how powerful the device is, processor architecture and clock speed etc etcMonanthous
For newer versions of ffmpeg you should replace -vpre slow to -preset slow in the last step.Persevering
This is factually incorrect now. FFMPEG now has a reverse filter, as well as an areverse filter for audio.Wilkens
This method is still better than ffmpeg's reverse filter, which buffers all the frames in RAM, uncompressed. A 1080p video that is 33mb and 2 minutes long uses 10gb ram.Boring
Tynach: I suggest you post that as an outright answer; maybe with a note that older versions of ffmpeg can't do that and with dequis' caution (which would mean: use one of the other solutions). For my purposes, your comment here was the best solution, as I used that ffmpeg filter to convert small .swf files that easily fit into memory.Katzen
The original answer gave me very jerky video until I found @simonb's answer, ls -r solves the problem.Darrow
The ls method is not necessary. See https://mcmap.net/q/740399/-ffmpeg-convert-image-sequence-to-video-with-reversed-order/5726027Pose
@Boring you don't need 10gb of physical ram if you create a swap file of say 9gbPurely
K
11

I've created a script for this based on Andrew Stubbs' answer

https://gist.github.com/hfossli/6003302

Can be used like so

./ffmpeg_sox_reverse.sh -i Desktop/input.dv -o test.mp4
Keitloa answered 15/7, 2013 at 20:46 Comment(2)
This script did not work for me. It fails to make the temporary dir with mktemp.Arched
I have OS X, maybe that's a reason?Keitloa
B
2

New Solution

A much simpler method exists now, simply use the command (adjusting input.mkv and reversed.mkv accordingly):

ffmpeg -i input.mkv -af areverse -vf reverse reversed.mkv

The -af areverse will reverse audio, and -vf reverse will reverse video. The video and audio will be in sync automatically in the output file reversed.mkv, no need to worry about the input frame rate or anything else.

On one video if I only specified the -vf reverse to reverse video (but not audio), the output file didn't play correctly in mkv format but did work if I changed it to mp4 output format (I don't think this use case of reversing video only but not audio is common, but if you do run into this issue you can try changing the output format). On large input videos that exceed the RAM available in your computer, this method may not work and you may need to chop up the input file or use the old solution below.

Old Solution

One issue is the frame rate can vary depending on the video, many answers depend on a specific frame rate (like "-r 25" for 25 frames per second). If the frame rate in the video is different, this will cause the reversed audio and video to go out of sync.

You can of course manually adjust the frame rate each time (you can get the frame rate by running ffmpeg -i video.mkv and look for the number in front of the fps, this is sometimes a decimal number like 23.98). But with some bash code you can easily extract the fps, store it in a variable, and automatically pass it to the programs.

Based on this I've created the following bash script to do that. Simply chmod +x it and run it ./make-reversed-video.sh input.mkv output.mkv. The code is as follows:

#!/bin/bash
#Partially based on https://nhs.io/reverse/, but with some modifications, including automatic extraction of the frame rate.

#Get parameters.
VIDEO_FILE=$1
OUTPUT_FILE=$2
TEMP_FOLDER=$3

echo Using input file: $VIDEO_FILE
echo Using output file: $OUTPUT_FILE

mkdir /tmp/create_reversed_video

#Get frame rate.
FRAME_RATE=$(ffmpeg -i "$VIDEO_FILE" 2>&1 | grep -o -P '[0-9\\. ]+fps' | grep -o -P '[0-9\\.]+')
echo The frame rate is: $FRAME_RATE

#Extract audio from video.
ffmpeg -i "$VIDEO_FILE" -vn -ac 2 /tmp/create_reversed_video/audio.wav

#Reverse the audio.
sox -V /tmp/create_reversed_video/audio.wav /tmp/create_reversed_video/backwards.wav reverse

#Extract each video frame as an image.
ffmpeg -i "$VIDEO_FILE" -an -qscale 1 /tmp/create_reversed_video/%06d.jpg

#Recombine into reversed video.
ls -1 /tmp/create_reversed_video/*.jpg | sort -r | xargs cat | ffmpeg -framerate $FRAME_RATE -f image2pipe -i - -i /tmp/create_reversed_video/backwards.wav "$OUTPUT_FILE"

#Delete temporary files.
rm -rf /tmp/create_reversed_video

I've tested it and it works well on my Ubuntu 18.04 machine on lots of videos (after installing the dependencies like sox). Please let me know if it works on other Linux distributions and versions.

Boron answered 25/7, 2021 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.