Some video recorders have option to add data and time to recorded video. However others do not have that option. There are times when video was recorder without timestamp added but you need to have timestamp added to video.

Weather you have cameras in or around your house, or you are an private investigator, having the recording date and time permanently added into the video can be useful for documenting or surveying activities and events that have been recorded. You can see instantly when a particular event occurred or work out how much time had elapsed between two events.

There are many software products that you can buy to add date and time to video files, some are more expensive; others – less expensive. But all of them let you work one file at a time and still requires a lot of manual work. This is a step by step tutorial how to add date and time stamp for free and automate this process.

How To Add Timestamp to Video

In this section I will walk you step by how to setup the tools so you can automate date and time addition to your video files.

We will need a set of tools.

Step 1: Setup folder for the tools

Create a folder video-utils anywhere on your computer:

mkdir C:\video-utils
cd C:\video-utils

Step 2: Download ExifTool

In order to obtain original date and time when the video was taken, we will need to read video file’s meta data. This is where ExifTool comes into play. ExifTool is a free utility that cat Read, Write and Edit Meta Information of video and image files!

Go to https://oliverbetz.de/pages/Artikel/ExifTool-for-Windows and download the version based on your Operating System (OS). For Windows users here is a direct link to download ExifTool version 12.57: https://oliverbetz.de/cms/files/Artikel/ExifTool-for-Windows/exiftool-12.57_64.zip

If you downloaded this file into C:\Downloads folder you can use any method to unzip it or you can run the following commands to create exiftool folder and unzip the file that you just downloaded:

mkdir C:\video-utils\exiftool
tar -xf exiftool-12.57_64.zip -C C:\video-utils\exiftool

Step 3: Download FFmpeg

FFmpeg software is comprised of a collection of libraries and programs for managing multimedia files and streams, available to use freely and openly. ffmpeg command-line tool, which is specifically designed for processing video and audio files.

Go to https://ffmpeg.org/download.html page and download the version for your OS. Extract download zip file and copy all content to C:\video-utils\ffmpeg folder.

You can also use this direct link to download the latest version from FFmpeg from GitHub and use the following commands to unzip files and move to C:\video-utils\ffmpeg folder:

tar -xf ffmpeg-master-latest-win64-gpl.zip
move ffmpeg-master-latest-win64-gpl C:\video-utils\ffmpeg

Step 4: Write batch script to add date and time to video file

Before we can add date and time to video we need to extract video creation time from the file. We will use exiftool for that. Let’s create a folder which we will use to place our video files:

mkdir C:\video-utils\video
cd C:\video-utils\video

Assume we have a video file named 00000.MTS. Let’s copy this file to cd C:\video-utils\video folder. Let’s use exiftool to get meta information from this file by running the following command.

C:\video-utils\exiftool\exiftool 00000.MTS

You should see the following output, the output will be different based on the video file, camera that was used to record it:

C:\video-utils\video>C:\video-utils\exiftool\exiftool 00000.MTS
ExifTool Version Number         : 12.57
File Name                       : 00000.MTS
Directory                       : .
File Size                       : 15 MB
Zone Identifier                 : Exists
File Modification Date/Time     : 2023:02:24 12:29:26-05:00
File Access Date/Time           : 2023:03:02 09:28:17-05:00
File Creation Date/Time         : 2023:03:02 09:28:14-05:00
File Permissions                : -rw-rw-rw-
File Type                       : M2TS
File Type Extension             : mts
MIME Type                       : video/m2ts
Video Stream Type               : H.264 (AVC) Video
Audio Bitrate                   : 448 kbps
Surround Mode                   : Not indicated
Audio Channels                  : 3/2
Audio Stream Type               : PGS Audio
Image Width                     : 1920
Image Height                    : 1080
Date/Time Original              : 2023:01:11 14:27:15-05:00
Aperture Setting                : Auto
Gain                            : 0 dB
Exposure Program                : Program AE
White Balance                   : Auto
Focus                           : Manual (0.85)
Image Stabilization             : On (0x3f)
Exposure Time                   : 1/852
F Number                        : 3.4
Make                            : Sony
Camera Model Name               : HDR-CX455
Warning                         : [minor] The ExtractEmbedded option may find more tags in the video data
Audio Sample Rate               : 48000
Duration                        : 7.74 s
Aperture                        : 3.4
Image Size                      : 1920x1080
Megapixels                      : 2.1
Shutter Speed                   : 1/852

