DEV Community

Cover image for Automate Tweets From YouTube Videos
HeetVekariya
HeetVekariya

Posted on

Automate Tweets From YouTube Videos

Hello readers! Last Sunday, I was watching a YouTube video from the awesome BreakDown channel. It featured Nandan Nilekani’s talk, TheGreatUnlock, delivered on March, 2025. I highly recommend you watch this video !

The video was full of cool ideas that got me thinking! I was also looking for something to write about for my weekly blog, and I had a lightbulb moment: Why not turn the video’s insights into tweets? So, I created a Python script to do just that !

In this blog, I’ll walk you through the code in a simple way, keeping the tone same as my other blogs.


The Big Idea

The plan is straightforward: take a YouTube video, grab its spoken words (the transcript), create a short and insightful tweet, and share it on X. The script uses a few tools to make this happen: one to get the video’s transcript, another to post the tweet, and some smart “agents” to manage the process.

How the Code Works

Here’s the process in simple terms:

  1. Get the Video’s Words: The script grabs the spoken text from the YouTube video using its web address (URL).
  2. Make a Insightful Tweet: It picks out the best parts of the transcript and turns them into a short tweet (under 280 characters) that’s fun and shareable.
  3. Share on X: The tweet gets sent to X using a special tool, so it goes live for everyone to see.

Let’s break down the code step by step.

Step 1: Setting Up the Tools

The script uses two main tools:

  • Video Text Tool: This retrives transcribe from a YouTube video.
  • Tweet Posting Agent: This posts the tweet to X.
youtube_summary_tool = FunctionTool(
    name="youtube_summary_tool",
    description="Gets the spoken words from a YouTube video using its URL.",
    params_json_schema=YouTubeSummaryArgs.model_json_schema(),
    on_invoke_tool=summarize_youtube_video
)
Enter fullscreen mode Exit fullscreen mode

This tool uses a library called YouTubeTranscriptApi to fetch the video’s transcript. The code finds the video’s unique ID from the URL (like u5-UMgNZs6k from the BreakDown video) and pulls the text.

Step 2: Creating the Tweet

The script has a “Marketing Agent” that acts like a creative writer. It takes the video’s transcript, finds the most interesting facts, and crafts a tweet that’s short, engaging, and under 280 characters. The agent is told to make the tweet exciting so it has a chance to go viral.

def create_prompt(video_url):
    return f"""
You are a creative writer who makes exciting tweets from YouTube videos.

Steps to follow:
1. Get the video’s spoken words using the video text tool.
2. Write a short, catchy tweet (under 280 characters) based on the video’s content.
3. Pass the tweet to the Tweet Posting Agent to share it on X.

Target video: {video_url}
"""
Enter fullscreen mode Exit fullscreen mode

Step 3: Posting the Tweet

Once the tweet is ready, the Marketing Agent hands it off to a “Tweet Posting Agent.” This agent uses a library called tweepy to connect to X and post the tweet.

twitter_client = tweepy.Client(
    bearer_token=os.getenv("TWITTER_BEARER_TOKEN"),
    consumer_key=os.getenv("TWITTER_CONSUMER_KEY"),
    consumer_secret=os.getenv("TWITTER_CONSUMER_SECRET"),
    access_token=os.getenv("TWITTER_ACCESS_TOKEN"),
    access_token_secret=os.getenv("TWITTER_ACCESS_TOKEN_SECRET"),
    wait_on_rate_limit=True
)
response = twitter_client.create_tweet(text=tweet)
Enter fullscreen mode Exit fullscreen mode

The script uses secret keys (stored safely in a .env file, you can develop one for yourself using .env.example) to connect to X securely. Once the tweet is posted, the script checks if it worked and prints a success message.

Step 4: Tying It All Together

The main part of the script runs everything.

async def main(video_url=None):
    if video_url is None:
        video_url = "https://www.youtube.com/watch?v=u5-UMgNZs6k"

    print(f"Processing video: {video_url}")

    host_agent = create_host_agent(video_url)

    result = await Runner.run(
        host_agent,
        f"""Process this YouTube video: {video_url}
Steps:
1. Use the video text tool to get the transcript.
2. Create a catchy tweet based on the content.
3. Pass it to the Tweet Posting Agent to share on X.
"""
    )

    print("All done!")
    return result
Enter fullscreen mode Exit fullscreen mode

This code starts with the BreakDown video URL (you can swap it for any YouTube video), runs the agents, and makes sure the tweet gets posted.

Twetter posting

Why This Is Worth Giving a Try ?

This script is like a marketing machine for the YouTube content-creators. It saves time by automatically pulling video insights and turning them into tweets. You could use it for any YouTube video with a transcript—think tech talks, tutorials, or even funny vlogs. It’s a fun way to share ideas with your followers on X.

GitHub: YouTube-Tweet

Conclusion

This project was a fun way to turn interesting YouTube talks into short, shareable tweets. By combining a few Python tools, I was able to pull out insights from Nandan Nilekani’s talk and automatically post them to X. If you're someone who enjoys tech, content, or automation, give it a try with your favorite video. Thanks for reading!


👉 If you found this helpful, don’t forget to share and follow for more agent-powered insights. Got an idea or workflow in mind? Join the discussion in the comments or reach out on Twitter.

Thank you gif

Top comments (6)

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

this is extremely impressive. i’ve tried to automate stuff like this before, but never got it this smooth. you think having more voices turned into tweets actually helps people dig deeper into harder topics, or just makes them skim more

Collapse
 
heetvekariya profile image
HeetVekariya

Thank you @nathan_tarbert :)

I tried to keep the code neat and clean such that it can be easily understood by everyone. Ping me, if you try it and find any difficulties while running it.

In my opinion, tweets will give highlights and make others curious to watch video. It will help content creators, if they use it 😅🤞🍀(I hope so), to attract viewers from twitter and readers that are in very hurry.

What do you think ?

Collapse
 
solvecomputerscience profile image
Solve Computer Science

Interesting automation ideas. Does the AI agent work locally or is it cloud based? I see a GEMINI_API_KEY so I suppose it's not local.

Collapse
 
heetvekariya profile image
HeetVekariya

Yes, you are right.

It is using gemini running on google clouds.

Collapse
 
knight03 profile image
Dhvani

This is great man.

Collapse
 
heetvekariya profile image
HeetVekariya

Thank you :)