Skip to main content

蝉镜 AI Python SDK - 对口型、声音克隆、语音合成

Project description

蝉镜 AI Python SDK

基于蝉镜 AI 开放平台的 Python SDK,提供对口型、声音克隆、语音合成能力。

安装

pip install chanjingsdk

安装可选依赖(音频时长检测、视频尺寸检测):

pip install chanjingsdk[all]

从源码安装(开发模式):

cd chanjing-sdk
pip install -e ".[all]"

配置凭证

前往 https://www.chanjing.cc/platform/api_keys 获取 App ID 和 Secret Key。

支持三种配置方式(优先级从高到低):

1. 构造函数参数

from chanjing import CicadaClient
client = CicadaClient(app_id="your_app_id", secret_key="your_secret_key")

2. 环境变量

export CHANJING_APP_ID=your_app_id
export CHANJING_SECRET_KEY=your_secret_key
client = CicadaClient()  # 自动读取环境变量

3. 配置文件 ~/.chanjing/config.json

{
  "app_id": "your_app_id",
  "secret_key": "your_secret_key"
}
client = CicadaClient()  # 自动读取配置文件

快速开始

对口型(音频驱动视频)

from chanjing import CicadaClient

client = CicadaClient(app_id="xxx", secret_key="yyy")

result = client.lip_sync(
    video="./my_video.mp4",
    audio="./my_audio.wav",
    model="pro",           # "standard" | "pro"
    backway="forward",     # "forward" | "reverse"
)

print(result.video_url)
result.download("./output.mp4")

声音克隆 + 语音合成(一步完成)

result = client.voice_clone_and_speak(
    reference_audio="./reference.mp3",   # 参考音频(15秒-5分钟)
    text="你好,这是克隆的声音。",
    model="cicada3.0-turbo",
    speed=1.0,
    pitch=1.0,
)

print(result.audio_url)
result.download("./output.mp3")

分步调用(先克隆,再合成)

# 第一步:克隆声音(结果自动缓存)
voice_id = client.clone_voice(
    reference_audio="./reference.mp3",
    model="cicada3.0-turbo",
)

# 第二步:用克隆的声音合成语音(可多次调用)
result = client.tts(voice_id=voice_id, text="你好世界")
result.download("./hello.mp3")

result2 = client.tts(voice_id=voice_id, text="再见世界", speed=1.2)
result2.download("./goodbye.mp3")

进度回调

所有方法都支持 on_progress 回调:

def my_progress(stage: str, percent: int, message: str):
    print(f"[{stage}] {percent}% - {message}")

result = client.lip_sync(
    video="video.mp4",
    audio="audio.wav",
    on_progress=my_progress,
)

API 参考

CicadaClient(app_id, secret_key, cache_dir, log_level)

参数 类型 默认值 说明
app_id str None 蝉镜平台 App ID
secret_key str None 蝉镜平台 Secret Key
cache_dir str ~/.chanjing/cache/ 缓存目录
log_level int logging.INFO 日志级别,设 None 不修改

client.lip_sync(video, audio, model, backway, drive_mode, on_progress)

参数 类型 默认值 说明
video str 必填 本地视频文件路径
audio str 必填 本地音频文件路径
model str "pro" "standard""pro"
backway str "forward" "forward""reverse"
drive_mode str "normal" "normal""random"

返回 LipSyncResult.video_url .task_id .duration_ms .download(path)

client.clone_voice(reference_audio, model, use_cache, on_progress)

参数 类型 默认值 说明
reference_audio str 必填 参考音频路径(15秒-5分钟)
model str "cicada3.0-turbo" 模型类型
use_cache bool True 是否缓存克隆结果

返回 voice_id: str

client.tts(voice_id, text, speed, pitch, on_progress)

参数 类型 默认值 说明
voice_id str 必填 声音 ID
text str 必填 合成文案(最多4000字)
speed float 1.0 语速(0.5-2.0)
pitch float 1.0 音调(0.1-3.0)

返回 TTSResult.audio_url .task_id .duration .download(path)

client.voice_clone_and_speak(reference_audio, text, model, speed, pitch, use_cache, on_progress)

声音克隆 + 语音合成一步完成,参数同上。返回 TTSResult

依赖

必需 说明
requests HTTP 请求
mutagen 音频时长检测(pip install chanjing[audio]
opencv-python 视频尺寸检测(pip install chanjing[video]

支持

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chanjingsdk-1.0.1.tar.gz (5.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chanjingsdk-1.0.1-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file chanjingsdk-1.0.1.tar.gz.

File metadata

  • Download URL: chanjingsdk-1.0.1.tar.gz
  • Upload date:
  • Size: 5.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for chanjingsdk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8d7f7c6e9468ff7b6235d9c39c9b2a849f57afd60241225e4a1094468e5b862b
MD5 1efac65296eb28252f961c573d685339
BLAKE2b-256 4f17ffcb4fc289c73351ec7622010b9a806b832c584fed092d0cc0383033d4a9

See more details on using hashes here.

File details

Details for the file chanjingsdk-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: chanjingsdk-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for chanjingsdk-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8f4f5a189a323e036f2800a56b06971f216d72213fab2ed3b36b57d123c04bd3
MD5 6a5266d89edba7209951e957c40e7845
BLAKE2b-256 bdf48dbca03876ff980aa7e1ae6ad58e44e10ab522946c65839db6745b48f44e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page