DEV Community

Cover image for Transcribe Videos With Node.js
Kevin Lewis for Deepgram

Posted on • Originally published at developers.deepgram.com

Transcribe Videos With Node.js

Whether your video is hosted online or on your machine, for accessibility or analysis, Deepgram can provide accurate transcriptions in just a few lines of code.

I'm glad you're here, but I must confess that I am leading you down a path slightly different to what you would expect. Instead of transcribing video directly, this post will cover converting video files to audio files and then sending them to Deepgram. First, we will transcribe local files, and then we will download files programatically before transcribing them.

Before We Start

You will need:

Create a new directory and navigate to it with your terminal. Run npm init -y to create a package.json file and then install the following packages:

npm install @deepgram/sdk ffmpeg-static
Enter fullscreen mode Exit fullscreen mode

Create an index.js file, and open it in your code editor.

Preparing Dependencies

At the top of your file require these packages:

const fs = require('fs')
const https = require('https')
const { execSync: exec } = require('child_process')
const { Deepgram } = require('@deepgram/sdk')
const ffmpegStatic = require('ffmpeg-static')
Enter fullscreen mode Exit fullscreen mode

fs is the built-in file system module for Node.js. It is used to read and write files which you will be doing a few times throughout this post. ffmpeg-static includes a version of ffmpeg in our node_modules directory, and requiring it returns the file path.

Initialize the Deepgram client:

const deepgram = new Deepgram('YOUR DEEPGRAM KEY')
Enter fullscreen mode Exit fullscreen mode

Running ffmpeg Commands

ffmpeg is a toolkit for developers to work with audio and video files - which includes conversion between formats. It's used most commonly in a terminal, so below is a utility function to add to your index.js file. It allows us to fire off terminal commands directly from our Node.js application:

async function ffmpeg(command) {
  return new Promise((resolve, reject) => {
    exec(`${ffmpegStatic} ${command}`, (err, stderr, stdout) => {
      if (err) reject(err)
      resolve(stdout)
    })
  })
}
Enter fullscreen mode Exit fullscreen mode

Transcribing Local Video

This function will convert and transcribe local video files:

async function transcribeLocalVideo(filePath) {
  ffmpeg(`-hide_banner -y -i ${filePath} ${filePath}.wav`)

  const audioFile = {
    buffer: fs.readFileSync(`${filePath}.wav`),
    mimetype: 'audio/wav',
  }
  const response = await deepgram.transcription.preRecorded(audioFile, {
    punctuation: true,
  })
  return response.results
}

transcribeLocalVideo('deepgram.mp4')
Enter fullscreen mode Exit fullscreen mode

ffmpeg(`-hide_banner -y -i ${filePath} ${filePath}.wav`) takes in the provided file, and converts it to a .wav audio file. -hide_banner reduces the amount of information printed in the terminal and-y will overwrite an existing file (useful for development).

Save and run the file in your terminal with node index.js and you should see transcripts appear.

Transcribing Remote Video

Add this utility to the bottom of your file:

async function downloadFile(url) {
  return new Promise((resolve, reject) => {
    const request = https.get(url, (response) => {
      const fileName = url.split('/').slice(-1)[0] // Get the final part of the URL only
      const fileStream = fs.createWriteStream(fileName)
      response.pipe(fileStream)
      response.on('end', () => {
        fileStream.close()
        resolve(fileName)
      })
    })
  })
}
Enter fullscreen mode Exit fullscreen mode

This allows us to download a file to our machine. The file name will be derived from the last part of the URL - for example https://example.com/directory/directory2/file.mp4 becomes file.mp4 locally.

With this in place, we first download the video and then use our existing transcribeLocalVideo() function:

async function transcribeRemoteVideo(url) {
  const filePath = await downloadFile(url)
  const transcript = await transcribeLocalVideo(filePath)
}
Enter fullscreen mode Exit fullscreen mode

The complete project is available at https://github.com/deepgram-devs/transcribe-videos and if you have any questions please feel free to reach out on Twitter - we're @DeepgramDevs.

Top comments (0)