# Step-by-Step Guide to Transcribing YouTube Videos and Creating Subtitles with Node.js
In today’s digital age, video content is king. However, to make your videos accessible to a broader audience, including those who are deaf or hard of hearing, or those who speak different languages, adding subtitles is essential. This guide will walk you through the process of transcribing YouTube videos and creating subtitles using Node.js.
## Prerequisites
Before we dive into the steps, ensure you have the following:
1. **Node.js**: Make sure you have Node.js installed on your machine. You can download it from [Node.js official website](https://nodejs.org/).
2. **YouTube API Key**: You will need a YouTube Data API key. You can get one by creating a project in the [Google Developers Console](https://console.developers.google.com/).
3. **FFmpeg**: This is a multimedia framework that can decode, encode, transcode, and stream audio and video. Download it from [FFmpeg official website](https://ffmpeg.org/).
## Step 1: Setting Up Your Project
First, create a new directory for your project and initialize it with npm:
“`bash
mkdir youtube-transcriber
cd youtube-transcriber
npm init -y
“`
Next, install the necessary packages:
“`bash
npm install axios fluent-ffmpeg googleapis node-fetch
“`
## Step 2: Downloading the YouTube Video
To download the video, we will use the `ytdl-core` package. Install it using npm:
“`bash
npm install ytdl-core
“`
Create a file named `downloadVideo.js` and add the following code:
“`javascript
const ytdl = require(‘ytdl-core’);
const fs = require(‘fs’);
async function downloadVideo(videoUrl) {
const info = await ytdl.getInfo(videoUrl);
const title = info.videoDetails.title.replace(/[^a-zA-Z0-9]/g, ‘_’);
const output = `${title}.mp4`;
ytdl(videoUrl)
.pipe(fs.createWriteStream(output))
.on(‘finish’, () => {
console.log(`Downloaded ${output}`);
});
}
const videoUrl = ‘YOUR_YOUTUBE_VIDEO_URL’;
downloadVideo(videoUrl);
“`
Replace `YOUR_YOUTUBE_VIDEO_URL` with the URL of the YouTube video you want to transcribe.
## Step 3: Extracting Audio from the Video
Create a file named `extractAudio.js` and add the following code:
“`javascript
const ffmpeg = require(‘fluent-ffmpeg’);
function extractAudio(videoFile) {
const audioFile = videoFile.replace(‘.mp4’, ‘.mp3’);
ffmpeg(videoFile)
.output(audioFile)
.on(‘end’, () => {
console.log(`Extracted audio to ${audioFile}`);
})
.run();
}
const videoFile = ‘YOUR_DOWNLOADED_VIDEO_FILE.mp4’;
extractAudio(videoFile);
“`
Replace `YOUR_DOWNLOADED_VIDEO_FILE.mp4` with the name of the downloaded video file.
## Step 4: Transcribing the Audio
For transcription, we will use Google Cloud Speech-to-Text API. First, set up your Google Cloud project and enable the Speech-to-Text API. Then, download your service account key and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to your key file.
Install the Google Cloud Speech package:
“`bash
npm install @google-cloud/speech
“`
Create a file named `transcribeAudio.js` and add the following code:
“`javascript
const fs = require(‘fs’);
const speech = require(‘@google-cloud/speech’);
async function transcribeAudio(audioFile) {
const client = new speech.SpeechClient();
const file = fs.readFileSync(audioFile);
const audioBytes = file.toString(‘base64’);
const audio = {
content: audioBytes,
};
const config = {
encoding: ‘MP3’,
sampleRateHertz: 16000,
languageCode: ‘en-US’,
};
const request = {
audio: audio,
config: config,
};
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join(‘n’);
console.log(`Transcription: ${transcription}`);
}
const audioFile = ‘YOUR_EXTRACTED_AUDIO_FILE.mp3’;
transcribeAudio(audioFile);
“`
Replace `YOUR_EXTRACTED_AUDIO_FILE.mp3` with the name of the extracted audio file.
## Step 5: Creating Subtitles
To create subtitles, we will convert the transcription into SRT format.