forked from juanmcasillas/gopro2gpx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpmf.py
112 lines (83 loc) · 3.28 KB
/
gpmf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
# 17/02/2019
# Juan M. Casillas <[email protected]>
# https://github.com/juanmcasillas/gopro2gpx.git
#
# Released under GNU GENERAL PUBLIC LICENSE v3. (Use at your own risk)
#
# based on the info from:
# https://github.com/gopro/gpmf-parser
# https://docs.python.org/3/library/struct.html
# https://github.com/stilldavid/gopro-utils/blob/master/telemetry/reader.go
import os
import array
import sys
import struct
from ffmpegtools import FFMpegTools
from klvdata import KLVData
class Parser:
def __init__(self, config):
self.config = config
self.ffmtools = FFMpegTools(self.config)
# map some handy shortcuts
self.verbose = config.verbose
self.file = config.file
self.outputfile = config.outputfile
def readFromMP4(self):
"""read data the metadata track from video. Requires FFMPEG wrapper.
-vv creates a dump file with the binary data called dump_track.bin
"""
if not os.path.exists(self.file):
raise FileNotFoundError("Can't open %s" % self.file)
track_number, lineinfo = self.ffmtools.getMetadataTrack(self.file)
if not track_number:
raise Exception("File %s doesn't have any metadata" % self.file)
if self.verbose:
print("Working on file %s track %s (%s)" % (self.file, track_number, lineinfo))
metadata_raw = self.ffmtools.getMetadata(track_number, self.file)
if self.verbose == 2:
print("Creating output file for binary data (fromMP4): %s" % self.outputfile)
f = open("%s.bin" % self.outputfile, "wb")
f.write(metadata_raw)
f.close()
# process the data here
metadata = self.parseStream(metadata_raw)
return(metadata)
def readFromBinary(self):
"""read data from binary file, instead extract the metadata track from video. Useful for quick development
-vv creates a dump file with the binary data called dump_binary.bin
"""
if not os.path.exists(self.file):
raise FileNotFoundError("Can't open %s" % self.file)
if self.verbose:
print("Reading binary file %s" % (self.file))
fd = open(self.file, 'rb')
data = fd.read()
fd.close()
if self.verbose == 2:
print("Creating output file for binary data (from binary): %s" % self.outputfile)
f = open("%s.bin" % self.outputfile, "wb")
f.write(data)
f.close()
# process the data here
metadata = self.parseStream(data)
return metadata
def parseStream(self, data_raw):
"""
main code that reads the points
"""
data = array.array('b')
data.fromstring(data_raw)
offset = 0
klvlist = []
while offset < len(data):
klv = KLVData(data,offset)
if not klv.skip():
klvlist.append(klv)
if self.verbose == 3:
print(klv)
offset += 8
if klv.type != 0:
offset += klv.padded_length
#print(">offset:%d length:%d padded:%d" % (offset, length, padded_length))
return(klvlist)