In the age of AI, everything seems to be accelerating at an unprecedented speed. Generating this, generating that, data overload everywhere. I’m sure you’re already mentally exhausted from all the information, so let’s get down to business.
What I want to do:
YouTube Video → Quick Information → Learn ✨ + Share on Social Media 🐦
… that’s it …
This AI race is all about speed. If you can leverage AI to accelerate learning, why not? So please share this with your family, friends, followers or even as an influencer.
result:
Repository: https://github.com/WorkSmarter-lol/yt2tweets-cli/
Disclaimer:
This tutorial only works for YouTube videos with English subtitles and does not apply to YouTube Shorts.
process
0. Prerequisite knowledge:
Before you begin, it’s important to understand the basics of LangChain.js, prompt engineering, and how OpenAI models work.
1. Prerequisite tools:
In my case, I start my development using yarn. To easily start testing the script, I tried out the project by running a simple MVP using the CLI.
2. Choosing a name for the CLI
For easy reference and catchy name, I named this “yt2tweets”. This essentially means → “Tweet from YouTube.”
3. Desired output
$ yt2tweets “https://youtu.be/1-TZqOsVCNM” # Results: # Tweet 1: Introduction … 🧵👇 (1/X) # Tweet 2: … 🧵 (2/X) # Tweet 3: … 🧵 (3/X) # Tweet 4: … 🧵 (4/X) # Tweet 5: Conclusion … 🧵 (5/X)
Ideally, you should provide the transcript as context for your AI model (in this case GPT-4o-mini). This allows you to understand the context and summarize the input in the specified output format.
4. Rapid design
Here’s the secret sauce to making things work. Rapid engineering is a core skill needed to get the job done.
To customize how the CLI converts YouTube videos to Twitter/X threads, follow the simple and easy 3-step setup I defined. You can adjust the tone, length, and style to suit your needs. To guide the AI, follow the inserted blocks below for a smooth configuration process.
identity and purpose
Set the AI’s role and goals with ID and purpose blocks. Define its functionality and purpose so you can generate content that fits your needs and desired results.
step
Define step-by-step actions for the AI to follow and ensure a clear and structured approach to generating content.
Output format
Specify the format in which the AI will deliver your content.
Examples like:
import { ChatPromptTemplate } from ‘@langchain/core/prompts’; const prompts = ChatPromptTemplate.fromMessages([
{
role: ‘system’,
content: `
# IDENTITY AND PURPOSE
{identity}
# STEPS
{steps}
# OUTPUT INSTRUCTIONS
{formats}
# INPUT
INPUT: {input}
`,
},
]);
{input} is all the transcripts you want GPT to summarize.
A reference of how I added the prompt can be found here along with an example below.
5. Putting it all together
Finally, to run this you need @langchain/openai installed and an OpenAI API key prepared. Once everything is sorted, you can start the model and start passing prompts and feeds to the AI for responses.
import { ChatOpenAI } from ‘@langchain/openai’; // Instantiate the model const llm = new ChatOpenAI({ modelName: ‘gpt-4o-mini’, pressure: 0.7, // <-- ここで自由に温度を調整してください apiKey, }); // ... // ここにプロンプトを追加します // ... //chain.invoke が変数を正しく渡すことを確認します result = await prompt.pipe(llm).invoke({identity,steps,formats,input,}); // 結果を取得 console.log('>> result?.content’); // Tweet 1: Introduction … 🧵👇 (1/X) … 6. Put it all together as CLI
For convenience, I exported the function as CLI. This will make it easier to use in the future.
To achieve that I used the following.
Commander — Enabling CLI for NPM packages BIN Ora — Elegant terminal spinner Chalk — Terminal string style
A snippet of code is shown below (full code is at the end).
import { command } from ‘commander’; import chalk from ‘chalk’; Import ora from ‘ora’. const spinner = ora(‘Loading…’); // Initialize command line interface const Program = new Command(); // Command program to convert YouTube URL .argument(”) .description(‘ Turn YouTube videos into Twitter threads with AI’) .action(async url => { const apiKey = readApiKey(); // Key to read saved API // …spinner.start(); await ConvertYt2Tweets(url , apiKey); // … });
Repository link (full code)
https://github.com/WorkSmarter-lol/yt2tweets-cli
conclusion
Again, I hope this project helps you speed up your learning, digest YouTube content, or share it with your friends, family, and followers.
If you want to access a UI-enabled project, we have created a user interface for the same project. Please see the link below.
https://yt2tweets.worksmarter.lol