Created
July 26, 2025 11:52
-
-
Save pfichtner/1c06c49fc03cd22669c5cccbf627a210 to your computer and use it in GitHub Desktop.
Extract GPX data from gopro mp4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import os | |
| import subprocess | |
| from datetime import datetime, timezone | |
| import gpxpy.gpx | |
| from py_gpmf_parser.gopro_telemetry_extractor import GoProTelemetryExtractor | |
| def extract_gpmd_stream(mp4_path, output_bin="gpmd_stream.bin"): | |
| print("Extracting telemetry stream from MP4...") | |
| cmd = [ | |
| "ffmpeg", "-y", # overwrite output file if exists | |
| "-i", mp4_path, | |
| "-map", "0:3", # telemetry stream index 3 | |
| "-c", "copy", | |
| "-f", "data", | |
| output_bin | |
| ] | |
| result = subprocess.run(cmd, capture_output=True, text=True) | |
| if result.returncode != 0: | |
| print("❌ Failed to extract telemetry stream with ffmpeg:") | |
| print(result.stderr) | |
| return None | |
| print(f"✅ Telemetry stream extracted to {output_bin}") | |
| return output_bin | |
| def parse_telemetry_and_generate_gpx(mp4_path, output_gpx_path=None): | |
| extractor = GoProTelemetryExtractor(mp4_path) | |
| extractor.open_source() | |
| gps_vals, gps_times = extractor.extract_data("GPS5") | |
| extractor.close_source() | |
| if gps_vals is None or len(gps_vals) == 0: | |
| print("No GPS5 data found in telemetry.") | |
| return False | |
| gpx = gpxpy.gpx.GPX() | |
| trk = gpxpy.gpx.GPXTrack() | |
| gpx.tracks.append(trk) | |
| seg = gpxpy.gpx.GPXTrackSegment() | |
| trk.segments.append(seg) | |
| for (lat, lon, alt, *_), time_float in zip(gps_vals, gps_times): | |
| # Convert float timestamp to datetime UTC | |
| try: | |
| time_dt = datetime.fromtimestamp(time_float, tz=timezone.utc) | |
| except Exception as e: | |
| print(f"Warning: failed to convert timestamp {time_float}: {e}") | |
| time_dt = None | |
| pt = gpxpy.gpx.GPXTrackPoint(latitude=lat, longitude=lon, elevation=alt) | |
| pt.time = time_dt | |
| seg.points.append(pt) | |
| if not output_gpx_path: | |
| output_gpx_path = mp4_path.rsplit('.', 1)[0] + ".gpx" | |
| with open(output_gpx_path, "w") as f: | |
| f.write(gpx.to_xml()) | |
| print(f"✅ GPX saved to {output_gpx_path}") | |
| return True | |
| def main(): | |
| if len(sys.argv) != 2: | |
| print("Usage: python extract_gpx.py <file.mp4>") | |
| sys.exit(1) | |
| mp4_file = sys.argv[1] | |
| if not os.path.isfile(mp4_file): | |
| print(f"File not found: {mp4_file}") | |
| sys.exit(1) | |
| gpmd_bin = extract_gpmd_stream(mp4_file) | |
| if not gpmd_bin: | |
| sys.exit(1) | |
| success = parse_telemetry_and_generate_gpx(mp4_file) | |
| if not success: | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment