VideoDB Director GitHub
Explore the complete Director framework and see real-world agent implementations
Game Plan for Voice Replacement Agent
The steps are fairly simple:- User uploads voice samples (to clone) and a video (whose voice needs to be replaced).
- Clone the voice from the provided samples.
- Extract the transcript from the video.
- Generate an audio clip in the cloned voice, narrating the transcript.
- Overlay the new audio onto the video, replacing the original voice.
Core Architecture
The Voice Replacement Agent is built on Director’s extensible framework, leveraging its session management and state tracking capabilities while adding specialised voice replacement functionality. This is how the flow from the user’s input to the video output will look like.
The following parameters will be needed for the agent. To learn more about the importance of an agent’s parameters, check out the Director documentation.
From ElevenLabs:
You’ll need the following two key methods from the ElevenLabs SDK:
clone: Used to clone a voice based on the provided audio samples.generate: Generates synthesised audio from text using the cloned voice.
Setup
VideoDB and Director Setup
Get your API key from VideoDB Console. Install the latest SDKSetup Director Locally
Complete installation and configuration guide for Director framework
Create ElevenLabs methods
For the voice cloning feature, theclone and generate methods given by ElevenLabs needs to be implemented. For this, you can access the existing ElevenLabs tool present in the /backend/director/tools folder. Let’s go to the ElevenLabs tool in the elevenlabs.py.
Define the required methods in the ElevenLabsTool.
For cloning, create the clone_audio method
synthesise_text method
Building the Agent
1. Import the required components
Create avoice_replacement.py file inside backend/director/agents folder and add all our imports to it. They include:
Python
2. Define the parameters for the agent
Referring to the Game Plan, create a JSON schema for these parameters.Python
3. Implement Agent Class
We will now create the agent class. The parameters set here (self.agent_name, self.description and the self.parameters) determine how the agent interacts with the reasoning engine.
Python
4. Implement the core logic of the voice replacement agent
1. Declare arun method
We will need to implement a run method in the agent’s class. This is the heart of the agent as this is the method that runs when the agent is called.
In the run method, define the required parameters which will be used to implement the agent.
Python
Python
Python
- Generate a stream of the video for the sample based on start and end time of sample audio
- Download the video based on the stream
- Extract the audio file from the video
get_video_stream method from the VideoDBTool :
Python
Python
_download_video_file method which will download the video via the download_url and give the path where it is saved
Python
video_path
Python
extract_audio_from_video method.
Start by defining a method _extract_audio_from_video, which takes a video file path as input and extracts audio from it.
Python
get_audio method inside tools/videodb-tool.py file to include a url field in the returned object. This field provides a direct link to the extracted audio file.
Python
_download_audio_file method which will take the URL
Python
Python
clone_audio method to create the cloned voice.
Python
video_id present in the video_ids list
Python
Python
run method.
Python
synthesise_text method to generate audio from the input text
Python
Python
Python
video_id and audio_id which returns a stream link so that we can stream the video.
Python
run method, before adding the overlay to the video, we will use VideoContent to display the video output in the Director’s UI to show that adding the audio overlay is in progress.
Python
video_content so that we can watch it in the Director’s UI.
Python
AgentResponse to end the agent’s process
Once all the videos are processed, we will use the publish method to save the messages as a final step.
cloned_voice_id and audio_id as a response so that any subsequent chat requests will be able to use them to generate further responses.
Python
Python
5. Register the Agent
To use the agent, go inside thebackend/director/handler.py file and import the agent.
Python
self.agents list
Python
Using the agent
Simply go to the frontend at http://localhost:8080 and refresh the page to see the agent available in the options for use.Conclusion
The Voice Transformation Agent demonstrates the power and flexibility of Director’s agent framework. By leveraging VideoDB’s video overlay feature and robust database system and ElevenLabs’ voice cloning capabilities, we’ve created a secure, scalable solution for your voice cloning and video overlay needs. Key takeaways:- Simple integration with Director’s framework
- Robust error handling and security measures
- Scalable architecture for audio processing
- Seamless way of adding audio overlays on videos
- Ethical considerations built into the design
Resources
Related Tutorials
AI Voiceovers
Add professional narration to silent footage
Multi-Language Dubbing
Dub videos into any language with AI voices
Director Documentation
Comprehensive guides and API references for Director framework
Sample Agents Repository
Real-world examples and implementations of custom agents
Discord Community
Connect with other agent developers and get support
Need help? Reach out to our community or open an issue on GitHub. Happy building!