python+openai兼容api qwen实现ai根据主题生成带语音动画片视频代码bfwainatestudio
代码语言:python
所属分类:其他
版本1(最新):python+openai兼容api qwen实现ai根据主题生成带语音动画片视频代码bfwainatestudio
代码描述:python+openai兼容api qwen实现ai根据主题生成带语音动画片视频代码bfwainatestudio,通过大模型的html和svg生成能力,实现任意主题生成带字幕台词与语音的svg动画,预览后可导出webm格式视频到本地,这个动画效果不同的ai大模型效果不一样,推荐gpt5.6及claude5或fable5
代码标签: python openai 兼容 api qwen ai 根据 主题 生成 语音 动画片 视频 代
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
import os
import json
import time
import requests
from flask import Flask, request, jsonify, render_template_string
from openai import OpenAI
app = Flask(__name__)
STATIC_AUDIO_DIR = os.path.join(app.root_path, 'static', 'audio')
os.makedirs(STATIC_AUDIO_DIR, exist_ok=True)
def text_to_speech(text: str, output_path: str, api_key: str,
model="cosyvoice-v3-flash", voice="longanyang") -> float:
url = "https://dashscope.aliyuncs.com/api/v1/services/audio/tts/SpeechSynthesizer"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"model": model,
"input": {
"text": text,
"voice": voice,
"format": "wav",
"sample_rate": 24000
}
}
try:
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
result = response.json()
if "output" not in result or "audio" not in result["output"]:
raise Exception(f"API 返回异常: {result}")
audio_info = result["output"]["audio"]
audio_url = audio_info.get("url")
if not audio_url:
audio_data_b64 = audio_info.get("data")
if audio_data_b64:
import base64
audio_bytes = base64.b64decode(audio_data_b64)
with open(output_path, "wb") as f:
f.write(audio_bytes)
else:
raise Exception("未获取到音频数据或 URL")
else:
audio_resp = requests.get(audio_url, timeout=30)
audio_resp.raise_for_status()
with open(output_path, "wb") as f:
f.write(audio_resp.content)
try:
from pydub import AudioSegment
audio = AudioSegment.from_file(output_path)
return len(audio) / 1000.0
except Exception:
try:
import wave
with wave.open(output_path, 'rb') as f:
return f.getnframes() / float(f.getframerate())
except Exception:
return max(2.0, len(text) * 0.25)
except Exception as e:
print(f"TTS Synthesis Error: {e}")
raise e
@app.route('/')
def index():
html_template = r"""
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI SVG Studio</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>
:root {
--bg-main: #090d16;
--bg-panel: #111827;
--bg-input: #1f2937;
--accent: #06b6d4;
--border-color: rgba(255, 255, 255, 0.06);
}
* { box-sizing: border-box; }
html, body {
font-family: 'Plus Jakarta Sans', -apple-system, sans-serif;
background-color: var(--bg-main);
}
.panel {
background: var(--bg-panel);
border: 1px solid var(--border-color);
}
.scrollbar::-webkit-scrollbar { width: 5px; height: 5px; }
.scrollbar::-webkit-scrollbar-track { background: transparent; }
.scrollbar::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.1); border-radius: 99px; }
.scrollbar::-webkit-scrollbar-thumb:hover { background: rgba(255, 255, 255, 0.2); }
.active-card {
border-color: rgba(6, 182, 212, 0.4) !important;
background: rgba(6, 182, 212, 0.04) !important;
}
.toast {
position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
background: rgba(17, 24, 39, 0.95); color: #e5e7eb;
padding: 10px 20px; border-radius: 12px; border: 1px solid rgba(255, 255, 255, 0.1);
font-size: 13px; opacity: 0; transition: all .3s cubic-bezier(0.16, 1, 0.3, 1); z-index: 100;
pointer-events: none;
box-shadow: 0 10px 30px rgba(0,0,0,0.4);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
/* Responsive Grid */
@media (max-width: 1024px) {
.sidebar { position: fixed; left: -320px; top: 0; bottom: 0; z-index: 50; transition: left .3s ease; }
.sidebar.open { left: 0; }
}
</style>
</head>
<body class="text-slate-200 antialiased overflow-x-hidden min-h-screen flex flex-col">
<!-- Toast Notification -->
<div id="toast" class="toast"></div>
<!-- Main Layout Container -->
<div class="flex flex-1 min-h-0">
<!-- ============ Sidebar: Project Drawer ============ -->
<aside id="sidebar" class="sidebar w-72 flex-shrink-0 panel border-r flex flex-col transition-all duration-300">
<div class="p-6 border-b border-white/5">
<div class="flex items-center justify-between mb-5">
<div class="flex items-center gap-2.5">
<div class="w-8 h-8 rounded-lg bg-gradient-to-tr from-cyan-500 to-blue-600 flex items-center justify-center text-base shadow-md">
🎨
</div>
<div>
<h1 class="text-xs font-bold uppercase tracking-wider text-slate-400">BFW ANIMATE Studio</h1>
<p class.........完整代码请登录后点击上方下载按钮下载查看














网友评论0