Fix FFmpeg Queue Input is Backward in Time Error
The “Queue input is backward in time” error in FFmpeg is a common issue that occurs when input frames or packets have non-monotonic or out-of-order timestamps. This article explains why this error happens, its impact on your media processing, and the most effective command-line solutions to resolve it.
What Does the Error Mean?
FFmpeg processes media files chronologically based on two types of timestamps: Presentation Timestamps (PTS), which dictate when a frame is displayed, and Decoding Timestamps (DTS), which dictate when a frame is decoded.
FFmpeg expects these timestamps to increase steadily. If a frame or
audio packet arrives with a timestamp that is earlier than the one
before it, the timeline goes “backward.” FFmpeg flags this discrepancy
with the warning or error:
Queue input is backward in time.
This issue typically occurs in the following scenarios: * Variable Frame Rate (VFR) files: Screen recordings or smartphone videos often have fluctuating frame rates that confuse the decoder. * Concatenated videos: Merging different video clips with mismatched timelines. * Live streaming: Network latency or packet loss causing audio and video packets to arrive out of order.
How to Fix the Error
Depending on your specific use case, you can resolve this issue using one of the following methods.
Method 1: Regenerate Timestamps (Recommended)
The most common fix is to force FFmpeg to ignore the broken source
timestamps and generate clean, new ones during the input reading phase.
You can do this by adding the -fflags +genpts option before
your input file.
ffmpeg -fflags +genpts -i input.mp4 -c:v libx264 -c:a aac output.mp4Method 2: Reset Timestamps with Filters
If you are re-encoding the video, you can use video and audio filters to reset the timestamps so they start at zero and increase monotonically.
ffmpeg -i input.mp4 -vf "setpts=PTS-STARTPTS" -af "asetpts=PTS-STARTPTS" output.mp4setpts=PTS-STARTPTSresets the video presentation timestamps.asetpts=PTS-STARTPTSdoes the same for the audio track, keeping them in sync.
Method 3: Force a Constant Frame Rate (CFR)
If the error is caused by a variable frame rate (VFR) source file,
forcing FFmpeg to output a constant frame rate will normalize the
timestamps. Use the -r flag to define a target frame rate
(e.g., 30 or 60 frames per second).
ffmpeg -i input.mp4 -r 30 output.mp4Method 4: Correct Timestamps for Stream Copying
If you are copying the video and audio streams without re-encoding
(using -c copy), you cannot use filters. Instead, use the
-avoid_negative_ts flag to shift all timestamps so they
start at zero, preventing backward jumps.
ffmpeg -i input.mp4 -c copy -avoid_negative_ts make_zero output.mp4