Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 30, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 30, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 30, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 30, 2025

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025

      The Witcher 3: Wild Hunt reaches 60 million copies sold as work continues on The Witcher 4

      May 30, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      How Remix is shaking things up

      May 30, 2025
      Recent

      How Remix is shaking things up

      May 30, 2025

      Perficient at Kscope25: Let’s Meet in Texas!

      May 30, 2025

      Salesforce + Informatica: What It Means for Data Cloud and Our Customers

      May 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025
      Recent

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Artificial Intelligence»How to Create WebVTT Files for Videos in Node.js

    How to Create WebVTT Files for Videos in Node.js

    June 17, 2024

    WebVTT .vtt or Web Video Text Tracks Format is a widely used and supported format for subtitles in videos. This is what the first lines of the WebVTT file for this YouTube video look like:

    WEBVTT

    00:00.170 –> 00:04.234
    AssemblyAI is building AI systems to help you build AI applications

    00:04.282 –> 00:08.106
    with spoken data. We create superhuman AI models for speech

    In this guide, you’ll learn how to create WebVTT files for videos using Node.js and the AssemblyAI API.

    Step 1: Set up your development environment

    First, install Node.js 18 or higher on your system.
    Next, create a new project folder, change directories to it, and initialize a new Node.js project:

    mkdir vtt-subtitles
    cd vtt-subtitles
    npm init -y

    Open the package.json file and add type: “module”, to the list of properties.

    {
    …
    “type”: “module”,
    …
    }

    This will tell Node.js to use the ES Module syntax for exporting and importing modules, and not to use the old CommonJS syntax.

    Then, install the AssemblyAI JavaScript SDK which makes it easier to interact with the AssemblyAI API:

    npm install –save assemblyai

    Next, you need an AssemblyAI API key that you can find on your dashboard. If you don’t have an AssemblyAI account, first sign up for free. Once you’ve copied your API key, configure it as the ASSEMBLYAI_API_KEY environment variable on your machine:

    # Mac/Linux:
    export ASSEMBLYAI_API_KEY=<YOUR_KEY>

    # Windows:
    set ASSEMBLYAI_API_KEY=<YOUR_KEY>

    2. Transcribe your video

    Now that your development environment is ready, you can start transcribing your video files. In this tutorial, you’ll use this video in MP4 format. The AssemblyAI SDK can transcribe any audio or video file that’s publicly accessible via a URL, but you can also specify local files. Create a file called index.js and add the following code:

    import { AssemblyAI } from ‘assemblyai’;

    // create AssemblyAI API client
    const client = new AssemblyAI({ apiKey: process.env.ASSEMBLYAI_API_KEY });

    // transcribe audio or video file
    const transcript = await client.transcripts.transcribe({
    audio: “https://storage.googleapis.com/aai-web-samples/aai-overview.mp4”,
    });

    If the transcription is successful, the transcript object will be populated with the transcript text and many additional properties. However, you should verify whether an error occurred and log the error.

    Hostinger

    Add the following lines of JavaScript:

    // throw error if transcript status is error
    if (transcript.status === “error”) {
    throw new Error(transcript.error);
    }

    3. Generate WebVTT file

    Now that you have a transcript, you can generate the subtitles in WebVTT format.
    Add the following import which you’ll need to save the WebVTT file to disk.

    import { writeFile } from “fs/promises”

    Then add the following code to generate the WebVTT subtitles from the transcript and download the VTT file to disk.

    // generate WebVTT subtitles
    const vtt = await client.transcripts.subtitles(transcript.id, “vtt”);
    await writeFile(“./subtitles.vtt”, vtt);

    You can customize the maximum number of characters per caption by specifying the third parameter (chars_per_caption).

    // generate WebVTT subtitles
    const vtt = await client.transcripts.subtitles(transcript.id, “vtt”, 32);
    await writeFile(“./subtitles.vtt”, vtt);

    WebVTT Subtitle Format

    SRT is another widely supported and popular subtitle format. To generate SRT, replace `”vtt”` with `”srt”`, and save the file with the srt-extension.

    4. Run the script

    To run the script, go back to your shell and run:

    node index.js

    After a couple of seconds, you’ll see a new file on disk subtitles.vtt, which looks like this:

    WEBVTT

    00:00.200 –> 00:04.430
    AssemblyAI is building AI systems to help you build AI applications with

    00:04.462 –> 00:08.694
    spoken data. We create superhuman AI models for speech recognition,

    00:08.774 –> 00:13.062
    summarization, knowledge, augmentation of large language models with spoken

    Next steps

    Now that you have your subtitle file, you can configure it in your video player, or if you’re creating a YouTube video, upload it to YouTube Studio. You can also use other tools to bundle or even burn the subtitles into your video.

    Check out our Audio Intelligence models and LeMUR to add even more capabilities to your audio and video applications.

    Alternatively, feel free to check out our blog or YouTube channel for educational content on AI and Machine Learning, or feel free to join us on Twitter or Discord to stay in the loop when we release new content.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleGenerating audio for video
    Next Article How to Create SRT Files for Videos in Node.js

    Related Posts

    Security

    China-Linked Hackers Exploit SAP and SQL Server Flaws in Attacks Across Asia and Brazil

    May 30, 2025
    Security

    New Apache InLong Vulnerability (CVE-2025-27522) Exposes Systems to Remote Code Execution Risks

    May 30, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Figma Config 2024 Recap

    Development

    CVE-2024-8100 – Arista CloudVision Portal – Token Privilege Escalation

    Common Vulnerabilities and Exposures (CVEs)

    AI Hallucinations: What Designers Need to Know

    Web Development

    From Deep Knowledge Tracing to DKT2: A Leap Forward in Educational AI

    Machine Learning

    Highlights

    Databases

    Enforce row-level security with the RDS Data API

    May 15, 2024

    As applications grow to serve more users with sensitive data, implementing robust security controls becomes…

    MOZA built the best steering wheel for Euro Truck Simulator 2 you could ever hope to find and I’m obsessed

    July 4, 2024

    Windows 11 24H2 to get new features in February – what’s coming

    January 7, 2025

    How to Type Object Properties as Strings in TypeScript

    December 24, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.