Original date and time of video recording is stored in Date/Time Original metatag. We only need to the date and time without meta tag text. Let’s use the following command to extract it:

C:\video-utils\exiftool\exiftool -b -DateTimeOriginal 00000.MTS

You might see output similar to this one:

Warning: [minor] The ExtractEmbedded option may find more tags in the video data - 00000.MTS
2023:01:11 14:27:15-05:00
  -DateTimeOriginal - Output only Original Date/Time
  -b      (-binary) - Output metadata in binary format

There can be cases were the warning is printed indicating that video file has embedded metatags. We do not want this message to be printed so we will suppress it by adding two -q arguments indicating that we do not want to see any warnings:

C:\video-utils\exiftool\exiftool -q -q -b -DateTimeOriginal 00000.MTS

Now we only see original date and time:

2023:01:11 14:27:15-05:00

We will use ffmpeg to draw text on top of the video starting with this date and time that we extracted. We need to pass this date and time to ffmpeg using epoch/unix time. Unix time is a date and time representation used in computing which measures time by the number of seconds that have elapsed since 1 January 1970. To convert this time to unix time we will use powershell command. But before we can do that we need to fix date separators. As you can see it is using colons to separate year, month and date, however we need to use dashes instead.

We will run exiftool and store output into two environment variables originaldate and originaltime and will replace colons with dashes only for date. Once we have accomplished that we will run powershell command to get unix time.

Let’s create a batch file named C:\video-utils\video\add-date.cmd. And add the following script to it:

@echo off

FOR /F "tokens=1,2 usebackq" %%A IN (`C:\video-utils\exiftool\exiftool -q -q -b -s -DateTimeOriginal 00000.MTS`) DO (
SET originaldate=%%A
SET originaltime=%%B
)

for /f "delims=" %%a in ('powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date "%originaldate::=-%T%originaltime%")).TotalSeconds"') do set "unixtime=%%a"

Save the file and run it from command line and let’s print unixtime to see the value:

C:\video-utils\video>add-date.cmd

C:\video-utils\video>echo %unixtime%
1673447235

Now that we have Unix time in unixtime environment variable we can pass this value to ffmpeg command line tool. Let’s add the following line at the end of add-date.cmd batch script file:

C:\video-utils\ffmpeg\bin\ffmpeg -i 00000.MTS -vf "drawtext=fontfile=c:\Windows\Fonts\arial.ttf:text='%%{pts\:gmtime\:%unixtime%}':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=w-text_w-10:y=h-text_h-10" -c:a copy -y 00000_withtime.mp4

Note: arial.ttf font file will be used for drawing text, the assumption is that the Windows OS is installed in C:\Windows folder. If the folder is different, modify the path in highlighted area above.

Save the file and run add-date.cmd batch script file.

C:\video-utils\video>add-date.cmd

You should see similar output to this:

ffmpeg version N-109948-ge026e29460-20230301 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 12.2.0 (crosstool-NG 1.25.0.90_cf9beb1)
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-version3 --disable-debug --enable-shared --disable-static --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --disable-avisynth --enable-chromaprint --enable-libdav1d --disable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --disable-frei0r --enable-libgme --enable-libkvazaar --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --disable-libmfx --enable-libvpl --enable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --disable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --disable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --disable-libx264 --disable-libx265 --disable-libxavs2 --disable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20230301
  libavutil      58.  3.100 / 58.  3.100
  libavcodec     60.  5.100 / 60.  5.100
  libavformat    60.  4.100 / 60.  4.100
  libavdevice    60.  2.100 / 60.  2.100
  libavfilter     9.  4.100 /  9.  4.100
  libswscale      7.  2.100 /  7.  2.100
  libswresample   4. 11.100 /  4. 11.100
