'commit'
This commit is contained in:
@@ -7,6 +7,10 @@ from fastapi import HTTPException
|
||||
from JiMeng.Kit.FenJingTouGenerator import FenJingTouGenerator # 导入分镜头生成器
|
||||
from JiMeng.Kit.JmImg2VideoUtil import JmImg2Video # 导入视频生成工具
|
||||
from JiMeng.Kit.JmTxt2ImgUtil import JmTxt2Img
|
||||
import os
|
||||
import uuid
|
||||
import requests
|
||||
from Util.ObsUtil import ObsUploader
|
||||
|
||||
# 创建路由路由器
|
||||
router = APIRouter(prefix="/api/jimeng", tags=["即梦"])
|
||||
@@ -145,3 +149,73 @@ async def query_video_task(request: fastapi.Request):
|
||||
logger.error(f"视频任务查询失败: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"视频任务查询失败: {str(e)}")
|
||||
|
||||
|
||||
@router.post("/upload_url_to_obs")
|
||||
async def upload_url_to_obs(request: fastapi.Request):
|
||||
try:
|
||||
data = await request.json()
|
||||
url = data.get("url")
|
||||
|
||||
if not url:
|
||||
raise HTTPException(status_code=400, detail="缺少URL参数")
|
||||
|
||||
logger.info(f"收到URL上传请求,URL: {url}")
|
||||
|
||||
# 使用UUID生成唯一文件名
|
||||
unique_id = uuid.uuid4()
|
||||
object_key = f"HuangHai/JiMeng/{unique_id}.mp4"
|
||||
|
||||
# 临时文件保存路径
|
||||
temp_file_path = os.path.join(os.path.dirname(__file__), f"{unique_id}.mp4")
|
||||
|
||||
try:
|
||||
# 下载URL内容到临时文件
|
||||
logger.info(f"开始下载URL内容: {url}")
|
||||
with requests.get(url, stream=True, timeout=3600) as r:
|
||||
r.raise_for_status()
|
||||
with open(temp_file_path, 'wb') as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
logger.info(f"URL内容下载完成,保存至: {temp_file_path}")
|
||||
|
||||
# 上传文件到OBS
|
||||
obs_uploader = ObsUploader()
|
||||
logger.info(f"开始上传文件到OBS: {temp_file_path}\n对象键: {object_key}")
|
||||
|
||||
# 正确处理元组返回值
|
||||
success, response_info = obs_uploader.upload_file(
|
||||
file_path=temp_file_path,
|
||||
object_key=object_key
|
||||
)
|
||||
|
||||
if success:
|
||||
# 构造OBS URL
|
||||
from Config.Config import OBS_SERVER, OBS_BUCKET
|
||||
obs_url = f"https://{OBS_BUCKET}.{OBS_SERVER}/{object_key}"
|
||||
logger.info(f"文件上传成功,OBS URL: {obs_url}")
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
"data": {
|
||||
"obs_url": obs_url,
|
||||
"object_key": object_key
|
||||
}
|
||||
}
|
||||
else:
|
||||
error_msg = f"上传失败: {str(response_info)}"
|
||||
logger.error(error_msg)
|
||||
raise HTTPException(status_code=500, detail=error_msg)
|
||||
|
||||
finally:
|
||||
# 清理临时文件
|
||||
if os.path.exists(temp_file_path):
|
||||
os.remove(temp_file_path)
|
||||
logger.info(f"临时文件已删除: {temp_file_path}")
|
||||
|
||||
except HTTPException as e:
|
||||
logger.error(f"请求参数错误: {str(e.detail)}")
|
||||
raise e
|
||||
except Exception as e:
|
||||
logger.error(f"URL上传处理失败: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"URL上传处理失败: {str(e)}")
|
||||
|
||||
|
Binary file not shown.
@@ -48,7 +48,8 @@
|
||||
}
|
||||
.video-player {
|
||||
max-width: 100%;
|
||||
max-height: 600px;
|
||||
max-height: 800px; /* 增加最大高度 */
|
||||
width: 100%; /* 宽度充满容器 */
|
||||
border-radius: 6px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
display: none;
|
||||
@@ -186,12 +187,51 @@
|
||||
// 视频生成完成
|
||||
loading.style.display = 'none';
|
||||
progress.style.width = '100%';
|
||||
progressText.textContent = '生成完成';
|
||||
progressText.textContent = '生成完成,正在保存到OBS...';
|
||||
|
||||
// 设置视频源
|
||||
videoPlayer.src = statusInfo.video_url;
|
||||
videoPlayer.style.display = 'block';
|
||||
downloadBtn.style.display = 'inline-block';
|
||||
// 调用接口将视频保存到OBS
|
||||
fetch('/api/jimeng/upload_url_to_obs', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
url: statusInfo.video_url,
|
||||
object_key_prefix: 'jimeng_videos/'
|
||||
})
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('保存到OBS失败');
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(obsData => {
|
||||
if (obsData.code === 200) {
|
||||
// 获取OBS下载URL
|
||||
const obsVideoUrl = obsData.data.obs_url;
|
||||
progressText.textContent = '已保存到OBS';
|
||||
|
||||
// 设置视频源为OBS URL
|
||||
videoPlayer.src = obsVideoUrl;
|
||||
videoPlayer.style.display = 'block';
|
||||
downloadBtn.style.display = 'inline-block';
|
||||
} else {
|
||||
progressText.textContent = '保存到OBS失败: ' + obsData.message;
|
||||
// 仍然使用原始URL播放
|
||||
videoPlayer.src = statusInfo.video_url;
|
||||
videoPlayer.style.display = 'block';
|
||||
downloadBtn.style.display = 'inline-block';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('保存到OBS时出错:', error);
|
||||
progressText.textContent = '保存到OBS出错: ' + error.message;
|
||||
// 仍然使用原始URL播放
|
||||
videoPlayer.src = statusInfo.video_url;
|
||||
videoPlayer.style.display = 'block';
|
||||
downloadBtn.style.display = 'inline-block';
|
||||
});
|
||||
} else if (statusInfo.status === 'failed') {
|
||||
// 视频生成失败
|
||||
loading.textContent = '视频生成失败: ' + statusInfo.error_msg;
|
||||
|
Reference in New Issue
Block a user