FFmpeg Showing Incorrect Frame Number in Drawtext Output
When working with video files, it's often necessary to extract specific frames or add text overlays to the video. FFmpeg is a powerful tool that can be used for these tasks. However, some users have reported an issue where the frame number printed in the drawtext output is incorrect.
Understanding Subtitles and Text Overlays in FFmpeg
FFmpeg supports a wide range of video and audio formats, and it can be used to extract frames, convert formats, and add text overlays to videos. The drawtext filter is used to add text overlays to videos, and it can be used to display the current frame number, among other things.
The syntax for using the drawtext filter is as follows:
ffmpeg -i input.mp4 -vf "drawtext=text=%{n}:fontsize=72:fontcolor=white:borderw=3:bordercolor=black" output.mp4In this example, the %{n} expression is used to display the current frame number. However, some users have reported that the frame number displayed is incorrect.
Incorrect Frame Number in Drawtext Output
When using the drawtext filter to display the current frame number, some users have reported that the frame number displayed is incorrect. For example, the frame number may be displayed as 0 for the first frame, but then jump to a higher number for the next frame.
This issue can occur when the video has a variable frame rate (VFR). In a VFR video, the frame rate can change from one frame to the next. This can cause issues when trying to display the current frame number, as the frame rate can affect the timing of the video.
Solving the Issue
To solve the issue of incorrect frame numbers in the drawtext output, it's recommended to convert the video to a constant frame rate (CFR) before adding the text overlay. This can be done using the setpts filter in FFmpeg.
The syntax for converting a VFR video to a CFR video is as follows:
ffmpeg -i input.mp4 -vf "setpts=N/FRAME_RATE/TB" -c:v libx264 -crf 23 -c:a copy output.mp4In this example, the setpts filter is used to set the presentation timestamp of each frame to a constant value. The FRAME_RATE variable should be set to the desired frame rate for the output video.
Once the video has been converted to a CFR video, the drawtext filter can be used to add the text overlay, and the frame number displayed should be correct.
When working with FFmpeg and the drawtext filter, it's important to be aware of potential issues with variable frame rate videos. To avoid issues with incorrect frame numbers, it's recommended to convert the video to a constant frame rate before adding the text overlay.
References
- FFmpeg: https://ffmpeg.org/
- Setpts filter: https://ffmpeg.org/ffmpeg-filters.html#setpts
- Drawtext filter: https://ffmpeg.org/ffmpeg-filters.html#drawtext