Skip to content

Instantly share code, notes, and snippets.

@Eseperio
Last active August 26, 2021 08:52
Show Gist options
  • Save Eseperio/cebc0ebeade5687ba14dfcb1a8447559 to your computer and use it in GitHub Desktop.
Save Eseperio/cebc0ebeade5687ba14dfcb1a8447559 to your computer and use it in GitHub Desktop.
AWS Transcribe JSON to Script with php

Snippet to convert AWS transcribe into a scripted text

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");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment