Skip to content

Instantly share code, notes, and snippets.

@mtsukamoto
Created February 17, 2025 11:16
Show Gist options
  • Save mtsukamoto/0055e1dd0aae28e5fa732617684eeb8c to your computer and use it in GitHub Desktop.
Save mtsukamoto/0055e1dd0aae28e5fa732617684eeb8c to your computer and use it in GitHub Desktop.
Convert-AWSTranscribeJsonToText.ps1 - AWS TranscribeのJSON出力を適度にまとめつつテキストに変換
param (
[string]$InputJson, # 入力JSONファイル
[string]$OutputTxt # 出力テキストファイル
)
# JSONを読み込む
$data = Get-Content $InputJson | ConvertFrom-Json
$items = $data.results.items
# 整理された発話リスト
$groupedSpeeches = @()
$currentSpeech = $null
# 数値を h:mm:ss 形式に変換する関数
function ConvertTo-TimeFormat ($seconds) {
$ts = [timespan]::FromSeconds([math]::Round($seconds))
return "{0}:{1:D2}:{2:D2}" -f $ts.Hours, $ts.Minutes, $ts.Seconds
}
foreach ($item in $items) {
if (-not $item.alternatives[0].content) { continue }
$text = $item.alternatives[0].content
$speaker = $item.speaker_label
$startTime = if ($item.start_time) { [decimal]$item.start_time } else { $null }
$endTime = if ($item.end_time) { [decimal]$item.end_time } else { $null }
$isNewSpeech = $false
if ($currentSpeech) {
# 発話者が変わったら新しい発言とみなす
if ($currentSpeech.speaker -ne $speaker) {
$isNewSpeech = $true
}
# 前の発言の終了時刻と現在の発言の開始時刻の差が3秒以上なら新しい発言とみなす
elseif ($currentSpeech.endTime -and $startTime -and (($startTime - $currentSpeech.endTime) -ge 3)) {
$isNewSpeech = $true
}
# 前の発言が句点で終わっていたら新しい発言とみなす
elseif ($currentSpeech.content -match '[。.]$') {
$isNewSpeech = $true
}
} else {
$isNewSpeech = $true
}
if ($isNewSpeech) {
if ($currentSpeech) {
$groupedSpeeches += $currentSpeech
}
$currentSpeech = [PSCustomObject]@{
content = $text
startTime = $startTime
endTime = $endTime
speaker = $speaker
}
} else {
# 連結時のスペース処理
if ($currentSpeech.content -match '[a-zA-Z0-9]$' -and $text -match '^[a-zA-Z0-9]') {
$currentSpeech.content += ' '
}
$currentSpeech.content += $text
# 開始時刻は最初のまま、終了時刻は更新
if ($endTime) {
$currentSpeech.endTime = $endTime
}
}
}
# 最後の発話を追加
if ($currentSpeech) {
$groupedSpeeches += $currentSpeech
}
# 結果をファイルに出力
$output = ""
$previousSpeech = $null
foreach ($speech in $groupedSpeeches) {
# 時刻を h:mm:ss に変換
$startStr = if ($speech.startTime) { ConvertTo-TimeFormat $speech.startTime } else { "--:--:--" }
$endStr = if ($speech.endTime) { ConvertTo-TimeFormat $speech.endTime } else { "--:--:--" }
# 空行を挿入する条件
if ($previousSpeech) {
if ($speech.speaker -ne $previousSpeech.speaker -or ($speech.startTime - $previousSpeech.endTime -ge 3)) {
$output += "`n"
}
}
# 発話内容をフォーマット
$output += "[$startStr - $endStr] $($speech.speaker): $($speech.content)`n"
$previousSpeech = $speech
}
$output | Out-File -Encoding UTF8 $OutputTxt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment