Py / app.py
Ryanus's picture
Update app.py
4d31d47 verified
raw
history blame
7.8 kB
import gradio as gr
import os
import tempfile
import shutil
from datetime import datetime
from openai import OpenAI
# 配置 OpenAI API
client = OpenAI(
api_key=os.getenv(sk-ZYyxdh3NexC863OUdicSlKrunXEPlRlsLBMwc1vXMGEVwZdb)
)
def analyze_video_content(video_count):
"""使用 OpenAI 分析视频内容(不依赖 MoviePy)"""
try:
prompt = f"""作为专业视频内容分析师,请为用户准备处理的 {video_count} 个视频文件生成详细指导建议。
请提供:
1. 📹 视频混剪的最佳实践方法
2. 🎵 音频匹配和同步技巧
3. ✂️ 推荐的剪辑软件和工具
4. 🎯 不同平台的视频规格要求
5. 📈 提升播放量的优化建议
用中文回答,格式清晰。"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "你是一位资深的视频制作专家。"},
{"role": "user", "content": prompt}
],
max_tokens=600,
temperature=0.7
)
return response.choices[0].message.content
except Exception as e:
return f"❌ AI 分析失败: {str(e)}"
def generate_creative_titles(video_count):
"""生成创意标题"""
try:
prompt = f"""为 {video_count} 个视频混剪作品生成吸引人的标题:
要求:
- 🔥 包含热门关键词
- 📱 适合短视频平台
- 💯 15-25字最佳长度
- 🎭 风格多样化
请给出 {video_count} 个标题,每行一个。"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "你是新媒体内容创作专家。"},
{"role": "user", "content": prompt}
],
max_tokens=400,
temperature=0.9
)
return response.choices[0].message.content
except Exception as e:
return f"❌ 标题生成失败: {str(e)}"
def process_files(video_files, audio_file, clip_duration=2, num_output_videos=3):
"""处理文件(展示版本,不实际处理视频)"""
if not video_files:
return "❌ 请上传视频文件", "", "", ""
# 获取文件信息
video_info = []
for i, video_file in enumerate(video_files):
file_size = os.path.getsize(video_file.name) / (1024*1024) # MB
video_info.append(f"视频{i+1}: {os.path.basename(video_file.name)} ({file_size:.1f}MB)")
audio_info = ""
if audio_file:
audio_size = os.path.getsize(audio_file.name) / (1024*1024)
audio_info = f"音频: {os.path.basename(audio_file.name)} ({audio_size:.1f}MB)"
# 生成 AI 分析
video_analysis = analyze_video_content(len(video_files))
ai_titles = generate_creative_titles(num_output_videos)
# 生成处理建议
processing_guide = f"""
✨ 文件上传成功!
📊 上传统计:
{chr(10).join(video_info)}
{audio_info}
🔧 建议的处理方法:
1. **使用专业软件**:
- 推荐:DaVinci Resolve (免费)
- 或者:Adobe Premiere Pro
- 轻量级:OpenShot
2. **处理参数**:
- 切片时长:{clip_duration}
- 输出数量:{num_output_videos}
- 分辨率:1080p 建议
3. **导出设置**:
- 格式:MP4
- 编码:H.264
- 帧率:30fps
4. **上传平台优化**:
- 抖音:9:16竖屏
- 小红书:1:1方形
- B站:16:9横屏
💡 由于 Hugging Face CPU 环境限制,建议下载文件后使用本地软件处理。
"""
project_summary = f"""
📋 项目分析总结:
本次上传了 {len(video_files)} 个视频文件,计划生成 {num_output_videos} 个混剪作品。
🎯 制作建议:
• 保持统一的视觉风格
• 音频节拍匹配视频切换
• 添加转场效果增强观感
• 考虑平台特定的尺寸要求
📈 优化建议:
• 封面图设计要吸引眼球
• 标题包含热门关键词
• 发布时间选择高峰期
• 积极互动提升算法推荐
⚡ 下一步:请使用推荐的视频编辑软件进行实际处理。
"""
return processing_guide, video_analysis, ai_titles, project_summary
# 创建 Gradio 界面
with gr.Blocks(
title="pyTovideo2 + OpenAI (Guide Mode)",
theme=gr.themes.Soft()
) as demo:
gr.HTML("""
<div style="text-align: center; margin-bottom: 20px; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); border-radius: 10px; color: white;">
<h1>🎬 pyTovideo2 + OpenAI GPT</h1>
<p style="margin: 0;">智能视频混剪指导助手 | 文件分析 + AI 建议</p>
<p style="margin: 5px 0 0 0; font-size: 0.9em; opacity: 0.8;">⚠️ CPU 环境限制,提供专业指导和 AI 分析</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### 📤 文件上传")
video_input = gr.File(
label="🎥 选择视频文件",
file_count="multiple",
file_types=[".mp4", ".mov", ".avi"],
height=120
)
audio_input = gr.File(
label="🎵 选择背景音频",
file_types=[".mp3", ".wav", ".m4a"],
height=80
)
gr.Markdown("### ⚙️ 处理参数")
with gr.Row():
clip_duration = gr.Number(
label="切片时长(秒)",
value=2,
minimum=1,
maximum=8
)
num_videos = gr.Number(
label="输出数量",
value=3,
minimum=1,
maximum=6
)
analyze_btn = gr.Button(
"🔍 AI 分析文件",
variant="primary",
size="lg"
)
with gr.Column(scale=1):
gr.Markdown("### 📊 处理指导")
guide_output = gr.Textbox(
label="📋 处理建议",
lines=10,
interactive=False,
show_copy_button=True
)
gr.Markdown("---")
gr.Markdown("## 🤖 OpenAI GPT 智能分析")
with gr.Row():
with gr.Column():
analysis_output = gr.Textbox(
label="📈 内容分析",
lines=6,
interactive=False,
show_copy_button=True
)
with gr.Column():
titles_output = gr.Textbox(
label="🏷️ 创意标题",
lines=6,
interactive=False,
show_copy_button=True
)
summary_output = gr.Textbox(
label="📋 项目总结",
lines=5,
interactive=False,
show_copy_button=True
)
analyze_btn.click(
fn=process_files,
inputs=[video_input, audio_input, clip_duration, num_videos],
outputs=[guide_output, analysis_output, titles_output, summary_output]
)
gr.Markdown("""
---
### 💡 使用说明
**当前模式**:AI 分析和指导模式
- ✅ 文件信息分析
- ✅ AI 内容建议
- ✅ 创意标题生成
- ✅ 专业处理指导
**推荐工具**:
- 🆓 DaVinci Resolve (专业免费)
- 💰 Adobe Premiere Pro (付费专业)
- 🔧 OpenShot (简单免费)
- 📱 手机端:剪映、快影
**获取完整功能**:建议在本地环境安装 MoviePy 进行实际视频处理。
""")
if __name__ == "__main__":
demo.launch()