Input #0, mpegts, from '00000.MTS':
  Duration: 00:00:07.01, start: 1.033367, bitrate: 17506 kb/s
  Program 1
  Stream #0:0[0x1011]: Video: h264 (High) (HDMV / 0x564D4448), yuv420p(top first), 1920x1080 [SAR 1:1 DAR 16:9], 29.97 fps, 59.94 tbr, 90k tbn
  Stream #0:1[0x1100]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, 5.1(side), fltp, 448 kb/s
  Stream #0:2[0x1200]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090), 1920x1080
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> mpeg4 (native))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
Fontconfig error: Cannot load default config file: No such file: (null)
[Parsed_drawtext_0 @ 000002d1e0e56640] Using "C:/Windows/fonts\mingliu.ttc"
[mp4 @ 000002d1ddfbbd00] track 1: codec frame size is not set
Output #0, mp4, to '00000_withtime.mp4':
  Metadata:
    encoder         : Lavf60.4.100
  Stream #0:0: Video: mpeg4 (mp4v / 0x7634706D), yuv420p(top coded first (swapped)), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 29.97 fps, 30k tbn
    Metadata:
      encoder         : Lavc60.5.100 mpeg4
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
  Stream #0:1: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, 5.1(side), fltp, 448 kb/s
frame=  210 fps=110 q=31.0 Lsize=    9416kB time=00:00:06.97 bitrate=11057.9kbits/s speed=3.64x
video:9029kB audio:383kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.043234%

Once the ffmpeg completes processing video file you should see 00000_withtime.mp4 file. Run the following command to confirm it:

dir

You should see similar output:

C:\video-utils\video>dir
 Volume in drive C is OS
 Volume Serial Number is Z12A-CC1B

 Directory of C:\video-utils\video

03/02/2023  09:59    <DIR>          .
03/02/2023  09:59    <DIR>          ..
02/24/2023  12:29        15,335,424 00000.MTS
03/02/2023  10:48         9,642,457 00000_withtime.mp4
03/02/2023  10:48               626 add-date.cmd

Step 5: Modify batch script to add date and time to all .MTS files in folder

Now let’s modify batch script to process all .MTS video files (you can modify what video file extensions you would like to include in the script) in C:\video-utils\video folder. We will loop through the files with .MTS extension and invoke exiftool and ffmpeg tools.

Edit add-date.cmd file and replace content with the one below:

@echo off

set path=../ffmpeg/bin;../exiftool;%path%

FOR /R "." %%b IN (*.MTS) DO ( 
  (Echo "%%b" | FIND /I "_withtime" 1>NUL) || (
    FOR /F "tokens=1,2 usebackq" %%c IN (`exiftool -q -q -b -s -DateTimeOriginal "%%b"`) DO (
      echo Processing %%b file. Creating %%~nb_withtime.mp4. This process can take some time...
      call :GetOriginalDate "%%b" "%%~nb_withtime.mp4" %%c %%d
    )
  )
)
goto :end

:GetOriginalDate
set originaldate=%3
set creationdate=%originaldate::=-%T%4
call :GetUnixTime %1 %2 %creationdate%
goto :end

:GetUnixTime
for /f "delims=" %%x in ('powershell -command "(New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date "%3")).TotalSeconds"') do (
  call :AddDateTime %1 %2 %%x
)
goto :end

:AddDateTime
ffmpeg -hide_banner -loglevel error -i "%1" -vf "drawtext=fontfile=arial.ttf:text='%%{pts\:gmtime\:%3}':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=w-text_w-10:y=h-text_h-10" -c:v libx265 -crf 16 -c:a copy -y "%2"
goto :end

:end

We have added For loop and recursively searching files with .MTS extension. If you would like to include any other extension you can do that by separating them by comma:

FOR /R "." %%f IN (*.MTS, *.AVI, *.MPEG) DO ( ...

I hope this tutorial was helpful. Please Contact Us if you have any questions or suggestions or would like to receive fully packaged software for adding date and time to all video files.

Categories: Uncategorized