Trimming Start Video Generates Negative Presentation Timestamps in FFmpeg
In this article, we will discuss the issue of negative presentation timestamps that can arise when trimming the start of a video using the FFmpeg command-line tool. We will cover the key concepts related to this problem and provide a detailed context to help you understand and resolve this issue.
What are negative presentation timestamps?
Negative presentation timestamps occur when the starting point of a media stream is set to a negative value, usually due to editing or trimming operations. In the context of video processing, negative presentation timestamps can cause synchronization issues between audio and video streams, leading to visual and auditory glitches.
FFmpeg command to trim the start of a video
The FFmpeg command to trim the start of a video is as follows:
ffmpeg -y -ss 00:00:02 -i inputFile.mp4 -movflags use_metadata_tags -map_metadata 0 -cc copy outputFile.mp4This command uses the -ss option to specify the start time of the trimming operation. In this example, the trimming starts at 2 seconds into the video. The -i option specifies the input file, while the -movflags use_metadata_tags and -map_metadata 0 options ensure that the metadata of the input file is copied to the output file.
Negative presentation timestamps in FFmpeg
When trimming the start of a video using the FFmpeg command mentioned above, you might encounter negative presentation timestamps. This issue can be caused by the way FFmpeg handles the timestamps of the trimmed video. By default, FFmpeg sets the starting point of the trimmed video to zero, which can result in negative presentation timestamps if the trimmed section contains media data with non-zero timestamps.
Resolving negative presentation timestamps in FFmpeg
To resolve the issue of negative presentation timestamps in FFmpeg, you can use the setpts filter to adjust the timestamps of the trimmed video. The following command demonstrates how to use the setpts filter to fix negative presentation timestamps:
ffmpeg -y -ss 00:00:02 -i inputFile.mp4 -vf "setpts=PTS-STARTPTS" -movflags use_metadata_tags -map_metadata 0 -cc copy outputFile.mp4In this command, the setpts filter is used to subtract the start time of the trimmed video (STARTPTS) from the presentation timestamps (PTS) of the media data. This ensures that the presentation timestamps of the trimmed video start at zero, preventing negative presentation timestamps.
- Negative presentation timestamps can occur when trimming the start of a video using FFmpeg.
- The issue is caused by the way FFmpeg handles the timestamps of the trimmed video.
- To resolve negative presentation timestamps, you can use the
setptsfilter to adjust the timestamps of the trimmed video.