修复add_videos.py中添加视频位置不生效的问题。

This commit is contained in:
Hommy
2025-12-05 23:22:13 +08:00
parent 42d4da7505
commit ec4fd4163d
2 changed files with 89 additions and 15 deletions
+19 -15
View File
@@ -25,7 +25,6 @@ def add_videos(
) -> Tuple[str, str, List[str], List[str]]:
"""
添加视频到剪映草稿的业务逻辑
文档:https://jy-api.fyshark.com/docs/API_ADD_VIDEOS.md
Args:
draft_url: "" // [必选] 草稿URL
@@ -88,7 +87,9 @@ def add_videos(
# 6. 遍历视频信息,添加视频到草稿中的指定轨道,收集片段ID
segment_ids = []
for video in videos:
segment_id = add_video_to_draft(script, track_name, draft_video_dir=draft_video_dir, video=video)
segment_id = add_video_to_draft(script, track_name, draft_video_dir=draft_video_dir, video=video,
alpha=alpha, scale_x=scale_x, scale_y=scale_y,
transform_x=transform_x, transform_y=transform_y)
segment_ids.append(segment_id)
logger.info(f"segment_ids: {segment_ids}")
@@ -125,18 +126,10 @@ def add_video_to_draft(
向剪映草稿中添加视频
Args:
draft_id: 草稿ID(草稿文件夹名称)
video:
video_url: 视频URL
width: 视频宽度,
height: 视频高度,
start: 开始时间,
end: 结束时间,
duration: 视频总时长,
mask: 遮罩类型
transition: 转场
transition_duration: 转场时长
volume: 音量
script: 草稿文件对象
track_name: 视频轨道名称
draft_video_dir: 视频资源目录
video: 视频信息字典
alpha: 视频透明度
scale_x: 横向缩放
scale_y: 纵向缩放
@@ -152,10 +145,21 @@ def add_video_to_draft(
# 1. 创建视频素材并添加到草稿
duration = video['end'] - video['start']
# 创建图像调节设置
clip_settings = draft.ClipSettings(
alpha=alpha,
scale_x=scale_x,
scale_y=scale_y,
transform_x=transform_x / video['width'], # 转换为半画布宽单位
transform_y=transform_y / video['height'] # 转换为半画布高单位
)
video_segment = draft.VideoSegment(
material=video_path,
target_timerange=trange(start=video['start'], duration=duration),
volume=video['volume']
volume=video['volume'],
clip_settings=clip_settings
)
logger.info(f"material_id: {video_segment.material_instance.material_id}")
logger.info(f"video_path: {video_path}, start: {video['start']}, duration: {duration}, volume: {video['volume']}")
+70
View File
@@ -0,0 +1,70 @@
import sys
import os
import json
import requests
# 添加项目根目录到 Python 路径
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
def test_video_transform():
"""测试视频位置变换功能"""
# 1. 先创建一个草稿
create_draft_url = "http://localhost:8000/v1/create_draft"
create_draft_data = {
"width": 1920,
"height": 1080
}
try:
print("Creating draft...")
create_response = requests.post(create_draft_url, json=create_draft_data)
if create_response.status_code != 200:
print(f"Failed to create draft: {create_response.status_code}")
print(create_response.text)
return
draft_url = create_response.json()["draft_url"]
print(f"Draft created successfully: {draft_url}")
# 2. 添加带位置变换的视频
add_videos_url = "http://localhost:8000/v1/add_videos"
video_infos = [
{
"video_url": "https://example.com/test.mp4",
"width": 1920,
"height": 1080,
"start": 0,
"end": 5000000, # 5秒
"duration": 5000000
}
]
add_videos_data = {
"draft_url": draft_url,
"video_infos": json.dumps(video_infos),
"transform_x": 100, # X轴位置偏移100像素
"transform_y": -50 # Y轴位置偏移-50像素
}
print("Adding videos with position transformation...")
add_response = requests.post(add_videos_url, json=add_videos_data)
if add_response.status_code == 200:
result = add_response.json()
print(f"Videos added successfully!")
print(f"Track ID: {result['track_id']}")
print(f"Video IDs: {result['video_ids']}")
print(f"Segment IDs: {result['segment_ids']}")
print(f"Draft URL: {result['draft_url']}")
else:
print(f"Failed to add videos: {add_response.status_code}")
print(add_response.text)
except Exception as e:
print(f"Error occurred: {e}")
print("Make sure the server is running on http://localhost:8000")
if __name__ == "__main__":
test_video_transform()