8
n8n 中文网amn8n.com

使用 YouTube Transcript API 提取 YouTube 视频字幕

中级

这是一个Market Research, Miscellaneous, Multimodal AI领域的自动化工作流,包含 13 个节点。主要使用 Code, Webhook, HttpRequest, GoogleSheets, RespondToWebhook 等节点。 通过Google Sheets或API Webhook提取YouTube字幕

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
  • 可能需要目标 API 的认证凭证
  • Google Sheets API 凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "name": "使用 YouTube Transcript API 提取 YouTube 视频字幕",
  "nodes": [
    {
      "id": "main-explanation-note",
      "name": "主模板说明",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        200,
        -200
      ],
      "parameters": {
        "color": 7,
        "width": 700,
        "height": 800,
        "content": "# YouTube 字幕提取器"
      },
      "typeVersion": 1
    },
    {
      "id": "sheets-workflow-note",
      "name": "Sheets 工作流说明",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        512,
        -128
      ],
      "parameters": {
        "color": 3,
        "width": 600,
        "height": 100,
        "content": "## Google Sheets 自动化处理"
      },
      "typeVersion": 1
    },
    {
      "id": "webhook-workflow-note",
      "name": "Webhook 工作流说明",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        512,
        320
      ],
      "parameters": {
        "color": 4,
        "width": 600,
        "height": 100,
        "content": "## Webhook 直接处理"
      },
      "typeVersion": 1
    },
    {
      "id": "sheets-trigger",
      "name": "监控 Google 表格中的 URL",
      "type": "n8n-nodes-base.googleSheetsTrigger",
      "position": [
        528,
        32
      ],
      "parameters": {
        "event": "rowAdded",
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "urls",
          "cachedResultName": "urls"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "YOUR_GOOGLE_SHEET_ID",
          "cachedResultName": "Your YouTube Transcript Sheet"
        }
      },
      "credentials": {
        "googleSheetsTriggerOAuth2Api": {
          "id": "google-sheets-credentials",
          "name": "Google Sheets API"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "extract-video-id-sheets",
      "name": "提取 YouTube 视频 ID (Sheets)",
      "type": "n8n-nodes-base.code",
      "position": [
        768,
        32
      ],
      "parameters": {
        "jsCode": "// Get the URL from n8n input\nconst url = $input.first().json.url;\n\n// Function to extract YouTube video ID\nfunction getVideoId(url) {\n  if (!url) return null;\n  const match = url.match(/(?:v=|youtu\\.be\\/|embed\\/)([^&\\n?#]+)/);\n  return match ? match[1] : null;\n}\n\n// Extract the video ID\nconst videoId = getVideoId(url);\n\n// Return the result for n8n\nreturn {\n  json: {\n    url: url,\n    videoId: videoId,\n    success: videoId ? true : false\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "fetch-transcript-sheets",
      "name": "获取视频字幕数据 (Sheets)",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1008,
        32
      ],
      "parameters": {
        "url": "https://www.youtube-transcript.io/api/transcripts",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"ids\": [\n    \"{{ $json.videoId }}\"\n  ]\n} ",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ $credentials.youtubeTranscriptApi.token }}"
            }
          ]
        }
      },
      "credentials": {
        "youtubeTranscriptApi": {
          "id": "youtube-transcript-api",
          "name": "Dummy YouTube Transcript API"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "parse-transcript-sheets",
      "name": "解析字幕文本 (Sheets)",
      "type": "n8n-nodes-base.code",
      "position": [
        1232,
        32
      ],
      "parameters": {
        "jsCode": "const data = $input.first().json;\n\nreturn {\n  json: {\n    fullTranscript: data.tracks[0].transcript.map(segment => segment.text).join(' ')\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "save-to-sheet",
      "name": "保存字幕到表格",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1440,
        32
      ],
      "parameters": {
        "columns": {
          "value": {
            "transcript": "={{ $json.fullTranscript }}",
            "video title": "={{ $('Fetch Video Transcript Data (Sheets)').item.json.microformat.playerMicroformatRenderer.title.simpleText }}"
          },
          "schema": [
            {
              "id": "video title",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "video title",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "transcript",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "transcript",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "transcripts",
          "cachedResultName": "transcripts"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "YOUR_GOOGLE_SHEET_ID",
          "cachedResultName": "Your YouTube Transcript Sheet"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "google-sheets-credentials",
          "name": "Google Sheets API"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "webhook-trigger",
      "name": "Webhook 触发器(直接输入)",
      "type": "n8n-nodes-base.webhook",
      "position": [
        512,
        464
      ],
      "webhookId": "extract-youtube-transcript",
      "parameters": {
        "path": "extract-youtube-transcript",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2.1
    },
    {
      "id": "extract-video-id-webhook",
      "name": "提取 YouTube 视频 ID(Webhook)",
      "type": "n8n-nodes-base.code",
      "position": [
        752,
        464
      ],
      "parameters": {
        "jsCode": "// Get the video_url from n8n input\nconst url = $input.first().json.body.video_url;\n\n// Function to extract YouTube video ID\nfunction getVideoId(url) {\n  if (!url) return null;\n  const match = url.match(/(?:v=|youtu\\.be\\/|embed\\/)([^&\\n?#]+)/);\n  return match ? match[1] : null;\n}\n\n// Extract the video ID\nconst videoId = getVideoId(url);\n\n// Return the result for n8n\nreturn {\n  json: {\n    url: url,\n    videoId: videoId,\n    success: !!videoId\n  }\n};\n"
      },
      "typeVersion": 2
    },
    {
      "id": "fetch-transcript-webhook",
      "name": "获取视频字幕数据 (Webhook)",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1008,
        464
      ],
      "parameters": {
        "url": "https://www.youtube-transcript.io/api/transcripts",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"ids\": [\n    \"{{ $json.videoId }}\"\n  ]\n} ",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ $credentials.youtubeTranscriptApi.token }}"
            }
          ]
        }
      },
      "credentials": {
        "youtubeTranscriptApi": {
          "id": "youtube-transcript-api",
          "name": "Dummy YouTube Transcript API"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "parse-transcript-webhook",
      "name": "解析字幕文本 (Webhook)",
      "type": "n8n-nodes-base.code",
      "position": [
        1264,
        464
      ],
      "parameters": {
        "jsCode": "const data = $input.first().json;\n\nlet fullTranscript = null;\n\n// ✅ If transcript exists in POST response\nif (data.tracks && data.tracks.length > 0) {\n  fullTranscript = data.tracks[0].transcript\n    .map(segment => segment.text)\n    .join(' ');\n}\n\n// ✅ Fallback if transcript missing\nif (!fullTranscript && data.microformat?.playerMicroformatRenderer?.description?.simpleText) {\n  fullTranscript = data.microformat.playerMicroformatRenderer.description.simpleText;\n}\n\nreturn {\n  json: {\n    id: data.id,\n    title: data.title,\n    channel: data.microformat?.playerMicroformatRenderer?.ownerChannelName,\n    publishDate: data.microformat?.playerMicroformatRenderer?.publishDate,\n    duration: data.microformat?.playerMicroformatRenderer?.lengthSeconds,\n    category: data.microformat?.playerMicroformatRenderer?.category,\n    fullTranscript,\n    hasTranscript: (data.tracks?.length ?? 0) > 0\n  }\n};\n\n"
      },
      "typeVersion": 2
    },
    {
      "id": "return-transcript",
      "name": "返回字幕响应",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1488,
        464
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={\n  \"success\": true,\n  \"video\": {\n    \"id\": \"{{ $json.id }}\",\n    \"title\": \"{{ $json.title }}\",\n    \"channel\": \"{{ $json.channel }}\",\n    \"duration\": \"{{ $json.duration }}\",\n    \"hasTranscript\": {{ $json.hasTranscript }}\n  },\n  \"transcript\": \"{{ $json.fullTranscript }}\"\n}"
      },
      "typeVersion": 1.4
    }
  ],
  "connections": {
    "Monitor Google Sheet for URLs": {
      "main": [
        [
          {
            "node": "Extract YouTube Video ID (Sheets)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Transcript Text (Sheets)": {
      "main": [
        [
          {
            "node": "Save Transcript to Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook Trigger (Direct Input)": {
      "main": [
        [
          {
            "node": "Extract YouTube Video ID (Webhook)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Transcript Text (Webhook)": {
      "main": [
        [
          {
            "node": "Return Transcript Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract YouTube Video ID (Sheets)": {
      "main": [
        [
          {
            "node": "Fetch Video Transcript Data (Sheets)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract YouTube Video ID (Webhook)": {
      "main": [
        [
          {
            "node": "Fetch Video Transcript Data (Webhook)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Video Transcript Data (Sheets)": {
      "main": [
        [
          {
            "node": "Parse Transcript Text (Sheets)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Video Transcript Data (Webhook)": {
      "main": [
        [
          {
            "node": "Parse Transcript Text (Webhook)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 市场调研, 杂项, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量13
分类3
节点类型7
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Dahiana

Dahiana

@mssporto

No-Code Specialist with more than 10 years of experience in Digital Marketing. Currently working with Bubble. Webflow, AI, Agents and N8N.

外部链接
在 n8n.io 查看

分享此工作流