Be careful: This script has been created to be used under a command controller in Yii2.
You can easily port to any php by adapting last three lines
<?php | |
$data= json_decode($aws_transcribe_json, true); | |
$labels = $data['results']['speaker_labels']['segments']; | |
$speakerStartTimes = []; | |
foreach ($labels as $label) { | |
foreach ($label['items'] as $item) { | |
$speakerStartTimes[$item['start_time']] = $item['speaker_label']; | |
} | |
} | |
$items = $data['results']['items']; | |
$lines = []; | |
$line = ""; | |
$time = 0; | |
$speaker = null; | |
$i = 0; | |
foreach ($items as $item) { | |
$i++; | |
$content = $item['alternatives']['0']['content']; | |
if (isset($item['start_time'])) { | |
$currentSpeaker = $speakerStartTimes[$item['start_time']]; | |
} elseif ($item['type'] == "punctuation") { | |
$line .= $content; | |
} | |
if ($currentSpeaker != $speaker) { | |
if (!empty($speaker)) { | |
$lines[] = [ | |
'speaker' => $speaker, | |
'line' => $line, | |
'time' => $time | |
]; | |
} | |
$line = $content; | |
$speaker = $currentSpeaker; | |
$time = $item['start_time']; | |
} else if ($item['type'] != "punctuation") { | |
$line = $line . " " . $content; | |
} | |
} | |
$lines[] = [ | |
'speaker' => $speaker, | |
'line' => $line, | |
'time' => $time | |
]; | |
usort($lines, function ($a, $b) { | |
return $a['time'] <=> $b['time']; | |
}); | |
foreach ($lines as $line){ | |
$this->stdout(\Yii::$app->formatter->asTime($line['time']),Console::FG_GREEN). " "; | |
$this->stdout($line['speaker'],Console::FG_BLUE). " "; | |
$this->stdout($line['line']."\n"); | |
} |