Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Reference: https://www.bluebill.net/timestamps.html


Table of Contents

Fixing GoPro Video Creation Time

Install Pre-Requisits

Install ffmpeg, coreutils and jq.Check Creation Time

Code Block
./correctTime.sh DIVE_2_1_GH010089.MP4 
DIVE_2_1_GH010089.MP4

Video Create Time (Zulu):  2023-06-13T19:12:11.000000Z
Video Create Time (local): 2023-06-13T15:12:11 -0400 (EDT)
--------
Video Create Time (without timezone):2023-06-13T19:12:11
Video Create Time (to local):2023-06-13T19:12:11 -0400 (EDT)
--------
Video Create Time (to UTC):2023-06-13T23:12:11.000000Z

To update the metadata use --update

In the above output we see that the local time was recorded as Zulu time.  The local time was 19:12:11.

Fix Creation Time

Code Block
./correctTime.sh DIVE_2_1_GH010089.MP4 --update

Verify Creation Time

Code Block
./correctTime.sh DIVE_2_1_GH010089.corrected.MP4 
DIVE_2_1_GH010089.corrected.MP4

Video Create Time (Zulu):  2023-06-13T23:12:11.000000Z
Video Create Time (local): 2023-06-13T19:12:11 -0400 (EDT)
--------
Video Create Time (without timezone):2023-06-13T23:12:11
Video Create Time (to local):2023-06-13T23:12:11 -0400 (EDT)
--------
Video Create Time (to UTC):2023-06-14T03:12:11.000000Z

To update the metadata use --update

Add Timestamp to Video

Code Block
./timestamp.sh DIVE_2_1_GH010089.corrected.MP4

Changes to the timestamp.sh script:

  • Removed -vcodec libx265
  • Changed location of timestamp to top right corner
Code Block
...
#ffmpeg -i "${INPUT}" -vf drawtext="fontsize=30:fontcolor=yellow:text='%{pts\:localtime\:${EPOCH}}':x=(w-text_w) - 10:y=(h-text_h) - 10" -vcodec libx265 -crf 28 "${OUTPUT}"

ffmpeg -i "${INPUT}" -vf drawtext="fontsize=30:fontcolor=white:text='%{pts\:localtime\:${EPOCH}}':x=(w-text_w) - 10:y=10" -crf 28 "${OUTPUT}"
...

Joining Video Files

Code Block
echo file DIVE_2_1_GH010089.corrected.MP4 >  mylist.txt 
echo file DIVE_2_2_GH020089.corrected.MP4 >>  mylist.txt 

ffmpeg -f concat -i mylist.txt -c copy output.mp4

Get Video File Information

Code Block
ffprobe -loglevel 0 -print_format json -show_format -show_streams "${INPUT}" | jq -r .format.tags.creation_time
brew install ffmpeg
brew install coreutils
brew install jq


Joining the Videos While Preserving Metadata

First create a text file (e.g. filesToMerge.txt) with all the chaptered video files that you want to merge into a single video e.g.

Code Block
file GH010097.MP4
file GH020097.MP4
file GH030097.MP4
file GH040097.MP4 


Next, check the streams in the video files.

Code Block
themeEmacs
ffprobe -show_format GH010097.MP4
Code Block
  Stream #0:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, bt709, progressive), 2704x1520 [SAR 1:1 DAR 169:95], 60002 kb/s, 59.94 fps, 59.94 tbr, 60k tbn (default)
    Metadata:
      creation_time   : 2023-08-09T17:39:29.000000Z
      handler_name    : GoPro AVC  
      vendor_id       : [0][0][0][0]
      encoder         : GoPro AVC encoder
      timecode        : 17:38:25:34
  Stream #0:1[0x2](eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 189 kb/s (default)
    Metadata:
      creation_time   : 2023-08-09T17:39:29.000000Z
      handler_name    : GoPro AAC  
      vendor_id       : [0][0][0][0]
      timecode        : 17:38:25:34
  Stream #0:2[0x3](eng): Data: none (tmcd / 0x64636D74) (default)
    Metadata:
      creation_time   : 2023-08-09T17:39:29.000000Z
      handler_name    : GoPro TCD  
      timecode        : 17:38:25:34
  Stream #0:3[0x4](eng): Data: bin_data (gpmd / 0x646D7067), 49 kb/s (default)
    Metadata:
      creation_time   : 2023-08-09T17:39:29.000000Z
      handler_name    : GoPro MET  
  Stream #0:4[0x5](eng): Data: none (fdsc / 0x63736466), 13 kb/s (default)
    Metadata:
      creation_time   : 2023-08-09T17:39:29.000000Z
      handler_name    : GoPro SOS 


The above video has 4 streams. Lets try to join streams 0,1 and 3.

Code Block
themeEmacs
ffmpeg -f concat -safe 0 -i filesToMerge.txt -c copy -map 0:0 -map 0:1 -map 0:3 -c:v libx264 -pix_fmt yuv420p video-merged.mp4


Revise Scripts

Using the scripts from https://www.bluebill.net/timestamps.html

Revise timestamp.sh and correctTime.sh to use 'gdate' instead of 'date'.  

See below for revised scripts. 


Fixing GoPro Video Creation Time


Check Creation Time

Code Block
./correctTime.sh DIVE_2_1_GH010089.MP4 
DIVE_2_1_GH010089.MP4

Video Create Time (Zulu):  2023-06-13T19:12:11.000000Z
Video Create Time (local): 2023-06-13T15:12:11 -0400 (EDT)
--------
Video Create Time (without timezone):2023-06-13T19:12:11
Video Create Time (to local):2023-06-13T19:12:11 -0400 (EDT)
--------
Video Create Time (to UTC):2023-06-13T23:12:11.000000Z

To update the metadata use --update

In the above output we see that the local time was recorded as Zulu time.  The local time was 19:12:11.


Fix Creation Time

Code Block
./correctTime.sh DIVE_2_1_GH010089.MP4 --update


Verify Creation Time

Code Block
./correctTime.sh DIVE_2_1_GH010089.corrected.MP4 
DIVE_2_1_GH010089.corrected.MP4

Video Create Time (Zulu):  2023-06-13T23:12:11.000000Z
Video Create Time (local): 2023-06-13T19:12:11 -0400 (EDT)
--------
Video Create Time (without timezone):2023-06-13T23:12:11
Video Create Time (to local):2023-06-13T23:12:11 -0400 (EDT)
--------
Video Create Time (to UTC):2023-06-14T03:12:11.000000Z

To update the metadata use --update


Command to Update Creation Time:

Code Block
ffmpeg -I <MOVIE> -c copy -metadata creation_time=2023-06-13-23:12:11.000000Z <OUTPUT>


Add Timestamp to Video

Code Block
./timestamp.sh DIVE_2_1_GH010089.corrected.MP4


Changes to the timestamp.sh script:

  • Removed -vcodec libx265
  • Changed location of timestamp to top right corner


Code Block
...
#ffmpeg -i "${INPUT}" -vf drawtext="fontsize=30:fontcolor=yellow:text='%{pts\:localtime\:${EPOCH}}':x=(w-text_w) - 10:y=(h-text_h) - 10" -vcodec libx265 -crf 28 "${OUTPUT}"

ffmpeg -i "${INPUT}" -vf drawtext="fontsize=30:fontcolor=white:text='%{pts\:localtime\:${EPOCH}}':x=(w-text_w) - 10:y=10" -crf 28 "${OUTPUT}"
...


Get Video File Information

