In this project we will set the internal clock on the GoPro and then use that to add a timestamp overlay onto our videos.
I was working on a project this summer in which we placed multiple GoPro cameras on a commercial fishing trawl. For this project I was looking for a way to synchronize the data collected
from multiple cameras. My first thought was to use the WiFi remote to turn on all the cameras at the same time. This would synchronize the cameras but I didn't trust it and leaving
the WiFi on would shorten the battery life. Additionally, this process would link the cameras but I would then have to match them to a clock in the outside world to interpret the
data. I decided to use the internal clock of the GoPro to put a timestamp on the video files. The camera clocks were synchronized to the clock on my computer. This allowed for
comparisons between cameras, with other instrumentation on the net and to real world events. Overall the script worked for what was needed. In addition to the basic timestamp I added
a filewalker for batch processing and added data from a nearby depth sensor to the text overlay. In the future I would like to work on speeding the program up. Additionally, due to
limitations in the OpenCV libraries the audio is removed from the file and the file is converted from MP4 to AVI.
This script has been tested on GoPro Hero 2, 3 black and 3+ black models.
If your only interested in relative time, for measuring the duration of an event for instance, then you can skip this step. If your interested on having your videos stamped with the real-world time or having them synchronized with other cameras then you need to set the correct clock time. Unfortunately, the settings menu within the GoPro only allows you to set the minutes and hours, not the seconds. For more accuracy you can set the time using the GoPro app on your smartphone or tablet over WiFi or using the WiGo program for desktop computers (also uses WiFi).
For this script to work place the script file (GoPro_timestamp.py) and FFProbe into the folder with your GoPro video. Open the script in your python editor of choice. Change the line of the script with the filename you wish you timestamp, then run the script.
This script works by creating a call to FFProbe which returns the metadata from the MP4 file of interest. When the GoPro camera creates a video file the creation time is embedded into the
files metadata. FFProbe returns all the metadata which is then parsed down to the creation time. This value is then used to initiate a counter. OpenCV then loops through each
frame adds the timestamp, exports it to the return file and then adds the frame duration to the counter. This script takes some time to run, depending on the length of the file. You
can press the 'esc' button to cancel out at anytime.
I have added an updated script. This uses OpenCV 3. Additioanlly, this one uses the full FFmpeg pipeline to encode and save the video frames. This results in a faster, better compression with a bit more flexibility for different codecs, if people feel like venturing down that path. The only requirement is to place the ffmpeg.exe file in the same folder as the script. Everything else works the same. To get the latest copy of FFmpeg follow this link. Download the latest static library. Unzip it. The appropriate files should be located in the bin folder.
Adding a filewalking function will go through a folder and timestamp any and all MP4 files. This is great for loading up a queue and batch processing a group of files. Set them up and
go home for the night. The file attached below works the same as the base script except that it uses the os.walk() function in Python which creates a list of every file in the
directory. Each file is iterated through. If it contains a '.MP4' file extension then the timestamp script is executed. Currently the filewalker is set-up to only go through the
folder listed in the script. It can be set-up to run through sub-folders in needed.
To execute this script, place it and FFProbe in the folder containing your videos. Tell the script the directory to start its search by changing the indir line. Then let her rip.
The last modification that I made was to add data from a nearby depth sensor to the video. Any data with a timestamp can be overlaid onto the video creating a HUD (heads up display) like system. This can be a powerful tool to correspond observations in the video to external events.
For this project we used an RBR depth sensor programmed to record depth and pressure every second. The data was exported to a text file and the headers were stripped from the file, a sample file is attached below. Having the depth sensor capture data every second allowed me to match the time (hh:mm:ss) of the videos timestamp with the time of the depth data. Matching the time was done by stripping the video time of the milliseconds value and then searching for a corresponding time value in the depth data. The Python dictionary data structure provided a novel way to store and search the data file.
To execute this script, place all the files (video, ffprobe, script and external data file) in the same folder. Change the data_name and video_filename, within the script, to the files of
interest and run the script. I have additionally add another file which combines both the filewalker feature and the data overlay. This script uses a single external data file but can
process a batch of video files. As such the user must make sure all the applicable external data is placed in the file for the span of video files being processed.