Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
-
import
|
| 5 |
from datetime import datetime
|
| 6 |
-
from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip, vfx
|
| 7 |
from openai import OpenAI
|
| 8 |
|
| 9 |
# 配置 OpenAI API
|
|
@@ -11,31 +10,25 @@ client = OpenAI(
|
|
| 11 |
api_key=os.getenv("sk-ZYyxdh3NexC863OUdicSlKrunXEPlRlsLBMwc1vXMGEVwZdb")
|
| 12 |
)
|
| 13 |
|
| 14 |
-
def analyze_video_content(
|
| 15 |
-
"""使用 OpenAI
|
| 16 |
try:
|
| 17 |
-
prompt = f"""
|
| 18 |
|
| 19 |
-
|
| 20 |
-
1. 📹
|
| 21 |
-
2. 🎵
|
| 22 |
-
3. ✂️
|
| 23 |
-
4. 🎯
|
| 24 |
-
5. 📈
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
response = client.chat.completions.create(
|
| 29 |
-
model="gpt-4o-mini",
|
| 30 |
messages=[
|
| 31 |
-
{
|
| 32 |
-
|
| 33 |
-
"content": "你是一位资深的视频内容策划和分析专家,擅长为创作者提供专业的视频制作建议。"
|
| 34 |
-
},
|
| 35 |
-
{
|
| 36 |
-
"role": "user",
|
| 37 |
-
"content": prompt
|
| 38 |
-
}
|
| 39 |
],
|
| 40 |
max_tokens=600,
|
| 41 |
temperature=0.7
|
|
@@ -43,451 +36,223 @@ def analyze_video_content(video_path, video_count):
|
|
| 43 |
|
| 44 |
return response.choices[0].message.content
|
| 45 |
except Exception as e:
|
| 46 |
-
return f"❌ AI 分析失败: {str(e)}
|
| 47 |
|
| 48 |
-
def generate_creative_titles(video_count
|
| 49 |
-
"""
|
| 50 |
try:
|
| 51 |
-
prompt = f"""
|
| 52 |
|
| 53 |
要求:
|
| 54 |
-
- 🔥
|
| 55 |
-
- 📱
|
| 56 |
-
- 💯
|
| 57 |
-
- 🎭
|
| 58 |
-
- 🚀 包含动作词汇增加点击欲望
|
| 59 |
|
| 60 |
-
|
| 61 |
|
| 62 |
response = client.chat.completions.create(
|
| 63 |
model="gpt-4o-mini",
|
| 64 |
messages=[
|
| 65 |
-
{
|
| 66 |
-
|
| 67 |
-
"content": "你是一位专业的新媒体内容创作者,精通各种平台的标题优化技巧。"
|
| 68 |
-
},
|
| 69 |
-
{
|
| 70 |
-
"role": "user",
|
| 71 |
-
"content": prompt
|
| 72 |
-
}
|
| 73 |
],
|
| 74 |
max_tokens=400,
|
| 75 |
-
temperature=0.9
|
| 76 |
)
|
| 77 |
|
| 78 |
return response.choices[0].message.content
|
| 79 |
except Exception as e:
|
| 80 |
return f"❌ 标题生成失败: {str(e)}"
|
| 81 |
|
| 82 |
-
def
|
| 83 |
-
"""
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
model="gpt-4o", # 使用更强的模型做总结
|
| 103 |
-
messages=[
|
| 104 |
-
{
|
| 105 |
-
"role": "system",
|
| 106 |
-
"content": "你是一位技术项目管理专家,擅长撰写项目总结和技术方案评估报告。"
|
| 107 |
-
},
|
| 108 |
-
{
|
| 109 |
-
"role": "user",
|
| 110 |
-
"content": prompt
|
| 111 |
-
}
|
| 112 |
-
],
|
| 113 |
-
max_tokens=700,
|
| 114 |
-
temperature=0.6
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
return response.choices[0].message.content
|
| 118 |
-
except Exception as e:
|
| 119 |
-
return f"❌ 总结生成失败: {str(e)}"
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
- 视频文件数: {file_count}
|
| 126 |
-
- 音频时长: {audio_duration:.1f}秒
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
4. 注意事项提醒
|
| 133 |
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
],
|
| 141 |
-
max_tokens=300,
|
| 142 |
-
temperature=0.5
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
return response.choices[0].message.content
|
| 146 |
-
except Exception as e:
|
| 147 |
-
return "无法获取AI建议,将使用默认参数处理"
|
| 148 |
|
| 149 |
-
|
| 150 |
-
"""
|
| 151 |
-
|
| 152 |
-
if not video_files or not audio_file:
|
| 153 |
-
return "❌ 请上传视频文件和音频文件", [], "", "", ""
|
| 154 |
-
|
| 155 |
-
start_time = datetime.now()
|
| 156 |
-
|
| 157 |
-
# 获取 AI 处理建议
|
| 158 |
-
audio_duration = 0
|
| 159 |
-
try:
|
| 160 |
-
temp_audio = AudioFileClip(audio_file.name)
|
| 161 |
-
audio_duration = temp_audio.duration
|
| 162 |
-
temp_audio.close()
|
| 163 |
-
except:
|
| 164 |
-
audio_duration = 30 # 默认值
|
| 165 |
-
|
| 166 |
-
ai_suggestions = get_ai_processing_suggestions(len(video_files), audio_duration)
|
| 167 |
-
|
| 168 |
-
# 开始视频分析
|
| 169 |
-
status_updates = "🤖 OpenAI GPT 正在分析视频内容...\n"
|
| 170 |
-
video_analysis = analyze_video_content(video_files[0].name if video_files else "", len(video_files))
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
temp_dir = tempfile.mkdtemp()
|
| 175 |
-
output_dir = os.path.join(temp_dir, f"openai_output_{datetime.now().strftime('%m%d_%H%M')}")
|
| 176 |
-
os.makedirs(output_dir, exist_ok=True)
|
| 177 |
-
|
| 178 |
-
status_updates += "🎬 开始处理视频文件...\n"
|
| 179 |
-
|
| 180 |
-
# 处理视频逻辑
|
| 181 |
-
video_clips = []
|
| 182 |
-
total_source_duration = 0
|
| 183 |
-
|
| 184 |
-
# 加载并切割所有视频
|
| 185 |
-
for i, video_file in enumerate(video_files):
|
| 186 |
-
status_updates += f"📹 处理第 {i+1}/{len(video_files)} 个视频...\n"
|
| 187 |
-
|
| 188 |
-
clip = VideoFileClip(video_file.name)
|
| 189 |
-
total_source_duration += clip.duration
|
| 190 |
-
|
| 191 |
-
# 按时长切割视频
|
| 192 |
-
for start_time_clip in range(0, int(clip.duration), clip_duration):
|
| 193 |
-
end_time = min(start_time_clip + clip_duration, clip.duration)
|
| 194 |
-
if end_time - start_time_clip >= 0.5: # 至少0.5秒的片段
|
| 195 |
-
sub_clip = clip.subclip(start_time_clip, end_time).without_audio()
|
| 196 |
-
video_clips.append(sub_clip)
|
| 197 |
-
|
| 198 |
-
# 加载音频
|
| 199 |
-
audio = AudioFileClip(audio_file.name)
|
| 200 |
-
target_duration = audio.duration
|
| 201 |
-
|
| 202 |
-
# 智能打乱视频片段
|
| 203 |
-
random.shuffle(video_clips)
|
| 204 |
-
|
| 205 |
-
status_updates += f"🎵 音频时长: {target_duration:.1f}秒\n"
|
| 206 |
-
status_updates += f"📊 生成 {len(video_clips)} 个视频片段\n"
|
| 207 |
-
status_updates += f"🔄 准备输出 {num_output_videos} 个混剪视频...\n"
|
| 208 |
-
|
| 209 |
-
# 生成输出视频
|
| 210 |
-
output_files = []
|
| 211 |
-
clips_per_video = max(1, len(video_clips) // num_output_videos)
|
| 212 |
-
|
| 213 |
-
for i in range(num_output_videos):
|
| 214 |
-
start_idx = i * clips_per_video
|
| 215 |
-
end_idx = start_idx + clips_per_video
|
| 216 |
-
|
| 217 |
-
# 最后一个视频包含剩余片段
|
| 218 |
-
if i == num_output_videos - 1:
|
| 219 |
-
end_idx = len(video_clips)
|
| 220 |
-
|
| 221 |
-
selected_clips = video_clips[start_idx:end_idx]
|
| 222 |
-
|
| 223 |
-
if selected_clips:
|
| 224 |
-
# 拼接视频片段
|
| 225 |
-
final_video = concatenate_videoclips(selected_clips)
|
| 226 |
-
|
| 227 |
-
# 调整视频长度匹配音频
|
| 228 |
-
if final_video.duration > target_duration:
|
| 229 |
-
final_video = final_video.subclip(0, target_duration)
|
| 230 |
-
elif final_video.duration < target_duration:
|
| 231 |
-
# 循环播放直到匹配音频长度
|
| 232 |
-
loops_needed = int(target_duration / final_video.duration) + 1
|
| 233 |
-
looped_video = concatenate_videoclips([final_video] * loops_needed)
|
| 234 |
-
final_video = looped_video.subclip(0, target_duration)
|
| 235 |
-
|
| 236 |
-
# 添加音频和效果
|
| 237 |
-
final_video = final_video.set_audio(audio)
|
| 238 |
-
|
| 239 |
-
# 添加镜像效果(防止版权检测)
|
| 240 |
-
final_video = final_video.fx(vfx.mirror_x)
|
| 241 |
-
|
| 242 |
-
# 保存视频
|
| 243 |
-
output_filename = f"openai_mixcut_{i+1:02d}.mp4"
|
| 244 |
-
output_path = os.path.join(output_dir, output_filename)
|
| 245 |
-
|
| 246 |
-
final_video.write_videofile(
|
| 247 |
-
output_path,
|
| 248 |
-
codec='libx264',
|
| 249 |
-
audio_codec='aac',
|
| 250 |
-
temp_audiofile='temp-audio.m4a',
|
| 251 |
-
remove_temp=True,
|
| 252 |
-
verbose=False,
|
| 253 |
-
logger=None
|
| 254 |
-
)
|
| 255 |
-
|
| 256 |
-
output_files.append(output_path)
|
| 257 |
-
|
| 258 |
-
# 清理内存
|
| 259 |
-
final_video.close()
|
| 260 |
-
|
| 261 |
-
# 清理资源
|
| 262 |
-
for clip in video_clips:
|
| 263 |
-
clip.close()
|
| 264 |
-
audio.close()
|
| 265 |
-
|
| 266 |
-
# 计算处理时间
|
| 267 |
-
end_time = datetime.now()
|
| 268 |
-
processing_time = (end_time - start_time).total_seconds()
|
| 269 |
-
|
| 270 |
-
# 生成 AI 标题和总结
|
| 271 |
-
status_updates += "🎯 生成创意标题...\n"
|
| 272 |
-
ai_titles = generate_creative_titles(num_output_videos)
|
| 273 |
-
|
| 274 |
-
status_updates += "📋 生成项目总结...\n"
|
| 275 |
-
project_summary = generate_project_summary(num_output_videos, f"{processing_time:.1f}秒")
|
| 276 |
-
|
| 277 |
-
# 最终状态
|
| 278 |
-
final_status = f"""✅ 处理完成!OpenAI GPT 增强版
|
| 279 |
-
|
| 280 |
-
📊 处理统计:
|
| 281 |
-
• 输入视频: {len(video_files)} 个
|
| 282 |
-
• 生成片段: {len(video_clips)} 个
|
| 283 |
-
• 输出视频: {len(output_files)} 个
|
| 284 |
-
• 处理时长: {processing_time:.1f} 秒
|
| 285 |
-
• 音频匹配: {target_duration:.1f} 秒
|
| 286 |
|
| 287 |
-
|
| 288 |
-
• ✓ 内容智能分析
|
| 289 |
-
• ✓ 创意标题生成
|
| 290 |
-
• ✓ 专业项目总结
|
| 291 |
-
• ✓ 处理建议优化
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
error_msg = f"""❌ 处理出现错误: {str(e)}
|
| 299 |
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
return error_msg, [], ai_suggestions, "", ""
|
| 307 |
|
| 308 |
-
|
| 309 |
-
with gr.Blocks(
|
| 310 |
-
title="pyTovideo2 + OpenAI GPT",
|
| 311 |
-
theme=gr.themes.Soft(),
|
| 312 |
-
css="""
|
| 313 |
-
.gradio-container {
|
| 314 |
-
font-family: 'Arial', sans-serif;
|
| 315 |
-
}
|
| 316 |
-
.main-header {
|
| 317 |
-
text-align: center;
|
| 318 |
-
margin-bottom: 25px;
|
| 319 |
-
padding: 20px;
|
| 320 |
-
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 321 |
-
border-radius: 10px;
|
| 322 |
-
color: white;
|
| 323 |
-
}
|
| 324 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
) as demo:
|
| 326 |
|
| 327 |
-
# 主标题区域
|
| 328 |
gr.HTML("""
|
| 329 |
-
<div
|
| 330 |
-
<h1
|
| 331 |
-
<p style="margin: 0;
|
|
|
|
| 332 |
</div>
|
| 333 |
""")
|
| 334 |
|
| 335 |
-
# 主要操作区域
|
| 336 |
with gr.Row():
|
| 337 |
-
# 左侧:文件上传和参数设置
|
| 338 |
with gr.Column(scale=1):
|
| 339 |
-
gr.Markdown("### 📤
|
| 340 |
|
| 341 |
video_input = gr.File(
|
| 342 |
-
label="🎥 选择视频文件
|
| 343 |
file_count="multiple",
|
| 344 |
-
file_types=[".mp4", ".mov", ".avi"
|
| 345 |
-
height=
|
| 346 |
)
|
| 347 |
|
| 348 |
audio_input = gr.File(
|
| 349 |
label="🎵 选择背景音频",
|
| 350 |
-
file_types=[".mp3", ".wav", ".m4a"
|
| 351 |
-
height=
|
| 352 |
)
|
| 353 |
|
| 354 |
gr.Markdown("### ⚙️ 处理参数")
|
| 355 |
-
|
| 356 |
with gr.Row():
|
| 357 |
clip_duration = gr.Number(
|
| 358 |
-
label="
|
| 359 |
value=2,
|
| 360 |
minimum=1,
|
| 361 |
-
maximum=8
|
| 362 |
-
step=0.5,
|
| 363 |
-
info="每个视频片段的长度"
|
| 364 |
)
|
| 365 |
-
|
| 366 |
num_videos = gr.Number(
|
| 367 |
-
label="
|
| 368 |
value=3,
|
| 369 |
minimum=1,
|
| 370 |
-
maximum=6
|
| 371 |
-
step=1,
|
| 372 |
-
info="生成混剪视频的数量"
|
| 373 |
)
|
| 374 |
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
"🚀 开始 AI 智能处理",
|
| 378 |
variant="primary",
|
| 379 |
-
size="lg"
|
| 380 |
-
scale=1
|
| 381 |
)
|
| 382 |
-
|
| 383 |
-
# AI 模型信息
|
| 384 |
-
gr.Markdown("""
|
| 385 |
-
<div style="background: #f0f8ff; padding: 10px; border-radius: 8px; margin-top: 15px;">
|
| 386 |
-
<b>🤖 AI 模型信息:</b><br>
|
| 387 |
-
• 内容分析: GPT-4o-mini<br>
|
| 388 |
-
• 标题生成: GPT-4o-mini<br>
|
| 389 |
-
• 项目总结: GPT-4o<br>
|
| 390 |
-
• 成本优化: 智能模型选择
|
| 391 |
-
</div>
|
| 392 |
-
""")
|
| 393 |
|
| 394 |
-
# 右侧:状态显示和文件下载
|
| 395 |
with gr.Column(scale=1):
|
| 396 |
-
gr.Markdown("### 📊
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
lines=8,
|
| 401 |
-
max_lines=12,
|
| 402 |
interactive=False,
|
| 403 |
-
show_copy_button=True
|
| 404 |
-
placeholder="等待开始处理..."
|
| 405 |
-
)
|
| 406 |
-
|
| 407 |
-
gr.Markdown("### 📁 输出文件")
|
| 408 |
-
|
| 409 |
-
video_output = gr.File(
|
| 410 |
-
label="⬇️ 下载混剪视频",
|
| 411 |
-
file_count="multiple",
|
| 412 |
-
height=120,
|
| 413 |
-
interactive=False
|
| 414 |
)
|
| 415 |
|
| 416 |
-
# AI 分析结果展示区域
|
| 417 |
gr.Markdown("---")
|
| 418 |
-
gr.
|
| 419 |
|
| 420 |
with gr.Row():
|
| 421 |
with gr.Column():
|
| 422 |
-
gr.Markdown("### 📈 内容分析报告")
|
| 423 |
analysis_output = gr.Textbox(
|
| 424 |
-
label="
|
| 425 |
lines=6,
|
| 426 |
interactive=False,
|
| 427 |
-
show_copy_button=True
|
| 428 |
-
placeholder="AI 分析结果将在这里显示..."
|
| 429 |
)
|
| 430 |
|
| 431 |
with gr.Column():
|
| 432 |
-
gr.Markdown("### 🏷️ 创意标题推荐")
|
| 433 |
titles_output = gr.Textbox(
|
| 434 |
-
label="
|
| 435 |
lines=6,
|
| 436 |
interactive=False,
|
| 437 |
-
show_copy_button=True
|
| 438 |
-
placeholder="创意标题将在这里显示..."
|
| 439 |
)
|
| 440 |
|
| 441 |
-
# 项目总结区域
|
| 442 |
-
gr.Markdown("### 📋 AI 项目分析总结")
|
| 443 |
summary_output = gr.Textbox(
|
| 444 |
-
label="
|
| 445 |
lines=5,
|
| 446 |
interactive=False,
|
| 447 |
-
show_copy_button=True
|
| 448 |
-
placeholder="详细的项目分析总结将在这里显示..."
|
| 449 |
)
|
| 450 |
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
fn=process_videos_with_openai,
|
| 454 |
inputs=[video_input, audio_input, clip_duration, num_videos],
|
| 455 |
-
outputs=[
|
| 456 |
-
show_progress=True
|
| 457 |
)
|
| 458 |
|
| 459 |
-
# 使用指南
|
| 460 |
gr.Markdown("""
|
| 461 |
---
|
| 462 |
-
###
|
| 463 |
|
| 464 |
-
|
| 465 |
-
-
|
| 466 |
-
-
|
|
|
|
|
|
|
| 467 |
|
| 468 |
-
|
| 469 |
-
-
|
| 470 |
-
-
|
| 471 |
-
-
|
|
|
|
| 472 |
|
| 473 |
-
|
| 474 |
-
- ✨ GPT-4 智能内容分析
|
| 475 |
-
- 🎨 创意标题自动生成
|
| 476 |
-
- 📊 专业项目评估报告
|
| 477 |
-
- 💡 个性化优化建议
|
| 478 |
-
|
| 479 |
-
**⚡ 处理流程:**
|
| 480 |
-
1. 上传视频和音频文件
|
| 481 |
-
2. 调整处理参数
|
| 482 |
-
3. 点击开始处理
|
| 483 |
-
4. 等待 AI 分析完成
|
| 484 |
-
5. 下载结果文件
|
| 485 |
""")
|
| 486 |
|
| 487 |
-
# 启动应用
|
| 488 |
if __name__ == "__main__":
|
| 489 |
-
demo.launch(
|
| 490 |
-
share=False,
|
| 491 |
-
show_error=True,
|
| 492 |
-
show_tips=True
|
| 493 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
+
import shutil
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
# 配置 OpenAI API
|
|
|
|
| 10 |
api_key=os.getenv("sk-ZYyxdh3NexC863OUdicSlKrunXEPlRlsLBMwc1vXMGEVwZdb")
|
| 11 |
)
|
| 12 |
|
| 13 |
+
def analyze_video_content(video_count):
|
| 14 |
+
"""使用 OpenAI 分析视频内容(不依赖 MoviePy)"""
|
| 15 |
try:
|
| 16 |
+
prompt = f"""作为专业视频内容分析师,请为用户准备处理的 {video_count} 个视频文件生成详细指导建议。
|
| 17 |
|
| 18 |
+
请提供:
|
| 19 |
+
1. 📹 视频混剪的最佳实践方法
|
| 20 |
+
2. 🎵 音频匹配和同步技巧
|
| 21 |
+
3. ✂️ 推荐的剪辑软件和工具
|
| 22 |
+
4. 🎯 不同平台的视频规格要求
|
| 23 |
+
5. 📈 提升播放量的优化建议
|
| 24 |
|
| 25 |
+
用中文回答,格式清晰。"""
|
| 26 |
|
| 27 |
response = client.chat.completions.create(
|
| 28 |
+
model="gpt-4o-mini",
|
| 29 |
messages=[
|
| 30 |
+
{"role": "system", "content": "你是一位资深的视频制作专家。"},
|
| 31 |
+
{"role": "user", "content": prompt}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
],
|
| 33 |
max_tokens=600,
|
| 34 |
temperature=0.7
|
|
|
|
| 36 |
|
| 37 |
return response.choices[0].message.content
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"❌ AI 分析失败: {str(e)}"
|
| 40 |
|
| 41 |
+
def generate_creative_titles(video_count):
|
| 42 |
+
"""生成创意标题"""
|
| 43 |
try:
|
| 44 |
+
prompt = f"""为 {video_count} 个视频混剪作品生成吸引人的标题:
|
| 45 |
|
| 46 |
要求:
|
| 47 |
+
- 🔥 包含热门关键词
|
| 48 |
+
- 📱 适合短视频平台
|
| 49 |
+
- 💯 15-25字最佳长度
|
| 50 |
+
- 🎭 风格多样化
|
|
|
|
| 51 |
|
| 52 |
+
请给出 {video_count} 个标题,每行一个。"""
|
| 53 |
|
| 54 |
response = client.chat.completions.create(
|
| 55 |
model="gpt-4o-mini",
|
| 56 |
messages=[
|
| 57 |
+
{"role": "system", "content": "你是新媒体内容创作专家。"},
|
| 58 |
+
{"role": "user", "content": prompt}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
],
|
| 60 |
max_tokens=400,
|
| 61 |
+
temperature=0.9
|
| 62 |
)
|
| 63 |
|
| 64 |
return response.choices[0].message.content
|
| 65 |
except Exception as e:
|
| 66 |
return f"❌ 标题生成失败: {str(e)}"
|
| 67 |
|
| 68 |
+
def process_files(video_files, audio_file, clip_duration=2, num_output_videos=3):
|
| 69 |
+
"""处理文件(展示版本,不实际处理视频)"""
|
| 70 |
+
|
| 71 |
+
if not video_files:
|
| 72 |
+
return "❌ 请上传视频文件", "", "", ""
|
| 73 |
+
|
| 74 |
+
# 获取文件信息
|
| 75 |
+
video_info = []
|
| 76 |
+
for i, video_file in enumerate(video_files):
|
| 77 |
+
file_size = os.path.getsize(video_file.name) / (1024*1024) # MB
|
| 78 |
+
video_info.append(f"视频{i+1}: {os.path.basename(video_file.name)} ({file_size:.1f}MB)")
|
| 79 |
+
|
| 80 |
+
audio_info = ""
|
| 81 |
+
if audio_file:
|
| 82 |
+
audio_size = os.path.getsize(audio_file.name) / (1024*1024)
|
| 83 |
+
audio_info = f"音频: {os.path.basename(audio_file.name)} ({audio_size:.1f}MB)"
|
| 84 |
+
|
| 85 |
+
# 生成 AI 分析
|
| 86 |
+
video_analysis = analyze_video_content(len(video_files))
|
| 87 |
+
ai_titles = generate_creative_titles(num_output_videos)
|
| 88 |
+
|
| 89 |
+
# 生成处理建议
|
| 90 |
+
processing_guide = f"""
|
| 91 |
+
✨ 文件上传成功!
|
| 92 |
|
| 93 |
+
📊 上传统计:
|
| 94 |
+
{chr(10).join(video_info)}
|
| 95 |
+
{audio_info}
|
| 96 |
|
| 97 |
+
🔧 建议的处理方法:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
1. **使用专业软件**:
|
| 100 |
+
- 推荐:DaVinci Resolve (免费)
|
| 101 |
+
- 或者:Adobe Premiere Pro
|
| 102 |
+
- 轻量级:OpenShot
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
2. **处理参数**:
|
| 105 |
+
- 切片时长:{clip_duration}秒
|
| 106 |
+
- 输出数量:{num_output_videos}个
|
| 107 |
+
- 分辨率:1080p 建议
|
|
|
|
| 108 |
|
| 109 |
+
3. **导出设置**:
|
| 110 |
+
- 格式:MP4
|
| 111 |
+
- 编码:H.264
|
| 112 |
+
- 帧率:30fps
|
| 113 |
|
| 114 |
+
4. **上传平台优化**:
|
| 115 |
+
- 抖音:9:16竖屏
|
| 116 |
+
- 小红书:1:1方形
|
| 117 |
+
- B站:16:9横屏
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
💡 由于 Hugging Face CPU 环境限制,建议下载文件后使用本地软件处理。
|
| 120 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
project_summary = f"""
|
| 123 |
+
📋 项目分析总结:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
本次上传了 {len(video_files)} 个视频文件,计划生成 {num_output_videos} 个混剪作品。
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
🎯 制作建议:
|
| 128 |
+
• 保持统一的视觉风格
|
| 129 |
+
• 音频节拍匹配视频切换
|
| 130 |
+
• 添加转场效果增强观感
|
| 131 |
+
• 考虑平台特定的尺寸要求
|
|
|
|
| 132 |
|
| 133 |
+
📈 优化建议:
|
| 134 |
+
• 封面图设计要吸引眼球
|
| 135 |
+
• 标题包含热门关键词
|
| 136 |
+
• 发布时间选择高峰期
|
| 137 |
+
• 积极互动提升算法推荐
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
⚡ 下一步:请使用推荐的视频编辑软件进行实际处理。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
"""
|
| 141 |
+
|
| 142 |
+
return processing_guide, video_analysis, ai_titles, project_summary
|
| 143 |
+
|
| 144 |
+
# 创建 Gradio 界面
|
| 145 |
+
with gr.Blocks(
|
| 146 |
+
title="pyTovideo2 + OpenAI (Guide Mode)",
|
| 147 |
+
theme=gr.themes.Soft()
|
| 148 |
) as demo:
|
| 149 |
|
|
|
|
| 150 |
gr.HTML("""
|
| 151 |
+
<div style="text-align: center; margin-bottom: 20px; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); border-radius: 10px; color: white;">
|
| 152 |
+
<h1>🎬 pyTovideo2 + OpenAI GPT</h1>
|
| 153 |
+
<p style="margin: 0;">智能视频混剪指导助手 | 文件分析 + AI 建议</p>
|
| 154 |
+
<p style="margin: 5px 0 0 0; font-size: 0.9em; opacity: 0.8;">⚠️ CPU 环境限制,提供专业指导和 AI 分析</p>
|
| 155 |
</div>
|
| 156 |
""")
|
| 157 |
|
|
|
|
| 158 |
with gr.Row():
|
|
|
|
| 159 |
with gr.Column(scale=1):
|
| 160 |
+
gr.Markdown("### 📤 文件上传")
|
| 161 |
|
| 162 |
video_input = gr.File(
|
| 163 |
+
label="🎥 选择视频文件",
|
| 164 |
file_count="multiple",
|
| 165 |
+
file_types=[".mp4", ".mov", ".avi"],
|
| 166 |
+
height=120
|
| 167 |
)
|
| 168 |
|
| 169 |
audio_input = gr.File(
|
| 170 |
label="🎵 选择背景音频",
|
| 171 |
+
file_types=[".mp3", ".wav", ".m4a"],
|
| 172 |
+
height=80
|
| 173 |
)
|
| 174 |
|
| 175 |
gr.Markdown("### ⚙️ 处理参数")
|
|
|
|
| 176 |
with gr.Row():
|
| 177 |
clip_duration = gr.Number(
|
| 178 |
+
label="切片时长(秒)",
|
| 179 |
value=2,
|
| 180 |
minimum=1,
|
| 181 |
+
maximum=8
|
|
|
|
|
|
|
| 182 |
)
|
|
|
|
| 183 |
num_videos = gr.Number(
|
| 184 |
+
label="输出数量",
|
| 185 |
value=3,
|
| 186 |
minimum=1,
|
| 187 |
+
maximum=6
|
|
|
|
|
|
|
| 188 |
)
|
| 189 |
|
| 190 |
+
analyze_btn = gr.Button(
|
| 191 |
+
"🔍 AI 分析文件",
|
|
|
|
| 192 |
variant="primary",
|
| 193 |
+
size="lg"
|
|
|
|
| 194 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
|
|
|
| 196 |
with gr.Column(scale=1):
|
| 197 |
+
gr.Markdown("### 📊 处理指导")
|
| 198 |
+
guide_output = gr.Textbox(
|
| 199 |
+
label="📋 处理建议",
|
| 200 |
+
lines=10,
|
|
|
|
|
|
|
| 201 |
interactive=False,
|
| 202 |
+
show_copy_button=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
)
|
| 204 |
|
|
|
|
| 205 |
gr.Markdown("---")
|
| 206 |
+
gr.Markdown("## 🤖 OpenAI GPT 智能分析")
|
| 207 |
|
| 208 |
with gr.Row():
|
| 209 |
with gr.Column():
|
|
|
|
| 210 |
analysis_output = gr.Textbox(
|
| 211 |
+
label="📈 内容分析",
|
| 212 |
lines=6,
|
| 213 |
interactive=False,
|
| 214 |
+
show_copy_button=True
|
|
|
|
| 215 |
)
|
| 216 |
|
| 217 |
with gr.Column():
|
|
|
|
| 218 |
titles_output = gr.Textbox(
|
| 219 |
+
label="🏷️ 创意标题",
|
| 220 |
lines=6,
|
| 221 |
interactive=False,
|
| 222 |
+
show_copy_button=True
|
|
|
|
| 223 |
)
|
| 224 |
|
|
|
|
|
|
|
| 225 |
summary_output = gr.Textbox(
|
| 226 |
+
label="📋 项目总结",
|
| 227 |
lines=5,
|
| 228 |
interactive=False,
|
| 229 |
+
show_copy_button=True
|
|
|
|
| 230 |
)
|
| 231 |
|
| 232 |
+
analyze_btn.click(
|
| 233 |
+
fn=process_files,
|
|
|
|
| 234 |
inputs=[video_input, audio_input, clip_duration, num_videos],
|
| 235 |
+
outputs=[guide_output, analysis_output, titles_output, summary_output]
|
|
|
|
| 236 |
)
|
| 237 |
|
|
|
|
| 238 |
gr.Markdown("""
|
| 239 |
---
|
| 240 |
+
### 💡 使用说明
|
| 241 |
|
| 242 |
+
**当前模式**:AI 分析和指导模式
|
| 243 |
+
- ✅ 文件信息分析
|
| 244 |
+
- ✅ AI 内容建议
|
| 245 |
+
- ✅ 创意标题生成
|
| 246 |
+
- ✅ 专业处理指导
|
| 247 |
|
| 248 |
+
**推荐工具**:
|
| 249 |
+
- 🆓 DaVinci Resolve (专业免费)
|
| 250 |
+
- 💰 Adobe Premiere Pro (付费专业)
|
| 251 |
+
- 🔧 OpenShot (简单免费)
|
| 252 |
+
- 📱 手机端:剪映、快影
|
| 253 |
|
| 254 |
+
**获取完整功能**:建议在本地环境安装 MoviePy 进行实际视频处理。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
""")
|
| 256 |
|
|
|
|
| 257 |
if __name__ == "__main__":
|
| 258 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|