Creating SRT Files for Videos Using Node.js: A Comprehensive Guide
Creating SRT subtitle recordsdata for movies is a vital activity for bettering accessibility and consumer engagement. In line with AssemblyAI, this may be effectively achieved utilizing Node.js and the AssemblyAI API. This information walks by the method step-by-step.
Step 1: Arrange Your Improvement Surroundings
To start, guarantee you may have Node.js 18 or increased put in in your system. Create a brand new mission folder and initialize a Node.js mission:
mkdir srt-subtitles
cd srt-subtitles
npm init -y
Open the package deal.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
Get hold of 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: "
});
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 required module to avoid wasting the file to disk:
import { writeFile } from "fs/promises";
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 a number of seconds, a brand new file subtitles.srt
will seem on disk, containing the generated subtitles.
Subsequent Steps
Now that you’ve your subtitle file, you may add it to YouTube Studio or configure it in your video participant. AssemblyAI additionally presents varied instruments to reinforce your audio and video purposes, which might be explored by their blog and documentation.
Picture supply: Shutterstock