-
Notifications
You must be signed in to change notification settings - Fork 66
/
transcript_ch.php
62 lines (56 loc) · 1.58 KB
/
transcript_ch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
$file = $argv[1];
$data = json_decode(file_get_contents($file));
$basename = basename($file);
// Load up channel labels.
$labels = $data->results->channel_labels->channels;
$channel_start_times = [];
foreach ($labels as $label) {
foreach ($label->items as $item) {
$channel_start_times[number_format($item->start_time, 3)] = $label->channel_label;
}
}
// Now we iterate through items and build the transcript
$items = $data->results->items;
$lines = [];
$line = '';
$time = 0;
$channel = NULL;
foreach ($items as $item) {
$content = $item->alternatives[0]->content;
if (property_exists($item, 'start_time')) {
$current_channel = $channel_start_times[number_format($item->start_time, 3)];
}
elseif ($item->type == 'punctuation') {
$line .= $content;
}
if ($current_channel != $channel) {
if ($channel) {
$lines[] = [
'channel' => $channel,
'line' => $line,
'time' => $time,
];
}
$line = $content;
$channel = $current_channel;
$time = number_format($item->start_time, 3, '.', '');
}
elseif ($item->type != 'punctuation') {
$line .= ' ' . $content;
}
}
// Record the last line since there was no channel change.
$lines[] = [
'channel' => $channel,
'line' => $line,
'time' => $time,
];
// Finally, let's print out our transcript.
$fh = fopen($file . '-transcript_ch.txt', 'w');
foreach ($lines as $line_data) {
$line = '[' . gmdate('H:i:s', $line_data['time']) . '] ' . $line_data['channel'] . ': ' . $line_data['line'];
fputs($fh, $line . "\n\n");
}
fclose($fh);
/* End of the transcript_ch.php file */