Skip to content

Instantly share code, notes, and snippets.

@tarensanders
Last active May 12, 2023 00:23
Show Gist options
  • Save tarensanders/6f7c2a52d8d840b6966904a1c4611de6 to your computer and use it in GitHub Desktop.
Save tarensanders/6f7c2a52d8d840b6966904a1c4611de6 to your computer and use it in GitHub Desktop.
Extract creation time from GoPro files

Installation

Install FFmpeg

Download FFmpeg from their downloads page for your OS. If you're not sure how to do this, there are instructions for Windows and Mac.

To check that it is working, run ffmpeg --version in powershell or terminal.

Install Python

If you haven't done this before, Anaconda is a great choice.

If you are on Windows, use Anaconda Prompt (from the start menu) for the next steps.

Install tqdm

In terminal or anaconda prompt:

pip install tqdm

You could also create a conda environment.

Run the script

  1. Download the extract_time.py script and save it somewhere local.
  2. You will need to edit the folder_path to match the location of your files. Note that on Windows, the file paths need to be escaped. So C:\Path\to\files should become C:\\Path\\to\\files.
  3. In terminal or anaconda prompt, navigate to the location of the extract_time.py script (you can use cd to change directories)
  4. Run the script with python extract_time.py
  5. Data will be stored in metadata.csv in the same folder as you ran the script from.
import csv
import json
import os
import subprocess
from datetime import datetime
from tqdm import tqdm
# Add the file paths here:
folder_path = "C:\\Path\\to\\files" # e.g., /Users/tasanders/Downloads/test
output_file = "metadata.csv"
file_format = ".LRV" # Would also work with ".MP4"
def get_metadata(video_file):
cmd = [
"ffprobe",
video_file,
"-print_format",
"json",
"-show_streams",
"-show_format",
"-loglevel",
"quiet",
"-hide_banner",
]
try:
json_payload = subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True
)
data = json.loads(json_payload.stdout)
except subprocess.CalledProcessError as e:
print(f"Error with ffprobe: {e}")
if (
"streams" in data
and len(data["streams"]) > 0
and "tags" in data["streams"][0]
and "duration" in data["streams"][0]
):
stream_0 = data["streams"][0]
creation_time = stream_0["tags"]["creation_time"]
duration = stream_0["duration"]
# Convert the creation time to a datetime object
dt = datetime.strptime(creation_time, "%Y-%m-%dT%H:%M:%S.%fZ")
formatted_creation_time = dt.strftime("%Y-%m-%d %H:%M:%S")
return formatted_creation_time, duration
else:
return None, None
video_files = [f for f in os.listdir(folder_path) if f.endswith(file_format)]
metadata_list = []
for video in (pbar := tqdm(video_files)):
pbar.set_description(f"Processing {video}", refresh=True)
video_path = os.path.join(folder_path, video)
try:
creation_time, duration = get_metadata(video_path)
metadata_list.append(
{"filename": video, "creation_time": creation_time, "duration": duration}
)
except Exception as e:
print(f"Error with {video}: {e}")
# Write the metadata to a CSV file
with open(output_file, "w", newline="") as csvfile:
fieldnames = ["filename", "creation_time", "duration"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for metadata in metadata_list:
writer.writerow(metadata)
print(f"Metadata written to {output_file} for {len(video_files)} files.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment