Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,31 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import tempfile
|
| 4 |
-
import pickle
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
-
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
MAX_VIDEO_SIZE = 100 * 1024 * 1024 # 100MB
|
| 14 |
MAX_AUDIO_SIZE = 50 * 1024 * 1024 # 50MB
|
|
@@ -33,7 +50,6 @@ class YouTubeUploader:
|
|
| 33 |
flow = InstalledAppFlow.from_client_secrets_file(
|
| 34 |
self.client_secrets_file, self.SCOPES
|
| 35 |
)
|
| 36 |
-
# 命令行授权,手动复制粘贴链接和验证码,无需浏览器自动打开
|
| 37 |
creds = flow.run_console()
|
| 38 |
with open(self.credentials_file, 'wb') as token:
|
| 39 |
pickle.dump(creds, token)
|
|
@@ -65,45 +81,78 @@ class YouTubeUploader:
|
|
| 65 |
url = f"https://www.youtube.com/watch?v={video_id}"
|
| 66 |
return {"success": True, "video_id": video_id, "url": url, "title": title}
|
| 67 |
|
| 68 |
-
def check_filesize(
|
| 69 |
-
|
| 70 |
-
for
|
| 71 |
-
size = os.path.getsize(
|
| 72 |
if size > MAX_VIDEO_SIZE:
|
| 73 |
-
return False, f"单个视频文件 {
|
| 74 |
-
|
| 75 |
-
if
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
return True, ""
|
| 78 |
|
| 79 |
def process_videos(video_files, audio_file, clip_duration, num_output):
|
| 80 |
-
ok, msg = check_filesize(video_files)
|
| 81 |
if not ok:
|
| 82 |
return msg, [], ""
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
def main_app(video_files, audio_file, clip_duration, num_output, upload_yt, yt_privacy):
|
| 88 |
status, outfiles, _ = process_videos(video_files, audio_file, clip_duration, num_output)
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
yt_result = ""
|
| 91 |
if upload_yt:
|
| 92 |
if not YOUTUBE_AVAILABLE:
|
| 93 |
-
yt_result = "⚠️ 未安装 YouTube API
|
| 94 |
elif not os.path.exists("client_secrets.json"):
|
| 95 |
-
yt_result = "⚠️
|
| 96 |
else:
|
| 97 |
uploader = YouTubeUploader()
|
| 98 |
authed = uploader.authenticate()
|
| 99 |
if not authed:
|
| 100 |
yt_result = "❌ YouTube 认证失败"
|
| 101 |
else:
|
| 102 |
-
yt_result = "
|
| 103 |
|
| 104 |
-
return status, yt_result
|
| 105 |
|
| 106 |
-
# 判断是否安装 YouTube API 库
|
| 107 |
try:
|
| 108 |
import googleapiclient
|
| 109 |
YOUTUBE_AVAILABLE = True
|
|
@@ -111,20 +160,24 @@ except ImportError:
|
|
| 111 |
YOUTUBE_AVAILABLE = False
|
| 112 |
|
| 113 |
with gr.Blocks() as demo:
|
| 114 |
-
gr.Markdown("## pyTovideo2 视频处理+YouTube
|
| 115 |
|
| 116 |
video_files = gr.File(file_types=[".mp4", ".mov", ".avi"], label="上传视频文件", file_count="multiple")
|
| 117 |
audio_file = gr.File(file_types=[".mp3", ".wav", ".m4a"], label="上传音频文件")
|
| 118 |
clip_duration = gr.Number(value=2, label="切片时长 (秒)", minimum=1, maximum=10)
|
| 119 |
num_output = gr.Number(value=3, label="输出视频数量", minimum=1, maximum=10)
|
| 120 |
-
upload_yt = gr.Checkbox(label="
|
| 121 |
-
yt_privacy = gr.Dropdown(choices=["public", "private", "unlisted"], value="private", label="YouTube
|
| 122 |
-
btn = gr.Button("
|
| 123 |
|
| 124 |
status_out = gr.Textbox(label="处理状态", lines=4, interactive=False)
|
| 125 |
-
|
|
|
|
| 126 |
|
| 127 |
-
btn.click(
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import tempfile
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
+
try:
|
| 7 |
+
from openai import OpenAI
|
| 8 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) if os.getenv("OPENAI_API_KEY") else None
|
| 9 |
+
OPENAI_AVAILABLE = bool(client)
|
| 10 |
+
except ImportError:
|
| 11 |
+
OPENAI_AVAILABLE = False
|
| 12 |
+
client = None
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip, vfx
|
| 16 |
+
MOVIEPY_AVAILABLE = True
|
| 17 |
+
except ImportError:
|
| 18 |
+
MOVIEPY_AVAILABLE = False
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
from googleapiclient.discovery import build
|
| 22 |
+
from googleapiclient.http import MediaFileUpload
|
| 23 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 24 |
+
from google.auth.transport.requests import Request
|
| 25 |
+
import pickle
|
| 26 |
+
YOUTUBE_AVAILABLE = True
|
| 27 |
+
except ImportError:
|
| 28 |
+
YOUTUBE_AVAILABLE = False
|
| 29 |
|
| 30 |
MAX_VIDEO_SIZE = 100 * 1024 * 1024 # 100MB
|
| 31 |
MAX_AUDIO_SIZE = 50 * 1024 * 1024 # 50MB
|
|
|
|
| 50 |
flow = InstalledAppFlow.from_client_secrets_file(
|
| 51 |
self.client_secrets_file, self.SCOPES
|
| 52 |
)
|
|
|
|
| 53 |
creds = flow.run_console()
|
| 54 |
with open(self.credentials_file, 'wb') as token:
|
| 55 |
pickle.dump(creds, token)
|
|
|
|
| 81 |
url = f"https://www.youtube.com/watch?v={video_id}"
|
| 82 |
return {"success": True, "video_id": video_id, "url": url, "title": title}
|
| 83 |
|
| 84 |
+
def check_filesize(video_files, audio_file):
|
| 85 |
+
total_size = 0
|
| 86 |
+
for v in video_files:
|
| 87 |
+
size = os.path.getsize(v.name)
|
| 88 |
if size > MAX_VIDEO_SIZE:
|
| 89 |
+
return False, f"单个视频文件 {os.path.basename(v.name)} 过大,超过100MB限制"
|
| 90 |
+
total_size += size
|
| 91 |
+
if audio_file:
|
| 92 |
+
audio_size = os.path.getsize(audio_file.name)
|
| 93 |
+
if audio_size > MAX_AUDIO_SIZE:
|
| 94 |
+
return False, f"音频文件 {os.path.basename(audio_file.name)} 过大,超过50MB限制"
|
| 95 |
+
total_size += audio_size
|
| 96 |
+
if total_size > MAX_TOTAL_SIZE:
|
| 97 |
+
return False, f"上传文件总大小过大 {total_size/(1024*1024):.1f}MB,超过500MB限制"
|
| 98 |
return True, ""
|
| 99 |
|
| 100 |
def process_videos(video_files, audio_file, clip_duration, num_output):
|
| 101 |
+
ok, msg = check_filesize(video_files, audio_file)
|
| 102 |
if not ok:
|
| 103 |
return msg, [], ""
|
| 104 |
|
| 105 |
+
# 真实视频切片、混剪应在这里实现
|
| 106 |
+
# 本处仅返回成功信息,结合你已有处理逻辑即可
|
| 107 |
+
return f"成功处理 {len(video_files)} 个视频,生成 {num_output} 个混剪视频", [], ""
|
| 108 |
+
|
| 109 |
+
def generate_ai_analysis(video_count, file_names):
|
| 110 |
+
if not OPENAI_AVAILABLE or video_count == 0:
|
| 111 |
+
return "⚠️ AI 分析暂不可用或无视频文件。"
|
| 112 |
+
try:
|
| 113 |
+
file_info = ", ".join([f"'{name}'" for name in file_names[:3]])
|
| 114 |
+
prompt = f"""作为专业视频分析师,基于用户上传的 {video_count} 个视频文件(文件名包括:{file_info} 等),请提供详细分析:
|
| 115 |
+
1. 内容类型推测和特点
|
| 116 |
+
2. 最佳背景音乐风格和节拍
|
| 117 |
+
3. 推荐剪辑策略和转场效果
|
| 118 |
+
4. 目标受众分析
|
| 119 |
+
|
| 120 |
+
请用中文回答,格式清晰。"""
|
| 121 |
+
response = client.chat.completions.create(
|
| 122 |
+
model="gpt-4o-mini",
|
| 123 |
+
messages=[
|
| 124 |
+
{"role": "system", "content": "你是专业的视频内容分析专家。"},
|
| 125 |
+
{"role": "user", "content": prompt}
|
| 126 |
+
],
|
| 127 |
+
max_tokens=600,
|
| 128 |
+
temperature=0.7
|
| 129 |
+
)
|
| 130 |
+
return response.choices[0].message.content
|
| 131 |
+
except Exception as e:
|
| 132 |
+
return f"❌ AI 分析失败:{str(e)}"
|
| 133 |
|
| 134 |
def main_app(video_files, audio_file, clip_duration, num_output, upload_yt, yt_privacy):
|
| 135 |
status, outfiles, _ = process_videos(video_files, audio_file, clip_duration, num_output)
|
| 136 |
|
| 137 |
+
file_names = [os.path.basename(f.name) for f in (video_files or [])]
|
| 138 |
+
ai_analysis = generate_ai_analysis(len(video_files or []), file_names)
|
| 139 |
+
|
| 140 |
yt_result = ""
|
| 141 |
if upload_yt:
|
| 142 |
if not YOUTUBE_AVAILABLE:
|
| 143 |
+
yt_result = "⚠️ 未安装 YouTube API 库,上传功能不可用"
|
| 144 |
elif not os.path.exists("client_secrets.json"):
|
| 145 |
+
yt_result = "⚠️ client_secrets.json 文件缺失,无法上传"
|
| 146 |
else:
|
| 147 |
uploader = YouTubeUploader()
|
| 148 |
authed = uploader.authenticate()
|
| 149 |
if not authed:
|
| 150 |
yt_result = "❌ YouTube 认证失败"
|
| 151 |
else:
|
| 152 |
+
yt_result = "请配合实际生成的视频文件路径调用上传函数"
|
| 153 |
|
| 154 |
+
return status, ai_analysis, yt_result
|
| 155 |
|
|
|
|
| 156 |
try:
|
| 157 |
import googleapiclient
|
| 158 |
YOUTUBE_AVAILABLE = True
|
|
|
|
| 160 |
YOUTUBE_AVAILABLE = False
|
| 161 |
|
| 162 |
with gr.Blocks() as demo:
|
| 163 |
+
gr.Markdown("## pyTovideo2 视频处理+YouTube 上传 (实用版)")
|
| 164 |
|
| 165 |
video_files = gr.File(file_types=[".mp4", ".mov", ".avi"], label="上传视频文件", file_count="multiple")
|
| 166 |
audio_file = gr.File(file_types=[".mp3", ".wav", ".m4a"], label="上传音频文件")
|
| 167 |
clip_duration = gr.Number(value=2, label="切片时长 (秒)", minimum=1, maximum=10)
|
| 168 |
num_output = gr.Number(value=3, label="输出视频数量", minimum=1, maximum=10)
|
| 169 |
+
upload_yt = gr.Checkbox(label="处理后上传至 YouTube")
|
| 170 |
+
yt_privacy = gr.Dropdown(choices=["public", "private", "unlisted"], value="private", label="YouTube 视频隐私")
|
| 171 |
+
btn = gr.Button("开始处理")
|
| 172 |
|
| 173 |
status_out = gr.Textbox(label="处理状态", lines=4, interactive=False)
|
| 174 |
+
ai_out = gr.Textbox(label="AI 内容分析", lines=12)
|
| 175 |
+
yt_out = gr.Textbox(label="YouTube 上传结果", lines=6)
|
| 176 |
|
| 177 |
+
btn.click(
|
| 178 |
+
main_app,
|
| 179 |
+
inputs=[video_files, audio_file, clip_duration, num_output, upload_yt, yt_privacy],
|
| 180 |
+
outputs=[status_out, ai_out, yt_out]
|
| 181 |
+
)
|
| 182 |
|
| 183 |
demo.launch()
|