Livepeer Youtube Video Generator Swarm

Imagine automating the process of generating and uploading videos to YouTube with the power of AI and decentralized networks. In this tutorial, you'll learn how to use the Livepeer Youtube Video Generator Swarm to create a swarm of AI agents that collaborate to manage video generation and upload tasks seamlessly.
By leveraging Livepeer’s decentralized video platform and YouTube’s vast audience reach, these agents work together to automatically create and upload video content without the need for manual intervention.
Whether you want to streamline content creation, automate video uploads, or explore decentralized media solutions, this agent-driven process simplifies video management and distribution across multiple platforms, showcasing the potential of decentralized video processing.
Prerequisites
Python 3.11 or higher installed
Git installed
An IDE or text editor (VS Code, PyCharm, etc.)
Basic understanding of Python programming
API keys from:
OpenAI
Anthropic
Livepeer AI
Google API Client
MoviePy
Step-by-Step Tutorial for Building a Livepeer YouTube Video Generator Swarm

Below is the step-by-step tutorial to build the swarm which will autonomously automate the process of creating and uploading YouTube videos.
Cloning the Repository
First, clone the repository containing the base code for building your dApp.
Step 1: Open your terminal or command prompt and run the following commands:
git clone https://github.com/swarmzero/examples/blob/main/swarms/
cd livepeer_youtube_swarm
These commands will download the repository and set up the directory to start working on the agent.
Setting Up the Environment
Now, set up the environment required to run the agent.
Step 1: Create a virtual environment:
python -m venv ./venv
Step 2: Activate the virtual environment:
On Windows
Command Prompt:
venv\Scripts\activate.bat
PowerShell:
venv\Scripts\Activate.ps1
On Linux/Mac:
venv/bin/activate
Installing Dependencies
With the environment activated, install the required dependencies:
pip install -r requirements.txt
Setting Up Environment Variables
Step 1: Create a new file called .env
in the root directory:
touch .env
Step 2: Copy the contents of .env.example
into your new .env
file.
Step 3: Add your API keys to the .env
file:
MODEL=gpt-4-turbo-preview
OPENAI_API_KEY=
MISTRAL_API_KEY=
ANTHROPIC_API_KEY=
ENVIRONMENT=dev
SWARMZERO_LOG_LEVEL=INFO
SWARMZERO_DATABASE_URL=
PINECONE_API_KEY=
LIVEPEER_API_KEY=
Understanding the Code Structure
The repository contains essential components for running the Livepeer YouTube video generator swarm.
app folder: Contains the agents responsible for generating and uploading videos to YouTube, along with helper tools for integrating with Livepeer and YouTube APIs. The main script that orchestrates the swarm agents and manages the task distribution for video generation and upload is the swarm.py.
main.py: The core driver script where you input prompts for video creation and interact with the agents to execute the tasks.
.env: Stores your API keys and configuration settings necessary for accessing the Livepeer and YouTube services.
requirements.txt: Lists the dependencies required to run the agents, including libraries for API interaction and video processing.
Creating the Tools Folder
To set up the tools folder, you'll need to create a directory within your project structure and add the necessary Python files that facilitate various functionalities. Below is a step-by-step guide on how to create the tools folder and its contents.
Add Required Python Files:
Inside the tools folder, we will be creating the following Python files:
__init__.py
files.py
livepeer_api.py
video_editor.py
youtube_upload.py
init.py
This file initializes the tools module and imports necessary functions from other modules.
from .files import save_to_file, read_from_file, list_files, download_from_url
from .livepeer_api import text_to_image, image_to_video
from .video_editor import video_editor
from .youtube_upload import upload_video
files.py
Contains utility functions for file handling such as saving, reading, and listing files.
livepeer_api.py
This file integrates with the Livepeer API for various functionalities, including text-to-image and image-to-video conversions.
video_editor.py
This file contains functions to edit videos, specifically to resize and crop them for platforms like YouTube Shorts.
youtube_upload.py
Implement the necessary functions for uploading videos to YouTube.
Creating the Swarm
1. Import Required Libraries
First, you need to import the necessary libraries and modules to build your AI swarm. This includes Swarm for managing the swarm, tools for saving and processing files, and SDKContext for context management.
from dotenv import load_dotenv
load_dotenv()
from swarmzero import Swarm
from app.tools import save_to_file,list_files, read_from_file
from app.tools import download_from_url
from swarmzero.sdk_context import SDKContext
2. Load Environment Variables
Use load_dotenv()
to load environment variables from a .env
file. This is essential for configuring your environment and accessing necessary credentials.
from dotenv import load_dotenv
load_dotenv()
3. Creating the Configuration File
The configuration file for the Livepeer YouTube Video Generator defines multiple AI agents, each tailored to specific roles in the video production process. It sets essential parameters such as the model, environment, and timeout for tasks, ensuring optimal performance.
Agents include a Script Writer, Scene Writer, Scene Prompt Generator, Scene Image Generator, Scene Image to Video Generator, Video Editor, and YouTube Upload Agent, each equipped with specialized instructions and tools.
4. Create an SDK Context
Define the path to your configuration file (e.g., swamrzero_config.toml
) and create an SDKContext
instance. This context will manage the configuration settings for the swarm.
config_path = "./app/swarmzero_config.toml"
sdk_context = SDKContext(config_path=config_path)
5. Initialize the Livepeer Swarm
Create an instance of Swarm
. Set its name, description, and instructions on how it should operate. Provide the functions it will use (e.g., save_to_file
, list_files
, read_from_file
, download_from_url
) and pass the previously created SDKContext
.
livepeer_swarm = Swarm(
name="Livepeer Youtube Video Generator",
description="A swarm of agents that collaborate as members of a Livepeer Youtube Video Generator team.",
instruction="""You are the manager of a video production team for Livepeer Youtube Video Generator. Your goal is to guide the team in creating an engaging and informative video. Follow these steps:
1. Coordinate with these agents in order:
a. Script Writer Agent
b. Scene Writer Agent
c. Scene Prompt Generator Agent
d. Scene Image Generator Agent (max 3 files at a time)
e. Scene Image to Video Generator Agent (max 3 files at a time)
f. Video Editor Agent
g. Youtube Upload Agent
2. After each interaction with agents, save the output as a separate file in:
`./swarmzero-data/output/<video_topic>/<agent_type>/`
Use the save_to_file tool for this purpose.
3. Scene Image Generator Agent and Scene Image to Video Agent will return url or list of urls. You should use download_from_url tool to download images and videos.
4. Use list_files and read_from_file tools to access and review previous outputs.
5. Ensure each agent receives the relevant input from the previous step.
6. Maintain consistency and coherence throughout the video creation process.
7. Monitor the quality and relevance of each output, requesting revisions if necessary.
8. Provide clear, concise instructions to each agent, specifying their task and any relevant constraints.
9. After all steps are complete, review the entire project for cohesiveness and alignment with the original video concept.
Remember to adapt your management style based on the specific video topic and requirements provided by the user.
""",
functions=[save_to_file, list_files, read_from_file, download_from_url],
sdk_context=sdk_context,
# max_iterations=30,
)
Creating main.py
to run the Swarm
main.py
to run the Swarm1. Import Required Libraries
Start by importing the necessary libraries. You'll use asyncio
for managing asynchronous operations and load_dotenv
to load your environment variables. Additionally, import the livepeer_swarm
instance created earlier.
import asyncio
from dotenv import load_dotenv
from app import livepeer_swarm
2. Load Environment Variables
Use load_dotenv()
to load the environment variables from a .env
file. This step ensures that your application can access the required API keys and other configuration settings.
load_dotenv()
3. Define the Main Asynchronous Function
Create the main asynchronous function, main()
which will handle user interactions with the swarm. Inside this function, print a welcome message and instructions for exiting the program.
async def main():
print(
"Welcome to the Livepeer Youtube Video Generator Swarm!\nVisit https://swarmzero.ai to learn more.\n\nType 'exit' to quit.\n"
)
4. Create an Input Loop
Inside the main()
function, implement a loop that continuously prompts the user for input. If the user types "exit", the loop breaks and the program ends. Otherwise, the input prompt is sent to the livepeer_swarm.chat()
method, which processes the prompt and returns a response.
while True:
prompt = input("\n\nEnter your prompt: \n\n")
if prompt.lower() == "exit":
break
response = await livepeer_swarm.chat(prompt)
print(response)
5. Execute the Main Function
Use the asyncio.run()
method to execute the main()
function. This line ensures that the asynchronous operations within the function are properly managed.
if __name__ == "__main__":
asyncio.run(main())
Running the Agent
Now that everything is set up, let's run the dApp builder.
Step 1: Run the agent:
(venv) python main.py
Step 2: Input your prompt:
Follow the prompts to enter your video topic. The swarm will then generate and upload a video based on your input.
Create a Short Video on Lavish Cars
Output



Last updated
Was this helpful?