Creating SRT subtitle recordsdata for movies is a vital activity for enhancing accessibility and person engagement. In keeping with AssemblyAI, this may be effectively achieved utilizing Node.js and the AssemblyAI API. This information walks by way of the method step-by-step.
Step 1: Arrange Your Growth Atmosphere
To start, guarantee you could have Node.js 18 or increased put in in your system. Create a brand new undertaking folder and initialize a Node.js undertaking:
mkdir srt-subtitles
cd srt-subtitles
npm init -y
Open the bundle.json file and add sort: “module”, to make use of the ES Module syntax. Subsequent, set up the AssemblyAI JavaScript SDK:
npm set up –save assemblyai
Receive an AssemblyAI API key out of your dashboard and set it as an setting variable:
# Mac/Linux:
export ASSEMBLYAI_API_KEY=<YOUR_KEY>
# Home windows:
set ASSEMBLYAI_API_KEY=<YOUR_KEY>
Step 2: Transcribe Your Video
With the setting arrange, you can begin transcribing your video recordsdata. Use a publicly accessible video URL or specify native recordsdata. Create a file referred to as index.js and add the next code:
import { AssemblyAI } from ‘assemblyai’;
const consumer = new AssemblyAI({ apiKey: course of.env.ASSEMBLYAI_API_KEY });
const transcript = await consumer.transcripts.transcribe({
audio: “https://storage.googleapis.com/aai-web-samples/aai-overview.mp4”,
});
Verify for errors and log them:
if (transcript.standing === “error”) {
throw new Error(transcript.error);
}
Step 3: Generate SRT File
After acquiring the transcript, generate the subtitles in SRT format. Import the mandatory module to save lots of the file to disk:
import { writeFile } from “fs/guarantees”;
Then, generate the SRT subtitles and save them:
const srt = await consumer.transcripts.subtitles(transcript.id, “srt”);
await writeFile(“./subtitles.srt”, srt);
You possibly can customise the captions by specifying the chars_per_caption parameter:
const srt = await consumer.transcripts.subtitles(transcript.id, “srt”, 32);
await writeFile(“./subtitles.srt”, srt);
Step 4: Run the Script
Lastly, run the script to generate the subtitles:
node index.js
After just a few seconds, a brand new file subtitles.srt will seem on disk, containing the generated subtitles.
Subsequent Steps
Now that you’ve got your subtitle file, you’ll be able to add it to YouTube Studio or configure it in your video participant. AssemblyAI additionally presents numerous instruments to reinforce your audio and video purposes, which could be explored by way of their weblog and documentation.
Picture supply: Shutterstock