How to Match Reverse Audio and Video in FFmpeg
Reversing a video in FFmpeg requires reversing its corresponding
audio track to maintain perfect synchronization. This article explains
how to use the areverse filter alongside the
asetpts filter to reset presentation timestamps, ensuring
your reversed audio matches the reversed video stream seamlessly without
playback or sync issues.
To reverse both video and audio in FFmpeg, you must apply the
reverse filter to the video stream and the
areverse filter to the audio stream. When reversing audio,
the original presentation timestamps (PTS) can become corrupted or
disjointed. To fix this, you chain the asetpts=PTS-STARTPTS
filter immediately after reversing the audio. This resets the timeline
of the reversed audio packets so they start cleanly at zero.
The Standard FFmpeg Command
Use the following command to reverse both streams and correct the audio timestamps:
ffmpeg -i input.mp4 -vf reverse -af "areverse,asetpts=PTS-STARTPTS" output.mp4Command Breakdown
-i input.mp4: Specifies your source video file.-vf reverse: Applies the video filter to play the video backward. Note that this filter buffers the entire video in memory, so it is best suited for shorter clips.-af "areverse,asetpts=PTS-STARTPTS": Applies a chain of two audio filters:areverse: Reverses the audio stream.asetpts=PTS-STARTPTS: Recalculates the Presentation Time Stamp (PTS) of each audio frame relative to the start of the stream, preventing sync issues in media players.
Handling Video Timestamps (Optional)
While resetting audio timestamps is usually sufficient, you can also
reset the video timestamps using setpts to ensure complete
synchronization across all media players:
ffmpeg -i input.mp4 -vf "reverse,setpts=PTS-STARTPTS" -af "areverse,asetpts=PTS-STARTPTS" output.mp4By resetting both the video (setpts=PTS-STARTPTS) and
audio (asetpts=PTS-STARTPTS) timestamps, you guarantee that
the reversed output file behaves exactly like a standard video file
during playback and editing.