Implementing a Google Drive Video Downloader in TypeScript
Implementing Google Drive Downloader
Welcome to the first lesson in this course! In this unit, you'll build a system that allows users to preview, download, and transcribe videos directly from Google Drive URLs.
Heads-up:
Support for LinkedIn video downloads is also present in the codebase via a separate service. You’re welcome to try it now — but we’ll explore it in depth in Unit 2.
Overview
Here’s what you’ll accomplish in this unit:
Extract Google Drive File ID from URLs
Preview videos in-browser before downloading
Download the video using curl
Load and display the downloaded video for playback
Transcribe the first 30 seconds of audio using Whisper
Understanding Google Drive URL Formats
Google Drive URLs typically follow one of two structures:
Direct file path https://drive.google.com/file/d/{fileid}/view
Open ID parameter https://drive.google.com/open?id={fileid}
Both of these formats are supported by the application.
The getDriveFileId() function checks for both patterns:
TypeScript
function getDriveFileId(url) { // Match format: https://drive.google.com/file/d/FILEID/view const fileIdMatch = url.match(/\/file\/d\/([^/]+)/); // Match format: https://drive.google.com/open?id=FILEID const openIdMatch = url.match(/open\?id=([^&]+)/); return fileIdMatch?.[1] || openIdMatch?.[1] || null;}
This approach ensures maximum compatibility with typical Google Drive sharing links.
Previewing the Video (before Download)
public/index.html
HTML
<div id="previewFrame" class="flex-grow w-full relative overflow-hidden"> <!-- iframe or video will be injected here --> </div>
When the user clicks "Load", the video URL is posted to /process-url, which in turn invokes the backend downloader service.
Backend Route: /process-url
The /process-url endpoint acts as a gateway to downloading supported video types.
src/routes/processUrl.ts
router.post('/', async (req, res) => { const { url } = req.body; // If no URL is provided, return a 400 Bad Request if (!url) { return res.status(400).json({ error: 'Please provide a URL' }); } try { let result; // Check if the URL is a Google Drive link if (url.includes('drive.google.com')) { // Download the video and extract metadata result = await downloadGoogleDriveVideo(url); } else { // Reject unsupported platforms return res.status(400).json({ error: 'Unsupported URL. Only Google Drive URLs are supported.' }); } // Respond with filename and video duration res.json(result); } catch (err) { console.error('Download error:', err); res.status(500).json({ error: err.message || 'Failed to download video' }); }});
This route handles the logic to detect the video platform and invokes the corresponding Google Drive download service. If the platform is unsupported, a 400 error is returned. It validates the input, determines the platform, and calls the appropriate service. In this lesson, only Drive links are supported — LinkedIn is covered later in Unit 2.
Note: The video download is not a separate manual step—the /process-url route immediately downloads the video as part of its internal flow. The downloadGoogleDriveVideo() function handles this behind the scenes and also extracts the video's duration so it’s ready for transcription later.
The Google Drive Video Download Service
src/services/googleDriveService.ts
The service below handles the entire process of downloading a video from Google Drive and probing it for metadata (like duration). Let’s walk through the logic step by step.
router.post('/', async (req, res) => { const { url } = req.body; // If no URL is provided, return a 400 Bad Request if (!url) { return res.status(400).json({ error: 'Please provide a URL' }); } try { let result; // Check if the URL is a Google Drive link if (url.includes('drive.google.com')) { // Download the video and extract metadata result = await downloadGoogleDriveVideo(url); } else { // Reject unsupported platforms return res.status(400).json({ error: 'Unsupported URL. Only Google Drive URLs are supported.' }); } // Respond with filename and video duration res.json(result); } catch (err) { console.error('Download error:', err); res.status(500).json({ error: err.message || 'Failed to download video' }); }});
Once a video is loaded, the frontend sends the file path to /transcribe, which performs the following steps:
Checks if the video file exists
Uses FFmpeg to clip the first 30 seconds of audio
Converts the audio to mono 16kHz format
Sends it to Whisper for transcription
Deletes the temporary audio file
src/routes/transcribe.ts
TypeScript
router.post('/', (req, res) => { const { file_path } = req.body; // Validate input if (!file_path) { return res.status(400).json({ error: 'Missing file_path' }); } // Build the absolute path to the downloaded video const fullPath = path.join(process.cwd(), 'downloads', file_path); if (!fs.existsSync(fullPath)) { return res.status(404).json({ error: 'Video file not found' }); } // Construct output path for clipped audio (mp3 format) const clipPath = fullPath.replace(/\.[^.]+$/, '.clip.mp3'); const ffmpegCmd = `ffmpeg -y -ss 0 -t 30 -i "${fullPath}" -ac 1 -ar 16000 "${clipPath}"`; // Use ffmpeg to clip the first 30 seconds of audio exec(ffmpegCmd, async (err) => { if (err) { return res.status(500).json({ error: 'Audio clipping failed' }); } try { // Transcribe the audio file using Whisper const transcription = await transcribe(clipPath); // Delete the temporary audio file fs.unlink(clipPath, () => {}); res.json({ transcription }); } catch (e) { res.status(500).json({ error: 'Transcription failed' }); } });});
This endpoint clips the first 30 seconds of the downloaded video, prepares the audio in the correct format, and sends it to Whisper for transcription. Once done, it cleans up the temporary .mp3 file and returns the transcription to the frontend.