You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用NodeJS从YouTube Studio下载视频?以及如何借助YouTube API v3自动检测并下载自有频道已处理完成的直播回放?

Automating Download of Your Own YouTube Live Replay with Node.js

Absolutely, you can build this automated workflow for your own channel's live replay using Node.js. Let's break this down into two core parts: checking when the video finishes processing, and triggering the download once it's ready.

1. Check Video Processing Status with YouTube Data API v3

First, you'll use the YouTube Data API to poll the video's status until it's marked as fully processed. Since this is your own channel, you'll need proper authentication to access the video details.

Setup Steps:

  • Create a project in Google Cloud Console, enable the YouTube Data API v3, and generate OAuth2 credentials (or use a service account linked to your channel if you prefer server-to-server access).
  • Install the official Google API client for Node.js:
    npm install googleapis
    

Example Code for Status Polling:

const { google } = require('googleapis');
const youtube = google.youtube('v3');

// Configure OAuth2 client (replace with your credentials)
const oauth2Client = new google.auth.OAuth2(
  'YOUR_CLIENT_ID',
  'YOUR_CLIENT_SECRET',
  'YOUR_REDIRECT_URI'
);
// Set credentials (you'll need to handle the OAuth flow to get access tokens)
oauth2Client.setCredentials({ access_token: 'YOUR_ACCESS_TOKEN', refresh_token: 'YOUR_REFRESH_TOKEN' });

const VIDEO_ID = 'YOUR_LIVE_REPLAY_VIDEO_ID';
const CHECK_INTERVAL = 300000; // 5 minutes in milliseconds

const checkVideoStatus = async () => {
  try {
    const response = await youtube.videos.list({
      auth: oauth2Client,
      id: VIDEO_ID,
      part: 'status,contentDetails'
    });

    const video = response.data.items[0];
    if (!video) {
      console.log('Video not found');
      return;
    }

    const uploadStatus = video.status.uploadStatus;
    console.log(`Current video status: ${uploadStatus}`);

    if (uploadStatus === 'processed') {
      console.log('Video is ready for download!');
      clearInterval(pollingInterval); // Stop polling
      await downloadVideo(VIDEO_ID); // Trigger download
    }
  } catch (err) {
    console.error('Error checking video status:', err);
  }
};

// Start polling
const pollingInterval = setInterval(checkVideoStatus, CHECK_INTERVAL);
// Run immediately on startup
checkVideoStatus();

Key note: The uploadStatus field will be uploaded while processing, and switch to processed once YouTube finishes handling the replay. Avoid polling too frequently to stay within API rate limits (the default quota is 10,000 units per day, and each videos.list call uses 1 unit).

2. Download the Ready Video

Since this is your own channel's content, you can safely use yt-dlp (a powerful tool for downloading YouTube videos) via Node.js. It will handle authentication if you're signed in, or you can pass your OAuth credentials to it.

Setup Steps:

  • Install yt-dlp on your system (follow the official docs for your OS), or use the Node.js wrapper:
    npm install yt-dlp-exec
    

Example Download Function:

const ytDlp = require('yt-dlp-exec');

const downloadVideo = async (videoId) => {
  try {
    await ytDlp(`https://www.youtube.com/watch?v=${videoId}`, {
      output: '%(title)s.%(ext)s', // Save with video title
      // If using OAuth, pass your access token:
      // headers: { Authorization: `Bearer YOUR_ACCESS_TOKEN` }
    });
    console.log('Video downloaded successfully!');
  } catch (err) {
    console.error('Error downloading video:', err);
  }
};

If you prefer not to use yt-dlp, you could also use the YouTube Data API to check if the download is available in YouTube Studio, but yt-dlp simplifies the process significantly and works reliably once the video is processed.

Important Notes

  • Terms Compliance: Since you're downloading your own content, this aligns with YouTube's Terms of Service—just ensure you're not distributing the content in ways that violate your channel's agreements.
  • Authentication: Make sure your OAuth credentials have the https://www.googleapis.com/auth/youtube.readonly scope to access video status details.
  • Error Handling: Add extra checks (like handling token expiration, network errors) to make the script more robust for long-running polls.

内容的提问来源于stack exchange,提问作者TXP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 16:07:29