This is the second part of a two-part guide on how to use AWS Transcribe to convert audio to text, and use these as subtitles for your videos. The first part covers how you use the AWS Transcribe API to generate an audio transcription from a video file.

Convert AWS transcriptions to SRT using Node.js

We finished off the last guide with a JSON file we got through AWS Transcribe. The contents of this file look as follows:

{
"jobName": "transcription-job-01",
"accountId": "140587111551",
"results": {
"transcripts": [
{
"transcript": "Hi, my name is Scott Co. As an entrepreneur. I cannot overstate how important it is these days to use video as a tool to reach your audience, your community and your customers. People connect with stories. And video allows us to be the most authentic we can be in order to tell those stories. And so if you can present in front of a camera and you have the right tools to support and amplify you, you can be unstoppable."
}
],
"items": [
{
"start_time": "0.54",
"end_time": "0.95",
"alternatives": [{ "confidence": "1.0", "content": "Hi" }],
"type": "pronunciation"
},
{
"alternatives": [{ "confidence": "0.0", "content": "," }],
"type": "punctuation"
},

...
{
"start_time": "24.46",
"end_time": "25.35",
"alternatives": [{ "confidence": "1.0", "content": "unstoppable" }],
"type": "pronunciation"
},
{
"alternatives": [{ "confidence": "0.0", "content": "." }],
"type": "punctuation"
}
]
},
"status": "COMPLETED"
}

To convert this JSON specification to the industry standard SRT format which are able to be used by all most media players we will need to convert the JSON using aws-transcription-to-srt.

Generating an SRT file from an AWS Transcription using Node.js

Create a new Node.js project and add the asrOutput.json file downloaded from AWS Transcribe into the project folder.

Install aws-transcription-to-srt with:

$ npm i aws-transcription-to-srt

Then create an index.js file and add the following:

const srtConvert = require("aws-transcription-to-srt");
const fs = require("fs");
fs.readFile("asrOutput.json", "utf8", (err, data) => {
if (err) {
console.error(err);
}
const json = JSON.parse(data);
const srt = srtConvert(json);
fs.writeFile("subtitles.srt", srt, (err) => {
if (err) {
console.error(err);
}
// File written successfully
console.log("Check subtitles.srt");
});
});

The above code reads the asrOutput.json file and parses its content into a JSON object. It then calls srtConvert() to generate SRT of the JSON from AWS and saves the subtitles into a subtitles.srt file.

If you run the code and check subtitles.srt, you'll see the subtitles:

1
00:00:00.540 --> 00:00:00.950
Hi,
2
00:00:00.960 --> 00:00:01.950
my name is Scott Co.
3
00:00:02.340 --> 00:00:03.210
As an entrepreneur.
4
00:00:03.220 --> 00:00:08.140
I cannot overstate how important it is these days to use video as a tool to reach your
5
00:00:08.150 --> 00:00:08.690
audience,
6
00:00:08.700 --> 00:00:11.020
your community and your customers.
7
00:00:11.030 --> 00:00:13.140
People connect with stories.
8
00:00:13.150 --> 00:00:18.000
And video allows us to be the most authentic we can be in order to tell those stories.
9
00:00:18.010 --> 00:00:22.590
And so if you can present in front of a camera and you have the right tools to support and
10
00:00:22.600 --> 00:00:23.550
amplify you,
11
00:00:23.640 --> 00:00:25.350
you can be unstoppable.

Now that you have a subtitles file, you can load it into a media player when watching the video, or upload it with the video if hosting it on a platform that supports subtitles. e.g. YouTube. You can also integrate the subtitle into the video file. To do so, you can use the Shotstack video editing API that enables you to create and edit videos easily and at scale.

To learn how to hard code subtitles into your video, take a look at how to add subtitles to a video using PHP.

Your final video should look like this:

Get started with Shotstack's video editing API in two steps:

  1. Sign up for free to get your API key.
  2. Send an API request to create your video:
    curl --request POST 'https://api.shotstack.io/v1/render' \
    --header 'x-api-key: YOUR_API_KEY' \
    --data-raw '{
    "timeline": {
    "tracks": [
    {
    "clips": [
    {
    "asset": {
    "type": "video",
    "src": "https://shotstack-assets.s3.amazonaws.com/footage/beach-overhead.mp4"
    },
    "start": 0,
    "length": "auto"
    }
    ]
    }
    ]
    },
    "output": {
    "format": "mp4",
    "size": {
    "width": 1280,
    "height": 720
    }
    }
    }'
Joyce Echessa

BY JOYCE ECHESSA
24th May 2021

Studio Real Estate
Experience Shotstack for yourself.
SIGN UP FOR FREE

You might also like

How to add captions to video using PHP

How to add captions to video using PHP

Vlad Bartusica