{
	"name": "YouTube video to notes",
	"settings": {
		"executionOrder": "v1"
	},
	"nodes": [
		{
			"parameters": {
				"pollTimes": {
					"item": [
						{
							"mode": "everyHour"
						}
					]
				},
				"feedUrl": "https://www.youtube.com/feeds/videos.xml?channel_id=UCiHVTkJtWSdc9N3h0nUGWLg"
			},
			"id": "c1d2e3f4-0001-4000-8000-000000000001",
			"name": "New Video",
			"type": "n8n-nodes-base.rssFeedReadTrigger",
			"typeVersion": 1,
			"position": [0, -120]
		},
		{
			"parameters": {
				"formTitle": "YouTube video to notes",
				"formDescription": "Paste a YouTube link. Agent Leverage pulls the transcript and n8n saves a Markdown note with a summary, key moments, and the full transcript.",
				"formFields": {
					"values": [
						{
							"fieldLabel": "YouTube URL",
							"placeholder": "https://www.youtube.com/watch?v=...",
							"requiredField": true
						},
						{
							"fieldLabel": "Caption language",
							"placeholder": "en (leave blank for the video default)",
							"requiredField": false
						},
						{
							"fieldLabel": "If the video has no captions",
							"fieldType": "dropdown",
							"fieldOptions": {
								"values": [
									{
										"option": "Stop"
									},
									{
										"option": "Transcribe the audio"
									}
								]
							},
							"requiredField": true
						}
					]
				},
				"responseMode": "onReceived",
				"options": {
					"respondWithOptions": {
						"values": {
							"formSubmittedText": "Got it. The note lands in your youtube-notes folder in a minute or two."
						}
					}
				}
			},
			"id": "c1d2e3f4-0002-4000-8000-000000000002",
			"name": "Form",
			"type": "n8n-nodes-base.formTrigger",
			"typeVersion": 2.2,
			"position": [0, 120],
			"webhookId": "agentleverage-youtube-notes"
		},
		{
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "a1",
							"name": "url",
							"type": "string",
							"value": "={{ $json.link ?? $json['YouTube URL'] }}"
						},
						{
							"id": "a2",
							"name": "title",
							"type": "string",
							"value": "={{ $json.title ?? '' }}"
						},
						{
							"id": "a3",
							"name": "lang",
							"type": "string",
							"value": "={{ ($json['Caption language'] ?? '').trim() }}"
						},
						{
							"id": "a4",
							"name": "mode",
							"type": "string",
							"value": "={{ $json['If the video has no captions'] === 'Transcribe the audio' ? 'auto' : 'native' }}"
						}
					]
				},
				"options": {}
			},
			"id": "c1d2e3f4-0003-4000-8000-000000000003",
			"name": "Video",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [240, 0]
		},
		{
			"parameters": {
				"url": "https://www.youtube.com/oembed",
				"sendQuery": true,
				"queryParameters": {
					"parameters": [
						{
							"name": "url",
							"value": "={{ $json.url }}"
						},
						{
							"name": "format",
							"value": "json"
						}
					]
				},
				"options": {}
			},
			"id": "c1d2e3f4-0004-4000-8000-000000000004",
			"name": "Video Details",
			"type": "n8n-nodes-base.httpRequest",
			"typeVersion": 4.2,
			"position": [480, 0]
		},
		{
			"parameters": {
				"resource": "youtubeTranscript",
				"operation": "create",
				"url": "={{ $('Video').item.json.url }}",
				"additionalFields": {
					"lang": "={{ $('Video').item.json.lang }}",
					"mode": "={{ $('Video').item.json.mode }}"
				}
			},
			"id": "c1d2e3f4-0005-4000-8000-000000000005",
			"name": "YouTube Transcript",
			"type": "n8n-nodes-agentleverage.agentLeverage",
			"typeVersion": 1,
			"position": [720, 0],
			"credentials": {
				"agentLeverageApi": {
					"id": null,
					"name": "Agent Leverage API"
				}
			}
		},
		{
			"parameters": {
				"resource": "job",
				"operation": "wait",
				"jobId": "={{ $json.job.id }}",
				"pollIntervalMs": 5000,
				"timeoutMs": 600000
			},
			"id": "c1d2e3f4-0006-4000-8000-000000000006",
			"name": "Wait for Transcript",
			"type": "n8n-nodes-agentleverage.agentLeverage",
			"typeVersion": 1,
			"position": [960, 0],
			"credentials": {
				"agentLeverageApi": {
					"id": null,
					"name": "Agent Leverage API"
				}
			}
		},
		{
			"parameters": {
				"jsCode": "// Turn the YouTube Transcript job into a Markdown note.\nconst job = $json.job;\nconst out = job.output ?? {};\nconst video = $('Video').first().json;\nconst details = $('Video Details').first().json;\n\nconst id = out.videoId;\nconst title = details.title || video.title || id;\nconst channel = details.author_name || '';\nconst link = (t) => `https://www.youtube.com/watch?v=${id}&t=${Math.floor(t)}s`;\nconst stamp = (t) => {\n  const h = Math.floor(t / 3600);\n  const m = Math.floor((t % 3600) / 60);\n  const s = Math.floor(t % 60);\n  const mm = String(m).padStart(h ? 2 : 1, '0');\n  return (h ? `${h}:` : '') + `${mm}:${String(s).padStart(2, '0')}`;\n};\n\n// Group caption lines into roughly one-minute paragraphs.\nconst paragraphs = [];\nfor (const s of out.snippets ?? []) {\n  const text = s.text.replace(/\\s+/g, ' ').trim();\n  if (!text) continue;\n  const last = paragraphs[paragraphs.length - 1];\n  if (last && s.start - last.start < 60) last.text += ' ' + text;\n  else paragraphs.push({ start: s.start, text });\n}\n\nconst a = out.analysis ?? {};\nconst words = paragraphs.reduce((n, p) => n + p.text.split(' ').length, 0);\nconst lines = [\n  `# ${title}`,\n  '',\n  `- Video: https://www.youtube.com/watch?v=${id}`,\n  channel ? `- Channel: ${channel}` : null,\n  `- Last caption at: ${stamp(out.durationSeconds ?? 0)}`,\n  `- Transcript language: ${out.language}`,\n  `- Notes made: ${$now.toFormat('yyyy-MM-dd HH:mm')}`,\n  '',\n  '## Summary',\n  '',\n  a.summary ?? '',\n  '',\n  '## Topics',\n  '',\n  ...(a.topics ?? []).map((t) => `- ${t}`),\n  '',\n  '## Key moments',\n  '',\n  ...(a.moments ?? []).map((m) => `- [${stamp(m.start)}](${link(m.start)}) ${m.label}`),\n  '',\n  '## Transcript',\n  '',\n  ...paragraphs.map((p) => `[${stamp(p.start)}](${link(p.start)}) ${p.text}\\n`),\n].filter((l) => l !== null);\n\nconst slug = title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '').slice(0, 60);\n\nreturn [{\n  json: {\n    videoId: id,\n    title,\n    fileName: `${$now.toFormat('yyyy-MM-dd')}-${slug || id}.md`,\n    words,\n    moments: (a.moments ?? []).length,\n    paragraphs: paragraphs.length,\n    notes: lines.join('\\n'),\n  },\n}];"
			},
			"id": "c1d2e3f4-0007-4000-8000-000000000007",
			"name": "Build Notes",
			"type": "n8n-nodes-base.code",
			"typeVersion": 2,
			"position": [1200, 0]
		},
		{
			"parameters": {
				"operation": "toText",
				"sourceProperty": "notes",
				"options": {
					"fileName": "={{ $json.fileName }}"
				}
			},
			"id": "c1d2e3f4-0008-4000-8000-000000000008",
			"name": "Notes File",
			"type": "n8n-nodes-base.convertToFile",
			"typeVersion": 1.1,
			"position": [1680, -100]
		},
		{
			"parameters": {
				"operation": "write",
				"fileName": "=/home/node/.n8n-files/youtube-notes/{{ $('Build Notes').item.json.fileName }}",
				"options": {}
			},
			"id": "c1d2e3f4-0009-4000-8000-000000000009",
			"name": "Save Notes",
			"type": "n8n-nodes-base.readWriteFile",
			"typeVersion": 1,
			"position": [1920, -100]
		},
		{
			"parameters": {
				"conditions": {
					"options": {
						"caseSensitive": true,
						"leftValue": "",
						"typeValidation": "strict",
						"version": 2
					},
					"conditions": [
						{
							"id": "w1",
							"leftValue": "={{ $json.words }}",
							"rightValue": 50,
							"operator": {
								"type": "number",
								"operation": "gte"
							}
						}
					],
					"combinator": "and"
				},
				"options": {}
			},
			"id": "c1d2e3f4-0010-4000-8000-000000000010",
			"name": "Enough Speech?",
			"type": "n8n-nodes-base.if",
			"typeVersion": 2.2,
			"position": [1440, 0]
		},
		{
			"parameters": {},
			"id": "c1d2e3f4-0011-4000-8000-000000000011",
			"name": "Skip Note",
			"type": "n8n-nodes-base.noOp",
			"typeVersion": 1,
			"position": [1680, 100]
		}
	],
	"connections": {
		"New Video": {
			"main": [
				[
					{
						"node": "Video",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Form": {
			"main": [
				[
					{
						"node": "Video",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Video": {
			"main": [
				[
					{
						"node": "Video Details",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Video Details": {
			"main": [
				[
					{
						"node": "YouTube Transcript",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"YouTube Transcript": {
			"main": [
				[
					{
						"node": "Wait for Transcript",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Wait for Transcript": {
			"main": [
				[
					{
						"node": "Build Notes",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Build Notes": {
			"main": [
				[
					{
						"node": "Enough Speech?",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Enough Speech?": {
			"main": [
				[
					{
						"node": "Notes File",
						"type": "main",
						"index": 0
					}
				],
				[
					{
						"node": "Skip Note",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Notes File": {
			"main": [
				[
					{
						"node": "Save Notes",
						"type": "main",
						"index": 0
					}
				]
			]
		}
	},
	"pinData": {},
	"meta": {
		"templateCredsSetupCompleted": false
	}
}
