ffmpeg overlay timestamp with timezone offset without using Epoch

3 min read 22-10-2024
ffmpeg overlay timestamp with timezone offset without using Epoch

When working with video editing, you might find the need to overlay timestamps onto your videos. However, integrating time information with timezone offsets can sometimes be tricky, especially if you're trying to avoid using epoch time. In this article, we’ll walk you through a method to achieve this using FFmpeg.

Problem Scenario

You want to overlay a timestamp on a video with a specific timezone offset, but you don't want to use epoch time to do so. Below is the original FFmpeg command that you're likely trying to use:

ffmpeg -i input.mp4 -vf "drawtext=text='%{eif\:localtime\(n*1000\, \, \, \, 1\)\:h\:2\:d\:2} %{eif\:localtime\(n*1000\, \, \, \, 1\)\:m\:2\:d\:2} %{eif\:localtime\(n*1000\, \, \, \, 1\)\:s\:2\:d\:2}':x=10:y=10" -codec:a copy output.mp4

While this command uses epoch time to calculate and display the current timestamp, it can be quite complex and may not allow for straightforward timezone manipulations.

Understanding FFmpeg's Drawtext Filter

The drawtext filter is a powerful tool in FFmpeg that allows you to overlay text onto your video. To overlay a timestamp without using epoch, we can rely on FFmpeg's built-in time formatting options.

Revised Command

To accomplish your goal, you can use a command that utilizes the strftime function to display the current date and time based on the video's frame time. Here’s how you can do it:

ffmpeg -i input.mp4 -vf "drawtext=text='%{pts\:hms}':x=10:y=10:fontcolor=white:fontsize=24" -codec:a copy output.mp4

Explanation of the Command

  • -i input.mp4: This specifies the input video file.
  • -vf "drawtext=text='%{pts\:hms}'": This tells FFmpeg to overlay the text. Here, %{pts\:hms} generates a timestamp in hours, minutes, and seconds from the presentation timestamps of the video.
  • :x=10:y=10: This places the timestamp 10 pixels from the left and 10 pixels from the top of the video.
  • fontcolor=white:fontsize=24: This sets the color and size of the font for better visibility.
  • -codec:a copy output.mp4: This command copies the audio without re-encoding, resulting in faster processing.

Adding a Timezone Offset

While the command above displays a timestamp, it is in UTC by default. To adjust for a timezone offset (for example, UTC+3), you need to add the offset to the hours part manually. Here’s how you can modify the command:

ffmpeg -i input.mp4 -vf "drawtext=text='%{eif\:floor((pts/60)/60)+3\:d\:2}:%{eif\:floor((pts/60)%60)\:d\:2}:%{eif\:floor(pts%60)\:d\:2}':x=10:y=10:fontcolor=white:fontsize=24" -codec:a copy output.mp4

In this revised command:

  • +3 is added to the hours calculation to adjust for UTC+3.
  • floor((pts/60)/60) calculates the hours from the presentation timestamp.

Practical Example

Let's assume you have a video file named event.mp4, and you want to overlay the timestamp indicating the time of an event that takes place at UTC+3. You can run the command as follows:

ffmpeg -i event.mp4 -vf "drawtext=text='%{eif\:floor((pts/60)/60)+3\:d\:2}:%{eif\:floor((pts/60)%60)\:d\:2}:%{eif\:floor(pts%60)\:d\:2}':x=10:y=10:fontcolor=white:fontsize=24" -codec:a copy event_with_timestamp.mp4

This command will create a new video event_with_timestamp.mp4 with an overlay of the current time adjusted to the UTC+3 timezone.

Conclusion

Overlaying a timestamp with a specific timezone offset in FFmpeg without using epoch time can enhance the clarity of your videos, especially for events where local time matters. By using the drawtext filter creatively, you can achieve effective results while keeping your command relatively simple.

Useful Resources

Feel free to refer to the resources for more detailed options and examples. Happy editing!