id: youtube-video-repurposing-pack
namespace: company.team
description: |
Turn every new YouTube upload into a ready-to-review content pack. Wait for
the video's captions, distill the transcript into a content brief with an
LLM, then draft a YouTube description with chapters, an X thread, a LinkedIn
post and a newsletter section in parallel. The drafts land in Slack, and the
whole pack is saved as a Markdown file on the execution.
triggers:
- id: on_new_upload
type: io.kestra.plugin.youtube.VideoTrigger
description: Poll your channel every 5 minutes and start an execution when a new
video is published, including unlisted ones. Set your channel ID, then
enable it.
accessToken: "{{ kv('YOUTUBE_ACCESS_TOKEN') }}"
channelId: UC_your_channel_id
interval: PT5M
maxResults: 5
disabled: true
inputs:
- id: video_id
type: STRING
displayName: Video ID
description: For manual runs and backfills, the ID of one of your own videos
(the part after `v=` in the URL). Uploads detected by the trigger ignore
this.
required: false
- id: brand_voice
type: STRING
displayName: Brand voice
description: How the drafts should sound. Passed to every writing prompt.
defaults: Friendly and practical, plain language, no hype, no emojis.
- id: apply_to_youtube
type: BOOL
displayName: Write description to YouTube
description: After you approve in Kestra, replace the video's description with
the generated one, which adds the chapters. Title, category, tags and
language are kept. Off by default.
defaults: false
variables:
claude_model: claude-opus-5-5
formats:
seo_description: YouTube description with chapters
x_thread: X thread
linkedin_post: LinkedIn post
newsletter: Newsletter section
tasks:
- id: dedupe_trigger_runs
type: io.kestra.plugin.core.flow.If
description: Each poll looks back over the interval plus 5 minutes, so the same
upload is seen by two polls. Triggered runs claim the video ID in KV and
stop if it was already claimed. Manual runs skip this check, so a video
can always be reprocessed by hand.
condition: "{{ trigger.videoId ?? '' }}"
then:
- id: skip_if_claimed
type: io.kestra.plugin.core.flow.If
description: Stop if an earlier execution already took this upload.
condition: "{{ kv('yt_pack_' ~ trigger.videoId, errorOnMissing=false) is not
null }}"
then:
- id: already_processed
type: io.kestra.plugin.core.execution.Exit
description: End quietly, another execution is handling this upload.
state: SUCCESS
- id: claim_video
type: io.kestra.plugin.core.kv.Set
description: Claim the upload before the caption wait, which can take hours, so
later polls skip it.
key: "yt_pack_{{ trigger.videoId }}"
value: "{{ execution.id }}"
ttl: P30D
- id: get_token
type: io.kestra.plugin.youtube.OAuth2
description: Exchange the refresh token for a short-lived access token used by
the API calls below.
clientId: "{{ secret('YOUTUBE_CLIENT_ID') }}"
clientSecret: "{{ secret('YOUTUBE_CLIENT_SECRET') }}"
refreshToken: "{{ secret('YOUTUBE_REFRESH_TOKEN') }}"
- id: video_details
type: io.kestra.plugin.youtube.VideoStats
description: Read the video's title, description and duration. Uploads from the
trigger take precedence over the `video_id` input.
accessToken: "{{ outputs.get_token.accessToken }}"
videoIds:
- "{{ trigger.videoId ?? inputs.video_id }}"
includeSnippet: true
includeContentDetails: true
- id: wait_for_captions
type: io.kestra.plugin.core.flow.LoopUntil
description: Auto-generated captions can take minutes to hours after upload.
Check every 15 minutes for up to 6 hours, refreshing the token on each
check.
condition: "{{ fromJson(outputs.check_captions.body).items | length > 0 }}"
failOnMaxReached: true
checkFrequency:
interval: PT15M
maxDuration: PT6H
tasks:
- id: loop_token
type: io.kestra.plugin.youtube.OAuth2
description: Fresh access token for this check, since the wait can outlive a
one-hour token.
clientId: "{{ secret('YOUTUBE_CLIENT_ID') }}"
clientSecret: "{{ secret('YOUTUBE_CLIENT_SECRET') }}"
refreshToken: "{{ secret('YOUTUBE_REFRESH_TOKEN') }}"
- id: check_captions
type: io.kestra.plugin.core.http.Request
description: List the caption tracks available for the video.
uri: "https://www.googleapis.com/youtube/v3/captions?part=snippet&videoId={{
outputs.video_details.videos[0].videoId }}"
headers:
Authorization: "Bearer {{ outputs.loop_token.accessToken }}"
- id: caption_token
type: io.kestra.plugin.youtube.OAuth2
description: Fresh access token for the caption download.
clientId: "{{ secret('YOUTUBE_CLIENT_ID') }}"
clientSecret: "{{ secret('YOUTUBE_CLIENT_SECRET') }}"
refreshToken: "{{ secret('YOUTUBE_REFRESH_TOKEN') }}"
- id: list_captions
type: io.kestra.plugin.core.http.Request
description: List the caption tracks again so the download uses the final set of tracks.
uri: "https://www.googleapis.com/youtube/v3/captions?part=snippet&videoId={{
outputs.video_details.videos[0].videoId }}"
headers:
Authorization: "Bearer {{ outputs.caption_token.accessToken }}"
- id: download_transcript
type: io.kestra.plugin.core.http.Download
description: Download the captions as SRT, preferring a human-made track over
the auto-generated one. Only works for videos on your own channel.
uri: "https://www.googleapis.com/youtube/v3/captions/{{
outputs.list_captions.body | jq('.items | sort_by(.snippet.trackKind ==
\"asr\") | .[0].id') | first }}?tfmt=srt"
headers:
Authorization: "Bearer {{ outputs.caption_token.accessToken }}"
- id: content_brief
type: io.kestra.plugin.ai.completion.ChatCompletion
description: Distill the transcript into a factual brief with summary, key
points, chapters and quotes. Every draft is written from this brief, so
the transcript is only sent to the model once.
provider:
type: io.kestra.plugin.ai.provider.Anthropic
apiKey: "{{ secret('ANTHROPIC_API_KEY') }}"
modelName: "{{ vars.claude_model }}"
configuration:
maxToken: 16000
messages:
- type: SYSTEM
content: |
You analyze video transcripts for a content team. Use only what is said in the transcript. Never invent facts, numbers, names or links.
- type: USER
content: |
Video title: {{ outputs.video_details.videos[0].title }}
Video description: {{ outputs.video_details.videos[0].description }}
Write a content brief in Markdown with these sections:
## Summary
One paragraph of 3 to 4 sentences.
## Key points
5 to 8 bullets, each one concrete takeaway.
## Chapters
3 to 10 lines in the form `MM:SS Title` (use `H:MM:SS` for videos over an hour). The first chapter must be `00:00`. Take the timestamps from the transcript, keep titles under 50 characters, and space chapters at least 30 seconds apart. Write them as plain lines, not inside a code block.
## Quotes
3 short lines quoted verbatim from the transcript.
## Keywords
5 to 8 search keywords, comma-separated.
Transcript (SRT):
{{ read(outputs.download_transcript.uri) }}
- id: drafts
type: io.kestra.plugin.core.flow.Parallel
description: Write the four drafts at the same time, each from the same brief.
tasks:
- id: seo_description
type: io.kestra.plugin.ai.completion.ChatCompletion
description: YouTube description with a hook, summary, chapters and hashtags,
ready to paste into YouTube Studio.
provider:
type: io.kestra.plugin.ai.provider.Anthropic
apiKey: "{{ secret('ANTHROPIC_API_KEY') }}"
modelName: "{{ vars.claude_model }}"
configuration:
maxToken: 16000
messages:
- type: SYSTEM
content: "You write YouTube descriptions in the first person, as the creator of
the video. Voice: {{ inputs.brand_voice }}"
- type: USER
content: |
Write the description for the video "{{ outputs.video_details.videos[0].title }}".
Structure: a two-line hook (it shows above the fold), a short summary paragraph, the chapters copied exactly from the brief under a "Chapters" heading, then 3 hashtags.
Return only the description text, no commentary.
Brief:
{{ outputs.content_brief.textOutput }}
- id: x_thread
type: io.kestra.plugin.ai.completion.ChatCompletion
description: X thread of 5 to 7 posts with the video link in the last post.
provider:
type: io.kestra.plugin.ai.provider.Anthropic
apiKey: "{{ secret('ANTHROPIC_API_KEY') }}"
modelName: "{{ vars.claude_model }}"
configuration:
maxToken: 16000
messages:
- type: SYSTEM
content: "You write X threads in the first person, as the creator of the video
promoting their own work. Never refer to \"the creator\". Voice:
{{ inputs.brand_voice }}"
- type: USER
content: |
Write a thread of 5 to 7 posts about this video. Number them `1/`, `2/` and so on, keep each post under 270 characters, open with the strongest takeaway, and end the last post with https://www.youtube.com/watch?v={{ outputs.video_details.videos[0].videoId }}
Return only the thread.
Brief:
{{ outputs.content_brief.textOutput }}
- id: linkedin_post
type: io.kestra.plugin.ai.completion.ChatCompletion
description: LinkedIn post of 150 to 250 words with the link at the end.
provider:
type: io.kestra.plugin.ai.provider.Anthropic
apiKey: "{{ secret('ANTHROPIC_API_KEY') }}"
modelName: "{{ vars.claude_model }}"
configuration:
maxToken: 16000
messages:
- type: SYSTEM
content: "You write LinkedIn posts in the first person, as the creator of the
video sharing their own work. Never refer to \"the creator\".
Voice: {{ inputs.brand_voice }}"
- type: USER
content: |
Write a LinkedIn post of 150 to 250 words about this video. Start with a one-line hook, use short paragraphs, include at most 3 hashtags, and finish with https://www.youtube.com/watch?v={{ outputs.video_details.videos[0].videoId }}
Return only the post.
Brief:
{{ outputs.content_brief.textOutput }}
- id: newsletter
type: io.kestra.plugin.ai.completion.ChatCompletion
description: Newsletter section with a subject line, 250 to 400 words and a call
to watch.
provider:
type: io.kestra.plugin.ai.provider.Anthropic
apiKey: "{{ secret('ANTHROPIC_API_KEY') }}"
modelName: "{{ vars.claude_model }}"
configuration:
maxToken: 16000
messages:
- type: SYSTEM
content: "You write email newsletters in the first person, as the creator
writing to their own subscribers about their new video. Never
refer to \"the creator\". Voice: {{ inputs.brand_voice }}"
- type: USER
content: |
Write a newsletter section about this video: a subject line on the first line prefixed with `Subject:`, then 250 to 400 words built around the key points, then a call to watch at https://www.youtube.com/watch?v={{ outputs.video_details.videos[0].videoId }}
Return only the newsletter text.
Brief:
{{ outputs.content_brief.textOutput }}
- id: save_pack
type: io.kestra.plugin.core.storage.Write
description: Save the brief and all four drafts as one Markdown file attached to
the execution.
extension: .md
content: |
# Content pack: {{ outputs.video_details.videos[0].title }}
Video: https://www.youtube.com/watch?v={{ outputs.video_details.videos[0].videoId }}
Generated by execution {{ execution.id }}
{{ outputs.content_brief.textOutput }}
---
## YouTube description
{{ outputs.seo_description.textOutput }}
---
## X thread
{{ outputs.x_thread.textOutput }}
---
## LinkedIn post
{{ outputs.linkedin_post.textOutput }}
---
## Newsletter
{{ outputs.newsletter.textOutput }}
- id: announce_pack
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tell the channel a new pack is ready. Each draft follows as its own
message so it can be copied in one go.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
*Content pack ready:* {{ outputs.video_details.videos[0].title }}
https://www.youtube.com/watch?v={{ outputs.video_details.videos[0].videoId }}
Four drafts below. The full pack with the brief is attached to Kestra execution {{ execution.id }}.
- id: post_drafts
type: io.kestra.plugin.core.flow.Loop
description: Post each draft to Slack in a fixed order.
values: [ "seo_description", "x_thread", "linkedin_post", "newsletter" ]
concurrencyLimit: 1
tasks:
- id: post_draft
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: One Slack message per draft.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
*{{ vars.formats[item.value] }}*
{{ outputs[item.value].textOutput }}
- id: publish_to_youtube
type: io.kestra.plugin.core.flow.If
description: Optional write-back. Only runs when `apply_to_youtube` is true, and
only after a human approves.
condition: "{{ inputs.apply_to_youtube }}"
then:
- id: request_publish_approval
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Ask for approval before anything on YouTube changes.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
*Approval needed:* replace the YouTube description of "{{ outputs.video_details.videos[0].title }}" with the draft above (adds chapters).
Resume Kestra execution {{ execution.id }} within 24 hours to publish. If nobody does, nothing changes on YouTube.
- id: approve_publish
type: io.kestra.plugin.core.flow.Pause
description: Wait for a human to resume the execution. After 24 hours the
execution is cancelled and YouTube is left untouched.
pauseDuration: PT24H
behavior: CANCEL
- id: publish_token
type: io.kestra.plugin.youtube.OAuth2
description: Fresh access token, since the approval can take hours.
clientId: "{{ secret('YOUTUBE_CLIENT_ID') }}"
clientSecret: "{{ secret('YOUTUBE_CLIENT_SECRET') }}"
refreshToken: "{{ secret('YOUTUBE_REFRESH_TOKEN') }}"
- id: current_snippet
type: io.kestra.plugin.core.http.Request
description: Read the video's current snippet right before writing. The update
replaces the whole snippet, so the existing title, category, tags and
language must be sent back. This output also keeps the previous
description for rollback.
uri: "https://www.googleapis.com/youtube/v3/videos?part=snippet&id={{
outputs.video_details.videos[0].videoId }}"
headers:
Authorization: "Bearer {{ outputs.publish_token.accessToken }}"
- id: update_video
type: io.kestra.plugin.core.http.Request
description: Replace only the description. YouTube turns the `00:00` chapter
lines into chapters.
method: PUT
uri: https://www.googleapis.com/youtube/v3/videos?part=snippet
headers:
Authorization: "Bearer {{ outputs.publish_token.accessToken }}"
contentType: application/json
body: |
{% set current = fromJson(outputs.current_snippet.body).items[0].snippet %}
{
"id": {{ outputs.video_details.videos[0].videoId | toJson }},
"snippet": {
"title": {{ current.title | toJson }},
"categoryId": {{ current.categoryId | toJson }},
{% if current.tags is defined %}"tags": {{ current.tags | toJson }},{% endif %}
{% if current.defaultLanguage is defined %}"defaultLanguage": {{ current.defaultLanguage | toJson }},{% endif %}
"description": {{ outputs.seo_description.textOutput | toJson }}
}
}
- id: confirm_publish
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Confirm the update in Slack.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
*Published:* the description and chapters of "{{ outputs.video_details.videos[0].title }}" are now live.
https://www.youtube.com/watch?v={{ outputs.video_details.videos[0].videoId }}
errors:
- id: alert_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert Slack when the pack could not be built, for example when no
captions appeared within 6 hours.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
Content pack FAILED for video {{ trigger.videoId ?? inputs.video_id ?? 'unknown' }} (execution {{ execution.id }}). Check whether the video has captions and whether the YouTube refresh token is still valid.
outputs:
- id: content_pack
type: FILE
displayName: Content pack
description: Markdown file with the brief and all four drafts.
value: "{{ outputs.save_pack.uri }}"
- id: transcript
type: FILE
displayName: Transcript
description: The SRT captions the pack was written from.
value: "{{ outputs.download_transcript.uri }}"