Skip to content

Instantly share code, notes, and snippets.

@zlatinz
Created November 16, 2024 18:12
Show Gist options
  • Select an option

  • Save zlatinz/f68a224244a9ff1fe5c2e650e2f77e3f to your computer and use it in GitHub Desktop.

Select an option

Save zlatinz/f68a224244a9ff1fe5c2e650e2f77e3f to your computer and use it in GitHub Desktop.
Convert Microsoft Teams JSON transcript to SRT subtitles (Node.js)
const fs = require('fs');
/* Read a Microsoft Teams JSON file and convert it to a SRT file, using the fields: startOffset, endOffset and text */
// Usage: node convert.js <input.json> <output.srt>
/*example JSON file:
{
"$schema": "http://stream.office.com/schemas/transcript.json",
"version": "1.0.0",
"type": "Transcript",
"entries": [
{
"id": "a484e387-6fb4-4c9e-8af4-b155c9a0e347/44",
"speechServiceResultId": "8a3f7ae77ad14bd2b62f2821dba77887",
"text": "Here's what I love most about food and diet.",
"speakerId": "36b16ff6-46c2-4a7f-aa42-0ab4d3190fa8@3048dc87-43f0-4100-9acb-ae1971c79395",
"speakerDisplayName": "Narrator",
"confidence": 0.60541564,
"startOffset": "00:00:00.4981234",
"endOffset": "00:00:02.8272091",
"hasBeenEdited": false,
"roomId": null,
"spokenLanguageTag": "en-gb"
},
{
"id": "a484e387-6fb4-4c9e-8af4-b155c9a0e347/87",
"speechServiceResultId": "9afa49dbd4e5462f9340b3de0168a596",
"text": "We all eat several times a day, and we're totally in charge",
"speakerId": "36b16ff6-46c2-4a7f-aa42-0ab4d3190fa8@3048dc87-43f0-4100-9acb-ae1971c79395",
"speakerDisplayName": "Narrator",
"confidence": 0.32411957,
"startOffset": "00:00:02.8272091",
"endOffset": "00:00:06.3832091",
"hasBeenEdited": false,
"roomId": null,
"spokenLanguageTag": "en-gb"
},
{
"id": "a484e387-6fb4-4c9e-8af4-b155c9a0e347/89",
"speechServiceResultId": "ebbb601dda5f4742bbde7b4da14ac7a8",
"text": "of what goes on our plate and what stays off.",
"speakerId": "36b16ff6-46c2-4a7f-aa42-0ab4d3190fa8@3048dc87-43f0-4100-9acb-ae1971c79395",
"speakerDisplayName": "Narrator",
"confidence": 0.89466554,
"startOffset": "00:00:06.3832091",
"endOffset": "00:00:09.4272091",
"hasBeenEdited": false,
"roomId": null,
"spokenLanguageTag": "en-gb"
}
],
"events": [
]
}
example SRT file:
1
00:00:00,498 --> 00:00:02,827
Here's what I love most about food and diet.
2
00:00:02,827 --> 00:00:06,383
We all eat several times a day, and we're totally in charge
3
00:00:06,383 --> 00:00:09,427
of what goes on our plate and what stays off.
*/
if (process.argv.length !== 4) {
console.error('Usage: node convert.js <input.json> <output.srt>');
process.exit(1);
}
const inputFilePath = process.argv[2];
const outputFilePath = process.argv[3];
function formatTime(time) {
return time.replace('.', ',').substring(0, 12);
}
fs.readFile(inputFilePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading input file:', err);
process.exit(1);
}
const json = JSON.parse(data);
const entries = json.entries;
let srtContent = '';
entries.forEach((entry, index) => {
srtContent += `${index + 1}\n`;
srtContent += `${formatTime(entry.startOffset)} --> ${formatTime(entry.endOffset)}\n`;
srtContent += `${entry.text}\n\n`;
});
fs.writeFile(outputFilePath, srtContent, 'utf8', (err) => {
if (err) {
console.error('Error writing output file:', err);
process.exit(1);
}
console.log('SRT file has been created successfully.');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment