GoPro Timestamp

In this project we will set the internal clock on the GoPro and then use that to add a timestamp overlay onto our videos.


Overview

This is a GoPro camera that we attached a commercial trawl net.
GoPro camera attached to a commercial trawl net

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.


Software used:


Project Files

Download
GoPro_Timestamp
This python file adds a timestamp to any GoPro video. This website service saves the file as a text file, remove the .txt extension to use with python.
GoPro_timestamp.py.txt
Text Document 3.6 KB
Download
FFProbe file
ffprobe.zip
Compressed Archive in ZIP Format 12.5 MB

How it works

Step 1: Set the clock time on the GoPro

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).

Step 2: Take some video

Shoot away!

Checking the GoPro camera and the net before it goes overboard.
Checking the camera and the net before it goes overboard.

Step 3: Adding the timestamp (post-processing)

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.

A python script for adding a timelapse overlay onto our GoPro video.

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.


Update:

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.

Download
Updated script with FFmpeg and OpenCV 3.0
GoPro_timestamp_FFMPEG_3.0.zip
Compressed Archive in ZIP Format 1.8 KB

Add-ons


Filewalker

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.

A python script for batch processing the videos to add a timestamp overlay.
Download
GoPro Timestamp with filewalker
This is the base timestamp file with a filewalker added.
GoPro_timestamp_filewalker.py.txt
Text Document 4.1 KB

Data Overlay

An underwater video still frame shown the timestamp and data overlay.
An video still frame showing the timestamp and data overlay.

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. 

Download
GoPro Timestamp with External Data
GoPro_timestamper_w_external_data.py.txt
Text Document 4.5 KB
Download
Sample external data
External_data.txt
Text Document 2.9 MB
Download
GoPro Timestamp with a Filewalker and External Data Overlay
GoPro_timestamper_w_filewalker_external_
Text Document 4.4 KB
Download
Basic GoPro timestamp with a image/watermark overlay
GoPro_timestamp_watermark.py.txt
Text Document 3.7 KB

Write a comment

Comments: 81
  • #1

    TF (Tuesday, 13 January 2015 18:01)

    I get this error message after running GoPro_timestamp.py.txt :

    >>> ================================ RESTART ================================
    >>>

    Traceback (most recent call last):
    File "C:\Users\friertnf\Desktop\test timestamp\GoPro_timestamp.py.txt", line 47, in <module>
    frame_lapse = (1/FPS)*1000
    ZeroDivisionError: float division by zero

  • #2

    CR (Wednesday, 14 January 2015 09:29)

    Hello TF,

    It looks like the video file is not being loaded. Here is script is trying to figure out the time between frames but it looks like the FPS variable is 0 (i.e. something isn't correct with the import). Check that the filename within the script is correct. If the file is placed in the same folder as the script then it can look like this:
    filename = 'Test_video.MP4'
    If the file is located outside the script folder then you need to add the directory information.
    filename = 'C:\Some_folder\Test_video.MP4'
    If that looks OK, you can test the video playback with the following script:
    ---------------------------------------------------------------------------------------------------------------
    import cv2

    filename = 'Tow2_T45CE_DMFH3_1.MP4'
    video = cv2.VideoCapture(filename)
    total_frames = video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
    current_frame = 0

    while current_frame < total_frames:
    success, image = video.read()
    current_frame = video.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
    cv2.imshow('Video', image)

    k = cv2.waitKey(33)
    if k == 27:
    break

    video.release()
    cv2.destroyAllWindows()
    ------------------------------------------------------------------------------------------------------------------

    I hope this helped.

  • #3

    theeminentcodfish (Wednesday, 14 January 2015 11:53)

    If it is a codec problem. Check out this page:

    http://stackoverflow.com/questions/13834399/cannot-open-mp4-video-files-using-opencv-2-4-3-python-2-7-in-windows-7-machi

    Try the second answer. I think that this is what worked for me a while ago.

  • #4

    TF (Wednesday, 14 January 2015 12:07)

    OK cool, I fixed the indentation, and NO error messages using a Python shell 2.7.8, but no success at playback. This error using Spyder (Python 2.7): ... video.release()
    File "<stdin>", line 11
    video.release()
    ^
    SyntaxError: invalid syntax

    I will try the codec fix.


  • #5

    theeminentcodfish (Thursday, 15 January 2015 08:28)

    Did you try the following response on the previous website:
    --------------------------------------------------------------------------------------------------------------------
    I have the same issue before, solved by this step :

    Check your OpenCV python version

    >>> from cv2 import __version__
    >>> __version__
    '2.4.0'

    Then Copy your opencv_ffmpeg.dll to C:\Python27\ and rename it to relevant your OpenCV Python Version. in my case I should rename it to opencv_ffmpeg240.dll
    ----------------------------------------------------------------------------------------------------------------

    I found this file in the OpenCV directory under opencv/build/x86/vc12/bin.

  • #6

    TF (Thursday, 15 January 2015 11:02)

    Yes, I did copy that .dll over to \Python27. The version I have is ffmpeg2410 however. Still getting the error about ZeroDivisionError: float division by zero. Even when I change the video filename to a nonexistant video, same error. The mp4 files are labeled to only open with VLC media player. I won't give up on this, will be so awesome to get it working!

  • #7

    theeminentcodfish (Thursday, 15 January 2015 11:40)

    Out of curiosity are you running a 32 or 64-bit version of python? Windows or Mac?

    I am sorry for all the frustration. It definitely sounds like a codec issue. I vaguely remember having this issue when I started with OpenCV and GoPro but that was years ago. At this point we just need to get the video to load. I have attached another script below the comments section which is just a simple playback script, but it will tell you when the video fails to load.

    I think it is a matter of getting the correct ffmpeg driver into the correct Python folder. In the OpenCV folder go to: opencv\sources\3rdparty\ffmpeg. Copy the ffmpeg.dll file into the Python27 folder, you shouldn't need the opencv_ffmpeg_64.dll unless you are running a 64-bit version of python. Rename it as listed above but only use three digits (i.e. ffmpeg241.dll). Hopefully this works!

  • #8

    TF (Tuesday, 20 January 2015 12:12)

    SUCCESS!!! I needed to change the opencv version # to 2410, apparently needed the ending 0. So renaming the opencv_ffmpeg_64.dll to opencv_ffmpeg2410_64.dll and moving it to the Python27 folder was the trick.

    Thanks for the support, codfish!

    Now my next question: How can I change the color of the timestamp text? It's common to have very dark margins in the underwater video and the black text may not show clearly at times.

  • #9

    TF (Tuesday, 20 January 2015 17:12)

    I figured out how to change the text color, very easy.

    This color slider is helpful:
    http://www.calculatorcat.com/free_calculators/color_slider/rgb_hex_color_slider.phtml

  • #10

    theeminentcodfish (Wednesday, 21 January 2015 08:38)

    I am happy that you go everything working. Within that same line of code you can also change the position, size, style and thickness of the font. You can see the variables at:

    http://docs.opencv.org/modules/core/doc/drawing_functions.html?highlight=puttext#cv2.putText

  • #11

    TF (Friday, 30 January 2015 13:28)

    Hi again.

    Regarding external data overlay, can you show me how to add a fourth column of data (ie. Lat [3] & Long [4] coordinates) ? Your example overlays depth but not the pressure data column.

  • #12

    theeminentcodfish (Friday, 30 January 2015 14:13)

    First you have to parse the data and put it in an appropriate format. For this script I used a python dictionary. I set the dictionary up so that each entry consisted of the time as the dictionary key and the depth measurement as the corresponding value. See the code below:

    #Importing and parsing of the external data file
    import_file = open(data_name, 'r')
    time_rec = []
    pressure = []
    depth = []
    time_depth = {}

    for lines in import_file.read().splitlines():
    if lines != '':
    data = lines.split()
    time_rec.append(dt.datetime.strptime((data[0] + ' ' + data[1] + '000'), "%d-%b-%Y %H:%M:%S.%f"))
    pressure.append(float(data[2]))
    depth.append(float(data[3]))

    dt_dic = dict(zip(time_rec, depth))

    This allowed me to looked up the depth value at the relevant time during the main logic loop of the program. Once you have the additional data into a dictionary it is simply a matter of looking up the data value at each time step. See below:

    timestamp = initial + dt.timedelta(microseconds = elapsed_time*1000)
    t = timestamp + dt.timedelta(microseconds = -timestamp.microsecond)
    depth_value = dt_dic[t]

    The second line is used to strip out the microseconds value in the timestamp so that it matches identically to the time value in the data file. The third line searches the dictionary for the depth value from the time entry. Then you simply add it to the screen.

    cv2.putText(image, 'Depth: '+ str(depth_value)[0:-3], (50,int(height-50)),cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 255, 255), 3)

    If you want to add a forth line, then just add another one of these lines. Change the second parameter to reference the data you want overlaid (i.e. lat/long). Then change the third parameter to place the text in an appropriate place.

  • #13

    Paralegal Josh (Saturday, 04 April 2015 11:49)

    Hi.

    I may be closer to accomplishing this, but am still not there (shucks).

    When I download the FFprobe, it comes as a .tar.gz file (WinRAR). I extract to my desktop into a folder (ffprobe 53). I have placed that folder into the folder contained on an sdcard which has the gopro mp4 video on which I hope to implement the timestamp.

    I have removed the .txt extension as instructed; and I have opened it in IDLE and edited it. But, when I attempt to run via run>run module (or by pressing F5), I am receiving a "Subprocess Startup Error," which says, "IDLE's subprocess didn't make a connection. Either IDLE can't start a subprocess or personal firewall software is blocking the connection. When I double-click, I see the command module open up briefly, but still no timestamp when I watch the video.

    I am assuming that I ought to extract the FFprobe (WinRAR) download, else it would not be usable. Is that correct?



  • #14

    theeminentcodfish (Monday, 06 April 2015 12:50)

    Hey Josh. The problem that you are having is that python is not recognizing the openCV package that you have on your computer. To make python aware of it's exisitence then you need to go into the following OpenCV folder, \opencv\build\python\2.7\. Then you will find two folders (x64 and x86). If you are running a 32-bit version of python (it will tell you this when you start IDLE) then copy the cv2.pyd file from the x86 folder, if you are running a 64 bit version then copy the same file from the x64 folder. Copy that file into your python folder \Python2.7\Lib\. That should get it to work. I hope you are running a 32-bit version because you may have issues with another package called Numpy which OpenCV relies on and is typically not installed as a 64-bit version. If you have that problem then I can walk you through that.

  • #15

    Paralegal Josh (Monday, 13 April 2015 06:34)

    Hi.

    So, I have followed your previous instructions ("\opencv\build\python\2.7\...find two folders (x64 and x86)...then copy the cv2.pyd file from the x86 folder). Now as I run>run module in IDLE, I get this--"ImportError: numpy.core.multiarray failed to import"

    However, I am running a 32 bit version of windows (windows xp pro service pack 3). Maybe I accidentally installed an incorrect version of python or open cv?

    It seems like the numpy package issue you foresaw. But what confuses me is that I am indeed running a 32bit version of windows.

  • #16

    theeminentcodfish (Monday, 13 April 2015 10:24)

    Josh, Sorry for all the hassle. It makes me think that there should be a better way. I'll work on it, but until then it looks like a numpy issue. If you are running a 32-bit version then you may not have numpy (depending on what python package you downloaded) or have an older version. You can try downloading the most current version at this website:
    http://sourceforge.net/projects/numpy/files/NumPy/
    Hopefully this will work.

    Chris

  • #17

    Paralegal Josh (Sunday, 19 April 2015 19:52)

    Hey Chris,

    So I have uninstalled the previous version of Python (2.7.2). And I installed the NumPy version-1.9.2 from the website you gave me. However, I am not able to locate IDLE within this version of Python for the purpose of running the script. So, I downloaded Anaconda; and I am not sure how to initiate it (Anaconda) in order to run the (gopro timestamp) script. I have the (gopro timestamp) script saved in the folder (as .py) with the .mp4 file that I desire to implement a timestamp upon. Can I run this script using anaconda? What do you think?

    By the way, don't worry about the hassle here, I am just hoping I can get this done. I am looking forward to accomplishing this, and hoping that I will be able to implement the filewalker and process multiple batches of files, which I am thinking will really help me with a project I am working on.

    Thanks,
    Josh

  • #18

    Andrew (Thursday, 14 May 2015 11:39)

    This site has been a great help!

    I have modified your data overlay script to display 5 different parameters and it works great. I'm showing date, time, altitude, latitude and longitude.

    However, Is there are quick and dirty way to pump in a image as well? To watermark the video in a different corner?

  • #19

    theeminentcodfish (Friday, 15 May 2015 11:07)

    Hello Andrew.

    An image overlay is relatively simple. I added another file above the comments section which will work. You can adopt the code into your version. Just make sure that the watermark doesn't go outside the bounds of the video frame or it will throw an error message.

  • #20

    kfinchie (Wednesday, 17 June 2015 14:12)

    Great tutorial! I am having an issue, when I run the script I am getting an error: Error: Could not load video. I have the script in my video folder, any idea on how to fix?

  • #21

    nick (Friday, 19 June 2015 12:53)

    Hey Chris,

    I've been trying to get this code to work and I've been running into some issues. When I run the GoPro_timestamp.py file I get Error: Could not load video. When I run the Playback_test.py file I get the following statement.

    File ".\Playback_test.py", line 5, in <module>
    total_frames = video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
    AttributeError: 'module' object has no atribute 'cv'

    Do you have any idea of what the problem is, or how to fix it? Thank you for this site, and your help.

    ~ Nick

  • #22

    theeminentcodfish (Friday, 19 June 2015 14:40)

    kfinchie,

    This sounds like it could be a codec issue. OpenCV is not great at this.

    I think it is a matter of getting the correct ffmpeg driver into the correct Python folder. First, check which version of OpenCV you are using by typing the following into your python prompt.

    >>> from cv2 import __version__
    >>> __version__
    '2.4.0'

    In the OpenCV folder go to: opencv\sources\3rdparty\ffmpeg. Copy the ffmpeg.dll file into the Python27 folder, you shouldn't need the opencv_ffmpeg_64.dll unless you are running a 64-bit version of python. Rename based on the version number above. One my system I only used three digits (i.e. opencv_ffmpeg240.dll) but another user need four digits. Hopefully this works!

  • #23

    theeminentcodfish (Friday, 19 June 2015 14:43)

    Hey Nick,

    What version of OpenCV are you using? To check you can type the following into the python prompt.

    >>> from cv2 import __version__
    >>> __version__
    '2.4.0'

    Chris

  • #24

    Cinthia (Monday, 22 June 2015 11:00)

    Hi Chris,

    In having trouble with your first timestamp code. I'm getting a syntax error that says " 'break' outside loop".(I'm very new to programing and python.)

    Any advice on this? It would be greatly appreciated!

  • #25

    theeminentcodfish (Monday, 22 June 2015 11:43)

    Cinthia,

    It looks like the python compiler that you are using is interpreting the program slightly differently then mine. Either way, it looks like it doesn't like the 'break' statement in line 85. I updated the code on the website. I think it should work now. If you are using any of the other scripts, you can simply comment out the break statement by placing a # in front of it. Python will ignore that line (which isn't essential) and you shouldn't get an error.

  • #26

    Cinthia (Monday, 22 June 2015 11:52)

    Thanks Chris, I'll have to wait to try the updated code. My company doesn't download it here. I'll update you tomorrow. Thank you for the very quick response!

  • #27

    theeminentcodfish (Monday, 22 June 2015 11:59)

    Cinthia,

    If you already have the old code you can simply put a # in front of the break statement in line 85. That should solve the problem.

  • #28

    Cinthia (Monday, 22 June 2015 12:31)

    Any tips?
    Traceback (most recent call last):
      File "C:\Users\J36836\Desktop\3Video\GoPro_timestamp.py", line 59, in <module>
        t = creation_time(filename)
      File "C:\Users\J36836\Desktop\3Video\GoPro_timestamp.py", line 28, in creation_time
        p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      File "C:\Python27\lib\subprocess.py", line 679, in __init__
        errread, errwrite)
      File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
        startupinfo)
    WindowsError: [Error 2] The system cannot find the file specified

  • #29

    theeminentcodfish (Monday, 22 June 2015 12:35)

    It looks like it cannot find the ffprobe file. Did you download, unzip and place the ffprobe software in the file containing the GoPro video and this script? The link to the software is listed above.

  • #30

    Cinthia (Monday, 22 June 2015 12:52)

    In the folder, I have the unzipped (ffprobe-53) folder, (opencv) folder, the file with the script, the video file in mp4 format and opencv-3.0.0.exe

  • #31

    theeminentcodfish (Monday, 22 June 2015 13:00)

    Make sure the ffprobe.exe file is located in the same folder as the script. Did you install opencv on your system or just place the file in the folder. Make sure that it is installed. Additionally, I have only tested this program with OpenCV 2.4.8. OpenCV 3 is new and I haven't tested it yet. I can't say for sure whether it will work. You can try and find out.

  • #32

    kfinchie (Monday, 22 June 2015 16:25)

    Thank you for your help! I have done the steps you provided but now I am getting the same error as Cinthia:
    Traceback (most recent call last):
    File "C:\Video\GoPro_timestamp.py", line 59, in <module>
    t = creation_time(filename)
    File "C:\Video\GoPro_timestamp.py", line 28, in creation_time
    p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    File "C:\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
    File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
    startupinfo)
    WindowsError: [Error 2] The system cannot find the file specified

    Also a new file is being created in GoPro video directory with the same name as the file changed to .avi that I am trying to timestamp, I feel like i'm close, any suggestions? I have the ffprobe folder that I downloaded from the link provided. I have also tired to rename the folder ffprobe and ffprobe-53, neither of those got me going.

  • #33

    kfinchie (Monday, 22 June 2015 17:05)

    *Update*
    I was able to get past this by downloading FFmpeg and got the ffprobe from that, now I am running into this issue:

    >>>
    GOPR0102.MP4
    ==========output==========


    Traceback (most recent call last):
    File "C:\Video\GoPro_timestamp.py", line 59, in <module>
    t = creation_time(filename)
    File "C:\Video\GoPro_timestamp.py", line 37, in creation_time
    time = str(t[14][18:37])
    IndexError: list index out of range
    >>>

    Again thanks for this article!

  • #34

    nick (Tuesday, 23 June 2015 13:52)

    Chris,

    Sorry it took me a while to respond.
    I typed in these lines because the line: from cv2 import__version__ wasn't working for me so I did it this way.

    >>> import cv2
    >>> print cv2.__version__
    3.0.0

  • #35

    Cinthia (Tuesday, 23 June 2015 14:31)

    Hey Chris! So I downloaded an older version, and it AAALMOST works. It uploads the video, and processes it. (I know because I ask it to spit it out) and the time stamp is on there! Wooo! Unfortunately, it's not saving properly.

    This is the error I get:OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in unknown function, file ..\..\..\OpenCV-2.3.1\modules\core\src\array.cpp, line 2482
    Traceback (most recent call last):
      File "C:\Users\J36836\Desktop\3Video\GoPro_timestamp.py", line 83, in <module>
        cv2.imshow('Video', image)
    cv2.error: ..\..\..\OpenCV-2.3.1\modules\core\src\array.cpp:2482: error: (-206) Unrecognized or unsupported array type


    Thanks in advance!

  • #36

    theeminentcodfish (Tuesday, 23 June 2015 20:53)

    Nick,

    It looks like you are using the OpenCV 3. This is a new version which I have not tested yet. This may be the issue. I have only tested this program on OpenCV 2.4.8.

    Chris

  • #37

    Cinthia (Thursday, 25 June 2015 09:11)

    Hey Chris, I was able to get the code to work!

    Thanks for the help.

    Any chance you can update the filewalker code?

    P.S. if you're ever in SoCal,drinks are on me!

  • #38

    theeminentcodfish (Thursday, 25 June 2015 20:18)

    Hey Cinthia,

    I am glad that it finally worked. I apologize that the software is a bit buggy. It is something I definitely need to work on. It's part of my learning curve.

    I updated the filewalker script, so it should work.

    How did you end of fixing the last issue that you had? Out of curiosity.

    Chris

  • #39

    theeminentcodfish (Thursday, 25 June 2015 20:21)

    Kfinchie,

    It looks like the program is not talking to ffprobe and getting the correct information back. Can you check that the ffprobe.exe file is in the same file with the script?

  • #40

    Cinthia (Friday, 26 June 2015 08:36)

    Hey Chris,

    I re-ran OpenCV and then copied and pasted your code again and voila!

    Also, for your filewalker code, I keep getting 'import sitecustomize' failed. Any ideas?

  • #41

    theeminentcodfish (Friday, 26 June 2015 09:24)

    Cinthia,

    What software are you using to run python? A quick google search showed that this happens with Spyder. Are you using this python interpreter?

    Chris

  • #42

    Cinthia (Friday, 26 June 2015 09:50)

    Yes, I am. That might be the problem. Which one are you using?

    (Sorry, I'm a Matlab person)

  • #43

    Zimmer (Wednesday, 08 July 2015 21:13)

    So, I have followed your previous instructions ("\opencv\build\python\2.7\...find two folders (x64 and x86)...then copy the cv2.pyd file from the x86 folder). Now as I run>run module in IDLE, I get this--"ImportError: numpy.core.multiarray failed to import"

    I have downloaded the most current version at this website:
    http://sourceforge.net/projects/numpy/files/NumPy/
    (Version 1.9.2)

    Can you please walk me through the next steps.

  • #44

    Srini (Monday, 20 July 2015 17:57)

    Is there a ffprobe for Ubuntu? I don't work in Windows, I work in Ubuntu 14.04.

  • #45

    Anthony (Wednesday, 19 August 2015 12:35)

    Hello Chris, thanks for such a great post. I am unable to get this working and was hoping you could help. I am getting the "Could not load video" error.

    I am on Windows 7 64bit, running Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32, OpenCV version 2.4.9 (64bit). I saw that someone ran a "Playback_test.py" file to help troubleshoot, but I am not familiar with Python or where to find that file (My Google-Fu is not strong today).

    What other information do you need in order to assist me?

  • #46

    Anthony (Wednesday, 19 August 2015 12:48)

    Please disregard my last post. I was able to fix my issue: "On my system I only used three digits (i.e. opencv_ffmpeg240.dll) but another user need four digits."

    I also had to use three digits.

    Now I am getting a different error:
    >>>
    test.MP4
    ==========output==========
    [FORMAT]

    filename=test.MP4

    nb_streams=2

    nb_programs=0

    format_name=mov,mp4,m4a,3gp,3g2,mj2

    format_long_name=QuickTime / MOV

    start_time=0:00:-0.021333

    duration=0:01:49.184000

    size=118.055408 Mibyte

    bit_rate=9.070198 Mbit/s

    probe_score=100

    TAG:major_brand=isom

    TAG:minor_version=512

    TAG:compatible_brands=isomiso2avc1mp41

    TAG:encoder=Lavf55.33.100

    [/FORMAT]



    Traceback (most recent call last):
    File "C:\Users\ajm\Desktop\Python\grow.py", line 60, in <module>
    initial = dt.datetime.strptime(t, "%Y-%m-%d %H:%M:%S")
    File "C:\Python27\lib\_strptime.py", line 325, in _strptime
    (data_string, format))
    ValueError: time data '.33.100' does not match format '%Y-%m-%d %H:%M:%S'

  • #47

    Anthony (Wednesday, 19 August 2015 13:38)

    It seems as though this is not going to work with my videos. I use the TimeLapse feature and take one picture every 60 seconds; then I compile those pictures into a video using the GoPro Studio. The final file type is .MP4, but this script doesn't like the time stamp structure and I'm not sure what I can do about it...
    Traceback (most recent call last):
    File "C:\Users\ajm\Desktop\Python\grow.py", line 60, in <module>
    initial = dt.datetime.strptime(t, "%Y-%m-%d %H:%M:%S")
    File "C:\Python27\lib\_strptime.py", line 325, in _strptime
    (data_string, format))
    ValueError: time data '.33.100' does not match format '%Y-%m-%d %H:%M:%S'

  • #48

    Veronica (Sunday, 20 December 2015 22:19)

    I understand that this script simply produces a timestamp based on the initial time stored in the video meta-data and an assumption on a fixed frame rate. But how do you know if this assumption hold? That is, there could be fluctuations in the frame rate (25fps, 24.9fps, etc...) which could result in a drift overtime. Did you test this by any chance?

    Thanks!

  • #49

    Kyle (Wednesday, 06 January 2016 12:23)

    Very helpful, thanks. One common use for GoPros where timestamp overlay is helpful is for timelapse videos. In this case the video time elapsed does not equate to the time of day, so the "elapsed_time = video.get(cv2.cv.CV_CAP_PROP_POS_MSEC)" line needs to be modified to adjust for the timelapse factor. Presumably anyone technical enough to use this script can figure it out for themselves and adjust this line manually, but just thought I'd point it out if you are continuing to update and improve this helpful script.

  • #50

    Nate (Tuesday, 19 January 2016 17:16)

    Tried running everything but get the following error:

    " SyntaxError: Missing parentheses in call to 'print' "

    Can you let me know any additional advice?

    Thanks.

  • #51

    Chris (Wednesday, 20 January 2016 13:38)

    Kyle,

    You make a good point. If you were using this for timelapse, and you know the time between frames then you could simply alter the script. I can think of two ways to do this. First you could just increment the timestamp variable every frame. Here is an example for a frame rate of 1/5 sec.

    #line 76 in the main script
    timestamp = timestamp + dt.timedelta(seconds = 5)

    Or you could look at the number of elapsed frames (obtained in line 75) and extrapolate. Like so:

    timestamp = current_frame + dt.timedelta(seconds = 5)

    Both should work. I'm not sure if one method is better then the other.

  • #52

    Chris (Wednesday, 20 January 2016 13:45)

    Hey Nate,

    Can you give me a little more information? Which script are you using? Does is give any more info in the error message? I'd be looking for a line number where it through the error.

    The error message is saying that there was an error in a print statement. Typically when you tell python to print something you usually need to put it in quotes, this tells python that it is a text string. I don't see anything that has an open quote but not a closing quote. Double check you filename statement.

    filename = 'Your_GoPro_Video_Here.MP4'

    Chris

  • #53

    Chris (Wednesday, 20 January 2016 13:56)

    Hello Veronica,

    Sorry for the delay in my feedback. To the best of my knowledge OpenCV is taking the frame rate into consideration when calculating the elapsed time. For instance, you can investigate the frame rate with the following line:

    FPS = video.get(cv2.cv.CV_CAP_PROP_FPS)

    So varying frame rates shouldn't affect it, assuming that OpenCV can derive the correct frame rate. I haven't tried different frame rates. I have looked into the issue of drift. Based on some very basic analysis it doesn't look like there is that much drift (i.e. error) in the timestamp within a video file (less then 1 second, which was good enough for what I needed). I did notice that there was some drift when doing sequential videos (i.e. the gopro saved a long video every ~30 minutes). It looks like there is some error imparted when jumping from one video to another (~a couple seconds). If you have a long string of videos this could add up.

    Chris

  • #54

    Nate (Thursday, 11 February 2016 16:44)

    Chris,

    File "c: \ temp\GoPro_timestamp.py" , line 29
    print filename
    ^
    SyntaxError: Missing parentheses in call to 'print'

    Any help would be appreciated.

    Thanks.

  • #55

    Chris (Friday, 19 February 2016 07:59)

    Hey Nate,

    What version of python are you using? My guess is that you are using python 3.0. This script was written in 2.7.

  • #56

    Davis (Wednesday, 24 February 2016 15:12)

    Would this also work for timelapse?

  • #57

    Francisco (Tuesday, 22 March 2016 04:01)

    Thanks a lot for the programs !!!

    I had the error: "Cannot open video" but finally was able to use it following :

    http://stackoverflow.com/questions/11699298/opencv-2-4-videocapture-not-working-on-windows

  • #58

    Blair (Tuesday, 17 May 2016 22:12)

    This may be a big ask, but I am really not a programmer, is there any chance of very step by step guide e.g. including how to open the files in python etc?

    Thanks

  • #59

    Blair (Tuesday, 17 May 2016 23:07)

    Cancel above entry (I think). I am getting an erroer "Could not load video", and while I tried to follow another solution you have posted, the file in my ffmpeg folder is ffmpeg.cmake not ffmpeg.dll

    Thanks

  • #60

    Ritchi (Friday, 20 May 2016 02:40)

    Hi,
    I would like to know if your script works on Mac OS X ?
    Regards

  • #61

    MG (Tuesday, 09 August 2016 07:49)

    Hi there,
    I notice you mention this is tested on GoPro 2/3/3+; has it been modified to work on GoPro 4?
    Thanks

  • #62

    CharlesM (Wednesday, 23 November 2016 18:27)

    I have spent hours getting Python and Numpy and opencv to finally allow execution of the GoPro timestamp script. I have an error in the formatting of either the information provided in the metadata of the mp4, the formatting of the script line 60 dt.datetime.strptime or the formatting of the python library module _strptime.py, line 332. The actual error follows:


    C:\GoPro_TS>GoPro_timestamp.py
    WestEmersonTimeLapse.mp4
    ==========output==========
    [FORMAT]
    filename=WestEmersonTimeLapse.mp4
    nb_streams=2
    nb_programs=0
    format_name=mov,mp4,m4a,3gp,3g2,mj2
    format_long_name=QuickTime / MOV
    start_time=0:00:-0.021333
    duration=0:00:33.344000
    size=89.961450 Mibyte
    bit_rate=22.632297 Mbit/s
    probe_score=100
    TAG:major_brand=isom
    TAG:minor_version=512
    TAG:compatible_brands=isomiso2avc1mp41
    TAG:encoder=GoPro
    [/FORMAT]

    Traceback (most recent call last):
    File "C:\GoPro_TS\GoPro_timestamp.py", line 60, in <module>
    initial = dt.datetime.strptime(t, "%Y-%m-%d %H:%M:%S")
    File "C:\Python27\lib\_strptime.py", line 332, in _strptime
    (data_string, format))
    ValueError: time data '' does not match format '%Y-%m-%d %H:%M:%S'

    Any idea on what I should do to fix this. I am so close and excited to get this to work. Thanks in advance for any help.

  • #63

    Bill (Monday, 20 March 2017 17:42)

    Is anyone still actively working on this script project? I'm working with the timestamp script, and it's producing an AVI file bit it is only 116 bytes compared to the original 1.3GB GoPro video, and the AVI file will not open. There are a number of specific errors in the execution related to ParseTrackAtom & FigAssetWriterCreateWithURL. So far unable to find much helpful information. As much time as it may take to get this worked out, it may be easier to buy a device that has a time stamp function built into it. Still going to give it some effort though.

  • #64

    Bill (Tuesday, 21 March 2017 09:29)

    UPDATE: For anyone who may be looking into this, I successfully used this process to extract timestamp data from an MP4 file created on a GoPro Hero5 Session, and burn it onto the generated AVI file. I used a Windows 7 Pro 64 bit system running Python 2.7, OpenCV 2.4.11 & Numpy 1.7.1.

    The generated AVI file, without audio, was 75% larger in size than the original MP4 file with audio (1.75GB vs 1GB).

    Not a very practical process, however I'm $400 into the GoPro ( incl. $100 128GB SD Card) and have video made with it for which I need the time stamp. I was unaware when I bought it that GoPro products do not possess this basic feature. I will be migrating to another manufacturer asap.

  • #65

    Conor (Wednesday, 22 March 2017 10:08)

    Hi Bill,

    I am trying to get the original code to work for the GoPro Hero 5 Session as well, but am running into errors. Did you modify the code at all to fit the GoPro 5?

    Thanks,

    Conor

  • #66

    Chris (Monday, 27 March 2017 13:54)

    Bill,

    Thank you for the post. I agree that it is not a great process. I assumed that the GoPro's would have this functionality. I had to create this work around as a post-hoc method for timestamping using the equipment that I had. I have recently uploaded a newer script which will reduce the file size and maintain the MP4 codec.

    Chris

  • #67

    Chris (Monday, 27 March 2017 13:54)

    Conor,

    Can you be more specific as to the errors that you are getting?

    Chris

  • #68

    Conor (Thursday, 06 April 2017 15:33)

    Hi Chris,

    Thank you for putting this code together. Unfortunately, I'm a newcomer to programming in python, so i'm already at a disadvantage with trying to get the code to work for a GoPro 5 I have.

    After fixing a series of syntax errors python called, I run into the error
    "No module named cv2"

    Any help would be greatly appreciated! Thanks! Conor

  • #69

    Chris (Monday, 10 April 2017 11:57)

    Hey Conor,

    The error message that you are getting indicates that python is now recognizing the opencv module. If you downloaded opencv and have it on your computer then you need to copy the cv2.pyd file, usually found in the following directory C:\OpenCV_3.0.0\opencv\build\python\2.7\x64\ to your python Lib folder, found at C:\Python_2.7\Lib. The exact location may be slightly different depending on your system. Additionally, make sure you take the appropriate pyd file (either x86 or x64) depending on your system (32 vs 64-bit). Hopefully this helps.

    Chris

  • #70

    Conor (Monday, 22 May 2017 10:58)

    Thanks Chris,

    I'm using Python 2.7, Open CV 3.0, and Numpy 1.8. I have a 64 bit system and grabbed the corresponding CV file and placed in in the Lib folder.

    Using your updated Script, I am now running into the error of:
    Traceback (most recent call last):
    File "C:\Users\conor.egan\AppData\Local\Temp\Temp1_GoPro_timestamp_FFMPEG_3.0 (2).zip\GoPro_timestamp_FFMPEG_3.0.py", line 12, in <module>
    import cv2, sys, time
    ImportError: numpy.core.multiarray failed to import

    Going to play around a bit with different versions of Open CV and Numpy.

  • #71

    Conor (Monday, 22 May 2017 11:30)

    added import numpy.core.multiarray before import cv2 and ran into
    AttributeError: 'module' object has no attribute 'may_share_memory'

  • #72

    Antoine (Thursday, 01 June 2017 14:32)

    Regarding external data overlay, I have data sampled at a rate of 20 kHz that I'd like to overlay and if I strip the decimals (5 decimals) off the seconds I end up with an awful lot of measurements taken at the same time. What should I change in the code to make it work (without losing data)?

  • #73

    Antoine (Friday, 09 June 2017 14:08)

    I'm getting this error:

    Traceback (most recent call last):
    File "U:\GoPro Timestamp\Python\GoPro_timestamp_FFMPEG_3.0.py", line 92, in <module>
    pipe.stdin.write(image.tostring())
    AttributeError: 'NoneType' object has no attribute 'tostring'

  • #74

    Jess (Tuesday, 28 November 2017 23:27)

    Hi Chris,

    Nice bit of code! I'm getting the same error as Cynthia:
    Traceback (most recent call last):
    File "/Users/jessflicker/Documents/GoPro Timestamp/GoPro_timestamp_watermark.py", line 65, in <module>
    t = creation_time(filename)
    File "/Users/jessflicker/Documents/GoPro Timestamp/GoPro_timestamp_watermark.py", line 34, in creation_time
    p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1333, in _execute_child
    raise child_exception_type(errno_num, err_msg)
    FileNotFoundError: [Errno 2] No such file or directory: 'ffprobe'

    What are the steps to remediate this?
    Cheers

  • #75

    Jess (Tuesday, 28 November 2017 23:29)

    *Cinthia, sorry. I should mention that I'm using MAC OS X

  • #76

    Chris (Wednesday, 29 November 2017 09:24)

    Hello Jess,

    Based on the error code, it looks as if the script cannot find ffprobe. Ensure that you have downloaded ffprobe file, either from the link above or from the ffmpeg website. If you download it above, unzip the file. It should be an executable (*.exe). Then put that file in the same folder as the script.

    Hopefully that works.
    Chris

  • #77

    Jess (Thursday, 30 November 2017 00:07)

    Thanks for your quick reply, Chris. I'm fairly new to programming. I have the ffprobe file in there with the script and the .mp4 video file. While the ffprobe doesn't have a .exe extension, it does have a black icon with green 'exec' and opens in Terminal so I think it is the right file. When I run the script I get a zero byte copy of the video file with a .avi extension. Has anybody else had a similar experience on Mac?
    Cheers

  • #78

    Jess (Thursday, 30 November 2017 00:48)

    So I tried something else and now I'm getting this error code:

    Traceback (most recent call last):
    File "/Users/****/Documents/GoProTimestamp/GoPro_timestamper_w_filewalker_external_data.py", line 82, in <module>
    t = creation_time(video_filename)
    File "/Users/****/Documents/GoProTimestamp/GoPro_timestamper_w_filewalker_external_data.py", line 22, in creation_time
    p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1333, in _execute_child
    raise child_exception_type(errno_num, err_msg)
    PermissionError: [Errno 13] Permission denied

    I don't really understand what's going on here. What do I need to give permission to and how can I do it on a Mac?

  • #79

    Chris (Friday, 01 December 2017 13:47)

    Hey Jess,
    Try the solution on this website.

    https://stackoverflow.com/questions/2066068/what-permissions-are-required-for-subprocess-popen

    I hope this works.
    Chris

  • #80

    Jess (Sunday, 03 December 2017 00:38)

    Brilliant, Chris.
    Another step closer! However, now I'm getting the same error as kfinchie.
    Traceback (most recent call last):
    File "/Users/****/GoPro_timestamp_watermark.py", line 65, in <module>
    t = creation_time(filename)
    File "/Users/****/GoPro_timestamp_watermark.py", line 43, in creation_time
    time = str(t[14][18:37])
    IndexError: list index out of range
    You said this might be because there's something wrong with the communication with ffprobe. Any ideas on how to fix this?

  • #81

    Chris (Monday, 04 December 2017 10:07)

    Hey Jess,

    What version of python are you using? Which GoPro camera are you using? It looks as if there is an problem with FFprobe reading the video meta-data. Let's see what FFprobe is spitting out. Within the script, place the following lines of code in the creation_time function just after t = out.splitlines():

    print(t)

    This will show us the raw FFprobe metadata. Your error is python having problems parsing information which is either not there or not in the format which it expects. Try this and see what it says.

    Chris

Download
Playback_test.zip
Compressed Archive in ZIP Format 486 Bytes