Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python dubbing example #6

Merged
merged 3 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Ruff
on: [push, pull_request]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ Thumbs.db
# env
.env*
!.env.example

# formatters
.ruff_cache
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.3.7
hooks:
# Run the linter.
- id: ruff
args: ["--fix", "--select", "I"]
# Run the formatter.
- id: ruff-format
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
Binary file added examples/dubbing/example_speech.mp3
Binary file not shown.
1 change: 1 addition & 0 deletions examples/dubbing/python/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ELEVENLABS_API_KEY=
1 change: 1 addition & 0 deletions examples/dubbing/python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dubbed_file.mp4
82 changes: 82 additions & 0 deletions examples/dubbing/python/create_a_dub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import os
import time

import requests
from dotenv import load_dotenv
from elevenlabs.client import ElevenLabs

load_dotenv()

ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")

if ELEVENLABS_API_KEY is None:
raise ValueError(
"ELEVENLABS_API_KEY environment variable not found, please copy the .env.example file to .env and fill in the API key"
)

client = ElevenLabs(api_key=ELEVENLABS_API_KEY)


def create_dub(
file_name, format, input_file_path, output_file_path, source_lang, target_language
):
with open(input_file_path, "rb") as audio_file:
response = client.dubbing.dub_a_video_or_an_audio_file(
file=(file_name, audio_file, format),
target_lang=target_language,
mode="automatic",
source_lang="en",
num_speakers=1,
watermark=True, # reduces the characters used
)

dubbing_id = response.dubbing_id

for i in range(1000):
headers = {
"xi-api-key": ELEVENLABS_API_KEY,
}

metadata = client.dubbing.get_dubbing_project_metadata(dubbing_id=dubbing_id)
if metadata.status == "dubbed":
# TODO: fix the response type of client.dubbing.get_dubbed_file
response = requests.get(
"https://api.elevenlabs.io/v1/dubbing/"
+ dubbing_id
+ "/audio/"
+ target_language,
headers=headers,
)

if response.status_code == 200:
with open(output_file_path, "wb") as file:
file.write(response.content)
print("Dubbing complete and saved to dubbed_file.mp4")

return

elif metadata.status == "dubbing":
print("Dubbing in progress... Will check status again in 10 seconds")
time.sleep(10)
else:
print("Dubbing failed", response.json())
return

if i == 10:
print("Dubbing timed out")


if __name__ == "__main__":
input_file_path = os.path.join(os.path.dirname(__file__), "../example_speech.mp3")
output_file_path = "dubbed_file.mp4"

source_language = "en"
target_language = "es"
create_dub(
"example_speech.mp3",
"audio/mpeg",
input_file_path,
output_file_path,
source_language,
target_language,
)
2 changes: 2 additions & 0 deletions examples/dubbing/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elevenlabs==1.1.2
python-dotenv==1.0.1
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pre-commit==3.7.0
ruff==0.3.7
Loading