Skip to content

Instantly share code, notes, and snippets.

@anotherlab
Last active February 4, 2026 14:53
Show Gist options
  • Select an option

  • Save anotherlab/fcd1f934b71200b75c45ebb7b63fc382 to your computer and use it in GitHub Desktop.

Select an option

Save anotherlab/fcd1f934b71200b75c45ebb7b63fc382 to your computer and use it in GitHub Desktop.
Extract absolute altitude (abs_alt) from DJI SRT files
# Extract-AbsAlt.ps1
# Verion 1.02
# Extract absolute altitude (abs_alt) from DJI SRT files
# Usage: .\Extract-AbsAlt.ps1 -InputFile "input.srt" -OutputFile "output.srt" -DownsampleMs 1000 -RemoveFont -RemoveTimestamp
# Required Parameters:
# -InputFile: Path to the input SRT file
# -OutputFile: Path to the output SRT file
# Optional Parameters:
# -DownsampleMs: Interval in milliseconds to downsample the data (0 = no downsampling)
# -RemoveFont: Remove <font>...</font> tags from the output
# -RemoveFrameCount: Remove frame count information from the output lines
# -RemoveTimestamp: Remove timestamp information from the output lines
param (
[Parameter(Mandatory = $true)]
[string]$InputFile,
[Parameter(Mandatory = $true)]
[string]$OutputFile,
# Downsample interval in milliseconds (e.g. 1000 = 1 second)
[int]$DownsampleMs = 0,
# Remove <font>...</font> tags
[switch]$RemoveFont,
# Frame count removal
[Parameter()][switch]$RemoveFrameCount,
# Timestamp removal
[Parameter()][switch]$RemoveTimestamp
)
function Parse-TimeMs ($timestamp) {
# Converts "HH:MM:SS,mmm" to milliseconds
if ($timestamp -match '(\d+):(\d+):(\d+),(\d+)') {
return (
([int]$matches[1] * 3600000) +
([int]$matches[2] * 60000) +
([int]$matches[3] * 1000) +
[int]$matches[4]
)
}
return 0
}
$lines = Get-Content $InputFile
$output = @()
$lastWrittenTime = -1
$block = @()
Write-Host ('Reading ' + $InputFile )
$CurentLine = 0
foreach ($line in $lines + "") {
$CurentLine++
if ($line.Trim() -eq "") {
# End of SRT block
$percent = ($CurentLine * 100 / $lines.Length)
Write-Progress -Activity "Processing $InputFile" -Status "$percent% complete" -PercentComplete $percent
if ($block.Count -gt 0) {
$indexLine = $block | Where-Object { $_ -match '^\d+$' }
$timeLine = $block | Where-Object { $_ -match '-->' }
if ($indexLine -and $timeLine) {
$startTime = ($timeLine -split '-->')[0].Trim()
$startMs = Parse-TimeMs $startTime
if ($DownsampleMs -le 0 -or
$lastWrittenTime -lt 0 -or
($startMs - $lastWrittenTime) -ge $DownsampleMs) {
if ($RemoveFont) {
$block = $block -replace '<\/?font[^>]*>', ''
}
if ($RemoveFrameCount) {
$newBlock = @()
foreach($b in $block) {
$in = $b.IndexOf('FrameCnt: ')
if ($in -ge 0) {
if ($in -gt 0) {
$b = $b.Substring(0, $in).TrimEnd()
} else {
continue
}
}
$newBlock += $b
}
$block = $newBlock
}
if ($RemoveTimestamp) {
$newBlock = @()
foreach($b in $block) {
if ($b -match '\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}') {
continue
}
$newBlock += $b
}
$block = $newBlock
}
foreach($b in $block) {
# To include other fields, modify here
$newdata = ""
if ($b -match 'rel_alt:\s*([0-9]+(?:\.[0-9]+)?)') {
$newdata = "rel_alt: $($matches[1])"
}
# remove the `n to keep on the same line
if ($b -match 'abs_alt:\s*([0-9]+(?:\.[0-9]+)?)') {
$newdata = "`nabs_alt: $($matches[1])"
}
# remove the `n to keep on the same line
if ($b -match 'latitude:\s*([0-9]+(?:\.[0-9]+)?)') {
$newdata += "`nlatitude: $($matches[1])"
}
if ($b -match 'longitude:\s*([0-9]+(?:\.[0-9]+)?)') {
$newdata += "`nlongitude: $($matches[1])"
}
if ($newdata -ne "") {
$b = $newdata
}
$output += $b
}
$output += ""
$lastWrittenTime = $startMs
}
}
}
$block = @()
}
else {
$block += $line
}
}
Write-Host ('Writing ' + $OutputFile)
Out-File -FilePath $OutputFile -InputObject $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment