Skip to content

Commit 89f245a

Browse files
committed
Add very basic parsing code, will improve later
1 parent 846036a commit 89f245a

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

blackvue_gps.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
Get GPS records out of BlackVue video. Currently outputs CSV files,
3+
with support for more formats added later.
4+
5+
Usage:
6+
blackvue_gps --to-csv <filename>
7+
8+
"""
9+
import csv
10+
import os
11+
import re
12+
from collections import namedtuple
13+
14+
import pynmea2
15+
from docopt import docopt
16+
from pynmea2 import ParseError
17+
18+
import blackclue
19+
20+
GPSPoint = namedtuple('GPSPoint', ['unix_ms', 'lat', 'lon'])
21+
22+
23+
def parse_blackvue_nmea(filename):
24+
"""
25+
Parse a NMEA file as created by BlackVue, meaning there is a unix timestamp
26+
in milliseconds before the NMEA string.
27+
28+
:param filename: the location of the BlackVue NMEA as string
29+
:return: a list of GPSPoint
30+
"""
31+
filename = filename.replace(".mp4", ".nmea")
32+
records = []
33+
with open(filename, 'r') as r:
34+
for line in r.readlines():
35+
if line.startswith('['):
36+
# TODO make proper regex and less string slicing
37+
unix_ms = re.findall(r'(?=\[).*(?=\])', line)[0][1:]
38+
try:
39+
parsed_nmea = pynmea2.parse(line[len(unix_ms) + 2:])
40+
if hasattr(parsed_nmea, 'latitude'):
41+
lat, lon = parsed_nmea.latitude, parsed_nmea.longitude
42+
records.append(GPSPoint(unix_ms=unix_ms,
43+
lat=lat,
44+
lon=lon))
45+
except ParseError:
46+
print('ParseError', line[len(unix_ms) + 2:])
47+
return records
48+
49+
50+
def list_of_namedtuple_to_csv(list_of_tuples, outputfilename):
51+
"""
52+
Write a list of namedtuples to a csv file.
53+
54+
:param list_of_tuples: a list of namedtuples of the same type
55+
:param outputfilename: a target output filename
56+
"""
57+
fields = []
58+
if len(list_of_tuples) > 0:
59+
fields = list_of_tuples[0]._fields
60+
with open(outputfilename, 'w') as w:
61+
csvwriter = csv.writer(w)
62+
csvwriter.writerow(fields)
63+
for row in list_of_tuples:
64+
csvwriter.writerow(row)
65+
66+
67+
def main(**kwargs):
68+
tocsv = kwargs['--to-csv']
69+
filename = kwargs['<filename>']
70+
if filename:
71+
if os.path.isfile(filename):
72+
records = parse_blackvue_nmea(filename=filename)
73+
if tocsv:
74+
outputfilename = filename.replace('.mp4', '.gps.csv')
75+
list_of_namedtuple_to_csv(records, outputfilename)
76+
elif os.path.isdir(filename):
77+
foldername = filename
78+
for filename in filter(lambda x: x.endswith('.mp4'),
79+
os.listdir(foldername)):
80+
blackclue.dump(file=[os.path.join(foldername, filename)],
81+
dump_embedded=True,
82+
dump_raw_blocks=False,
83+
extended_scan=False,
84+
verbose=False)
85+
records = parse_blackvue_nmea(filename=os.path.join(
86+
foldername, filename))
87+
if tocsv:
88+
outputfilename = filename.replace('.mp4', '.gps.csv')
89+
list_of_namedtuple_to_csv(records, os.path.join(
90+
foldername, outputfilename))
91+
92+
93+
if __name__ == "__main__":
94+
main(**docopt(__doc__))

0 commit comments

Comments
 (0)