Code Block
ffprobe -loglevel 0 -print_format json -show_format -show_streams <VIDEO_FILE> 
Code Block
{
    "streams": [
        {
Code Block
{
    "streams": [
        {
            "index": 0,
            "codec_name": "aac",
            "codec_long_name": "AAC (Advanced Audio Coding)",
            "profile": "LC",
            "codec_type": "audio",
            "codec_time_base": "1/48000",
            "codec_tag_string": "mp4a",
            "codec_tag": "0x6134706d",
            "sample_fmt": "fltp",
            "sample_rate": "48000",
            "channels": 2,
            "channel_layout": "stereo",
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/48000",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 35213578,
            "duration": "733.616208",
            "bit_rate": "119736",
            "max_bit_rate": "128000",
            "nb_frames": "34391",
            "disposition": {
                "defaultindex": 10,
                "dub"codec_name": 0"aac",
                "original"codec_long_name": 0,
    "AAC (Advanced Audio Coding)",
            "commentprofile": 0"LC",
                "lyricscodec_type": 0"audio",
                "karaoke"codec_time_base": 0"1/48000",
                "forcedcodec_tag_string": 0"mp4a",
                "hearingcodec_impairedtag": 0"0x6134706d",
                "visual_impaired"sample_fmt": 0"fltp",
                "cleansample_effectsrate": 0"48000",
                "attached_picchannels": 02,
                "timedchannel_thumbnailslayout": 0"stereo",
            } "bits_per_sample": 0,
            "tagsr_frame_rate": {"0/0",
                "creationavg_timeframe_rate": "2023-07-16T15:22:49.000000Z0/0",
                "language"time_base": "eng1/48000",
                "handlerstart_namepts": "Core0,
 Media Audio"
            }"start_time": "0.000000",
        },
        {"duration_ts": 35213578,
            "indexduration": 1"733.616208",
            "codecbit_namerate": "h264119736",
            "codecmax_longbit_namerate": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10128000",
            "profilenb_frames": "High34391",
            "codec_typedisposition": "video",{
            "codec_time_base    "default": "1001/120000"1,
            "codec_tag_string    "dub": "avc1"0,
             "codec_tag   "original": "0x31637661"0,
                "widthcomment": 12800,
                "heightlyrics": 7200,
                "coded_widthkaraoke": 12800,
                "coded_heightforced": 7200,
                "hashearing_b_framesimpaired": 0,
                "samplevisual_aspect_ratioimpaired": "1:1"0,
                "displayclean_aspect_ratioeffects": "16:9"0,
                "pixattached_fmtpic": "yuv420p"0,
            "level    "timed_thumbnails": 32,0
            "color_range": "tv"},
            "color_spacetags": "bt709",{
                "colorcreation_transfertime": "bt709"2023-07-16T15:22:49.000000Z",
                "color_primarieslanguage": "bt709eng",
                "chromahandler_locationname": "leftCore Media Audio",
            "field_order": "progressive",
}
        },
       "refs": 1, {
            "is_avcindex": "true"1,
            "nalcodec_length_sizename": "4h264",
            "rcodec_framelong_ratename": "60000/1001H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
            "avg_frame_rateprofile": "60000/1001High",
            "timecodec_basetype": "1/60000video",
            "startcodec_time_ptsbase": 0"1001/120000",
            "startcodec_tag_timestring": "0.000000avc1",
            "durationcodec_tstag": 44016973"0x31637661",
            "durationwidth": "733.616217"1280,
            "bit_rateheight": "8069838"720,
            "bits_per_raw_samplecoded_width": "8"1280,
            "nbcoded_framesheight": "43973"720,
            "dispositionhas_b_frames": {0,
                "default"sample_aspect_ratio": "1:1",
                "dub"display_aspect_ratio": 0"16:9",
                "original"pix_fmt": 0"yuv420p",
                "commentlevel": 032,
                "lyrics"color_range": 0"tv",
                "karaoke"color_space": 0"bt709",
                "forcedcolor_transfer": 0"bt709",
                "hearingcolor_impairedprimaries": 0"bt709",
                "visualchroma_impairedlocation": 0"left",
                "clean_effects"field_order": 0"progressive",
                "attached_picrefs": 01,
                "timedis_thumbnailsavc": 0"true",
            } "nal_length_size": "4",
            "tagsr_frame_rate": {
    "60000/1001",
            "creationavg_frame_timerate": "2023-07-16T15:22:49.000000Z",
  60000/1001",
              "languagetime_base": "und1/60000",
                "handlerstart_namepts": "Core0,
 Media Video"
            }"start_time": "0.000000",
        }
    ]"duration_ts": 44016973,
    "format": {
        "filenameduration": "Dive2733.mp4616217",
         "nb_streams   "bit_rate": 2"8069838",
        "nb_programs": 0,
        "format_namebits_per_raw_sample": "mov,mp4,m4a,3gp,3g2,mj28",
            "formatnb_long_nameframes": "QuickTime / MOV43973",
            "start_timedisposition": "0.000000",{
        "duration        "default": "733.616217"1,
        "size": "751679577",
        "bit_ratedub": "8196978"0,
        "probe_score": 100,
        "tagsoriginal": {0,
                "major_brandcomment": "mp42"0,
                "minor_versionlyrics": "1"0,
            "compatible_brands": "isommp41mp42"    "karaoke": 0,
                "forced": 0,
                "creationhearing_timeimpaired": 0,
             "2023-07-16T15:22:49.000000Z"   "visual_impaired": 0,
        }
       }
}

Scripts

Code Block
languagebash
titletimestamp.sh
collapsetrue
#!/usr/bin/env bash

# -----------
# SPDX-License-Identifier: MIT
# Copyright (c) 2022 Troy Williams

# uuid "clean_effects": 0,
               = 70204dea-f8b9-11ec-a408-cbfc46763958
# author "attached_pic": 0,
       = Troy Williams
# email      = troy.williams@bluebill.net
# date"timed_thumbnails": 0
          = 2022-06-30
# -----------

# this script will add timestamps (like security cameras) to the bottom left
# corner of the video. It will use the creation date as the starting point of
# the timestamp counter. The output video will be re-encoded to h265 format.

# NOTE: Some videos indicate they are in UTC, but in fact are not.They will need
# be correct otherwise the timestamp will be incorrect.

# NOTE: If the creation_time isn't set, the video will be skipped.

# Usage:

# $ ./timestamp.sh 20210613_191928.mp4

# loop through files in a folder:
# $ for FILE in ./2022-06-28/*; do ./timestamp.sh "${FILE}"; done
# $ for FILE in ./2022-06-28/*; do ./timestamp.sh "${FILE}" --over-write; done

# Requirements:

# JSON parser: $ sudo apt install jq

# References

# https://www.cyberciti.biz/faq/unix-linux-bsd-appleosx-bash-assign-variable-command-output/
# https://stackoverflow.com/questions/1955505/parsing-json-with-unix-tools
# https://www.baeldung.com/linux/use-command-line-arguments-in-bash-script
# https://stackoverflow.com/a/965069
# https://www.howtogeek.com/529219/how-to-parse-json-files-on-the-linux-command-line-with-jq/
# https://cameronnokes.com/blog/working-with-json-in-bash-using-jq/
# https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash

# https://ourcodeworld.com/articles/read/1484/how-to-get-the-information-and-metadata-of-a-media-file-audio-or-video-in-json-format-with-ffprobe
# ffprobe -loglevel 0 -print_format json -show_format -show_streams GX013438.MP4

# ----------------
# Set the script to halt on errors
set -e

if [[ -z "$1" ]]; then
    echo "USAGE: $ 1./timestamp.sh video.mp4"
    exit 1
fi

# Get the movie name
# MOVIE=${1}

# What is the full path of the movie name?
INPUT=$(readlink -f "$1")

# Strip longest match of */ from start
filename="${INPUT##*/}"

# Substring from 0 thru pos of filename
dir="${INPUT:0:${#INPUT} - ${#filename}}"

# Strip shortest match of . plus at least one non-dot char from end
base="${filename%.[^.]*}"

# Substring from len of base thru end
ext="${filename:${#base} + 1}"

# If we have an extension and no base, it's really the base
if [[ -z "$base" && -n "$ext" ]]; then
    base=".$ext"
    ext=""
fi

# Create the new file name
OUTPUT="${dir}"/"${base}".stamped."${ext}"

# ----------
# Debugging Code
# echo "MOVIE = ${MOVIE}"
# echo "INPUT = ${INPUT}"
# echo "filename = ${filename}"
# echo "dir = ${dir}"
# echo "base = ${base}"
# echo "ext = ${ext}"
# echo "OUTPUT = ${OUTPUT}"
# ----------

# need to use raw mode `-r` in jq call to return a raw string otherwise it will
# be trouble to deal with
STARTDATE=$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "${INPUT}" | jq -r .format.tags.creation_time)

if [[ -z "${STARTDATE}" ]]; then
    echo "ERROR - No creation_time in metadata! ${INPUT}"
    exit 1
fi

# Convert the date to EPOCH. This will be used to set the time for the draw text
# method.
EPOCH=$(date --date="${STARTDATE}" +%s)

echo "Video Create Time: ${STARTDATE} (${EPOCH})"

# we assume that the STARTDATE is in UTC 0000, Zulu time, GMT and that we want
# to convert it to the local time on the computer.
ffmpeg -i "${INPUT}" -vf drawtext="fontsize=30:fontcolor=yellow:text='%{pts\:localtime\:${EPOCH}}':x=(w-text_w) - 10:y=(h-text_h) - 10" -vcodec libx265 -crf 28 "${OUTPUT}"

if [ "${3:-"invalid"}" == "--over-write" ]; then

    echo "Moving ${OUTPUT} -> ${INPUT}"
    mv "${OUTPUT}" "${INPUT}"

else
    echo "To overwrite the existing video, use --over-write"
fi
Code Block
languagebash
titlecorrect_times.sh
collapsetrue
#!/usr/bin/env bash

# -----------
# SPDX-License-Identifier: MIT
# Copyright (c) 2022 Troy Williams

# uuid       = e577e81c-f938-11ec-9c1a-0d95d112ee30
# author     = Troy Williams
# email      = troy.williams@bluebill.net
# date       = 2022-07-01
# -----------

# This script will take the video and correct the creation time issue with it.
# It assumes that the the creation time did not have the proper timezone set.
# This means that other programs will attempt to convert from GMT/Zulu time and
# get the wrong time stamp.

# This script assumes that the video has the correct local time, but was
# inserted without a timezone. It will extract that time, and configure it as a
# local time. It will then convert the local time properly to UTC +00:00 and
# write it back to the video.

# Generally this script would be executed before the `timestamp.sh` script.

#NOTE: This script should only be run if the timestamps stored in the video are
#not correctly set to UTC time.

# Usage:

# List the creation time and what it would be in local time:
# $ ./correct_times.sh GX013443.MP4

# Write the corrected local time to the video.
# $ ./correct_times.sh GX013443.MP4 --update

# Write the corrected local time to the video.and overwrite the existing file
# $ ./correct_times.sh GX013443.MP4 --update --over-write

# loop through files in a folder:
# $ for FILE in ./2022-06-28/*; do ./correct_times.sh "${FILE}"; done
# $ for FILE in ./2022-06-28/*; do ./correct_times.sh "${FILE}" --update; done
# $ for FILE in ./2022-06-28/*; do ./correct_times.sh "${FILE}" --update --over-write; done


# Requirements:

# JSON parser: $ sudo apt install jq

# References

# https://stackoverflow.com/questions/16548528/command-to-get-time-in-milliseconds
# https://stackoverflow.com/questions/28016578/how-can-i-parse-create-a-date-time-stamp-formatted-with-fractional-seconds-utc
# https://serverfault.com/questions/1020446/linux-get-utc-offset-from-current-or-other-timezones-on-given-date-and-time
# https://man7.org/linux/man-pages/man1/date.1.html

# ----------------
# Set the script to halt on errors
set -e

if [[ -z "$1" ]]; then
    echo "USAGE: $ 1./correct_times video.mp4 --update --over-write"
    exit 1
fi

# Get the movie name
MOVIE=$1

echo "${MOVIE}"
# need to use raw mode `-r` in jq call to return a raw string otherwise it will be trouble to deal with
CREATED=$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "${MOVIE}" | jq -r .format.tags.creation_time)

if [[ -z "${CREATED}" ]]; then
    echo "ERROR - No creation_time in metadata! ${MOVIE}"
    exit 1
fi

echo
echo "Video Create Time (Zulu):  ${CREATED}"

# Convert the creation time to local time
LOCALTIME=$(date --date="${CREATED}" "+%FT%T %z (%Z)")
echo "Video Create Time (local): ${LOCALTIME}"

echo "--------"
# strip the timezone from the string
STRIPPED=$(TZ=UTC date --date="${CREATED}" "+%FT%T")
echo "Video Create Time (without timezone):${STRIPPED}"

# Express the string in the local timezone
TO_LOCAL=$(date --date="${STRIPPED}" "+%FT%T %z (%Z)")
echo "Video Create Time (to local):${TO_LOCAL}"

echo "--------"
# convert the local time string to UTC
CONVERTED=$(TZ=UTC date --date="${TO_LOCAL}" "+%FT%T.%6NZ") # this is the correct time string
echo "Video Create Time (to UTC):${CONVERTED}"
echo

# Video Create Time (Zulu):  2022-06-28T12:34:27.000000Z
# Video Create Time (local): 2022-06-28T08:34:27 -0400 (EDT)
# --------
# Video Create Time (without timezone):2022-06-28T12:34:27
# Video Create Time (to local):2022-06-28T12:34:27 -0400 (EDT)
# --------
# Video Create Time (to UTC):2022-06-28T16:34:27.000000Z

# -----------

OUTPUT="${MOVIE%%.*}".corrected."${MOVIE##*.}"

if [ "${2:-"invalid"}" == "--update" ]; then

    # Write the converted timezone to the video
    echo "Updating metadata ${MOVIE} -> ${OUTPUT}"
    ffmpeg -i "${MOVIE}" -c copy -metadata creation_time="${CONVERTED}" "${OUTPUT}"

    if [ "${3:-"invalid"}" == "--over-write" ]; then

        echo "Moving ${OUTPUT} -> ${MOVIE}"
        mv "${OUTPUT}" "${MOVIE}"

    else
        echo "To overwrite the existing video, use --over-write"
    fi

else
    echo "To update the metadata use --update"
fi

...

 },
            "tags": {
                "creation_time": "2023-07-16T15:22:49.000000Z",
                "language": "und",
                "handler_name": "Core Media Video"
            }
        }
    ],
    "format": {
        "filename": "Dive2.mp4",
        "nb_streams": 2,
        "nb_programs": 0,
        "format_name": "mov,mp4,m4a,3gp,3g2,mj2",
        "format_long_name": "QuickTime / MOV",
        "start_time": "0.000000",
        "duration": "733.616217",
        "size": "751679577",
        "bit_rate": "8196978",
        "probe_score": 100,
        "tags": {
            "major_brand": "mp42",
            "minor_version": "1",
            "compatible_brands": "isommp41mp42",
            "creation_time": "2023-07-16T15:22:49.000000Z"
        }
    }
}


Scripts

ScriptDescription 

This script extracts the creation_time, removes the timezone and assigns the local timezone. It then converts this back to UTC and write it back to the video. This fixes issue with GoPro Videos.

USAGE: ./correct_time.sh video.mp4 --update 

Adds Timestamp in the top right hand corner of the video.

USAGE: ./timestamp.sh video.mp4

Sets the time to the local time specified and stores in the video as UTC.

USAGE: ./setTime.sh video.mp4 '2023-06-13T19:23:59' --update


References

ReferenceURL
Add Timestamp to Videos

https://www.bluebill.net/timestamps.html

Merging Chaptered GoPro Videos Whilst Preserving Telemetry | Trek View