Last active
March 25, 2019 09:24
-
-
Save bmcbride/9689580 to your computer and use it in GitHub Desktop.
Python script to convert a RouteShoot generated .kml file into an .srt subtitle file for overlaying info on the video. Add the following tag to the YouTube video to force showing the captions when the video plays: yt:cc=on.
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 xml.etree.ElementTree as ET | |
metric = 0 | |
kml = raw_input('Enter the KML file path and name: ') | |
srt = kml.replace('.kml', '.srt'); | |
file = open(srt, 'w') | |
if metric == 1: | |
speedUnits = '(km/h)' | |
altitudeUnits = '(m)' | |
distanceUnits = '(km)' | |
if metric == 0: | |
speedUnits = '(mi/h)' | |
altitudeUnits = '(ft)' | |
distanceUnits = '(mi)' | |
tree = ET.parse(kml) | |
root = tree.getroot() | |
def formatSrtTime(secTime): | |
'''Convert a time in decimal seconds to srt time format. http://code.activestate.com/recipes/577459-convert-a-youtube-transcript-in-srt-subtitle/''' | |
sec, micro = str(secTime).split('.') | |
m, s = divmod(int(sec), 60) | |
h, m = divmod(m, 60) | |
return "{:02}:{:02}:{:02},{}".format(h,m,s,micro) | |
for child in root.findall('./Document/Placemark/ExtendedData/SchemaData'): | |
fid = child[0].text | |
lat = child[1].text | |
lon = child[2].text | |
bearing = '%.2f' % float(child[3].text) | |
speed = '%.2f' % ((float(child[4].text)*3600)/1000) | |
uid = child[5].text | |
altitude = '%.2f' % float(child[6].text) | |
date = child[7].text | |
time = child[8].text | |
distance = '%.3f' % (float(child[9].text)*0.001) | |
start = str(int(fid) - 1) + '.00' | |
stop = str(fid) + '.00' | |
if metric == 0: | |
speed = '%.2f' % (float(speed)*0.621371) | |
altitude = '%.2f' % (float(altitude)*3.28084) | |
distance = '%.3f' % (float(distance)*0.621371) | |
file.write(fid + '\n') | |
file.write(formatSrtTime(start) + ' --> ' + formatSrtTime(stop) + '\n') | |
file.write('sp: ' + speed + ' ' + speedUnits + ' | dist: ' + distance + ' ' + distanceUnits + ' | elev: ' + altitude + ' ' + altitudeUnits + ' | bearing: ' + bearing + '\n') | |
file.write(date + ' | ' + time + ' | lat: ' + lat + ' | lon: ' + lon + '\n') | |
file.write('\n') | |
file.close() | |
print 'done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment