Skip to content

Instantly share code, notes, and snippets.

@derickr
Last active May 3, 2024 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derickr/f30bc04c394e89573071171459f6a9d4 to your computer and use it in GitHub Desktop.
Save derickr/f30bc04c394e89573071171459f6a9d4 to your computer and use it in GitHub Desktop.
A script to create SRT subtitle files out of WhisperNet's JSON files
<?php
/**
1
00:00:03,600 --> 00:00:09,740
Hello, today we're going to have a look at how to make a flame graph, a new feature
2
00:00:09,740 --> 00:00:11,920
in Xdebug 3.3.
3
00:00:12,620 --> 00:00:16,920
Flame graphs are an interesting way of showing where an application spends a lot of time.
*/
$f = json_decode( file_get_contents( $argv[1] ) );
$segmentCounter = 0;
$segmentText = '';
$segmentLength = 0;
$segmentStart = 0;
$lastStart = 0;
$lastEnd = 0;
foreach ( $f->segments as $segment )
{
foreach ( $segment->words as $word )
{
if ($word->start > $lastEnd + 0.25) {
$segmentEnd = $lastEnd;
emit();
$segmentStart = $word->start;
}
if ($segmentLength > 60) {
$segmentText = trim($segmentText) . "\n" . ltrim($word->word);
$segmentLength = strlen($word->word);
} else {
$segmentText .= $word->word;
$segmentLength += strlen($word->word);
}
$lastEnd = $word->end;
}
}
$segmentEnd = $lastEnd;
emit();
function emit()
{
global $segmentCounter, $segmentStart, $segmentEnd, $segmentText, $segmentLength;
$secondsStart = (int) $segmentStart;
$milliSecondsStart = (int) (($segmentStart - $secondsStart) * 1000);
$secondsEnd = (int) $segmentEnd;
$milliSecondsEnd = (int) (($segmentEnd - $secondsEnd) * 1000);
$segmentStartFmt = sprintf("%02d:%02d:%02d,%03d",
$secondsStart / 3600,
$secondsStart / 60,
$secondsStart % 60,
$milliSecondsStart);
$segmentEndFmt = sprintf("%02d:%02d:%02d,%03d",
$secondsEnd / 3600,
$secondsEnd / 60,
$secondsEnd % 60,
$milliSecondsEnd);
if (strlen($segmentText) == 0) {
return;
}
printf("%s\n%s --> %s\n%s\n\n", $segmentCounter, $segmentStartFmt, $segmentEndFmt, trim($segmentText));
$segmentText = '';
$segmentLength = 0;
$segmentCounter++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment