Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions dashscope/aigc/video_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def call(cls,
negative_prompt: str = None,
template: str = None,
img_url: str = None,
audio_url: str = None,
api_key: str = None,
extra_input: Dict = None,
workspace: str = None,
Expand All @@ -56,6 +57,7 @@ def call(cls,
negative_prompt (str): The negative prompt is the opposite of the prompt meaning.
template (str): LoRa input, such as gufeng, katong, etc.
img_url (str): The input image url, Generate the URL of the image referenced by the video.
audio_url (str): The input audio url
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The docstring for audio_url is a bit brief. For better clarity and consistency with other parameters like img_url, consider providing a more descriptive explanation of its purpose. Also, this docstring is missing a period at the end. This feedback also applies to the audio_url docstrings on lines 209, 378, and 432.

Suggested change
audio_url (str): The input audio url
audio_url (str): The input audio URL, used for video generation.

api_key (str, optional): The api api_key. Defaults to None.
workspace (str): The dashscope workspace id.
extra_input (Dict): The extra input parameters.
Expand All @@ -76,6 +78,7 @@ def call(cls,
return super().call(model,
prompt,
img_url=img_url,
audio_url=audio_url,
api_key=api_key,
extend_prompt=extend_prompt,
negative_prompt=negative_prompt,
Expand All @@ -94,6 +97,7 @@ def _get_input(cls,
model: str,
prompt: Any = None,
img_url: str = None,
audio_url: str = None,
# """@deprecated, use prompt_extend in parameters """
extend_prompt: bool = True,
negative_prompt: str = None,
Expand Down Expand Up @@ -125,6 +129,13 @@ def _get_input(cls,
has_upload = True
inputs['img_url'] = res_img_url

if audio_url is not None and audio_url:
is_upload, res_audio_url = check_and_upload_local(
model, audio_url, api_key)
if is_upload:
has_upload = True
inputs['audio_url'] = res_audio_url
Comment on lines +132 to +137
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic for handling audio_url is very similar to the logic for other URL-based parameters like img_url, head_frame, etc., leading to code duplication. This can make the code harder to maintain and prone to errors if changes are not applied consistently across all blocks.

To improve maintainability, consider refactoring this repeated logic into a private helper method. For example:

@classmethod
def _maybe_upload_and_set_input(cls, inputs, key, url, model, api_key):
    if not url:
        return False

    is_upload, res_url = check_and_upload_local(model, url, api_key)
    inputs[key] = res_url
    return is_upload

Then you could use it like this:

if cls._maybe_upload_and_set_input(inputs, 'audio_url', audio_url, model, api_key):
    has_upload = True

This would make the _get_input method much cleaner and easier to read. Also, the check if audio_url is not None and audio_url: can be simplified to if audio_url:.


if head_frame is not None and head_frame:
is_upload, res_head_frame = check_and_upload_local(
model, head_frame, api_key)
Expand Down Expand Up @@ -172,6 +183,7 @@ def async_call(cls,
model: str,
prompt: Any = None,
img_url: str = None,
audio_url: str = None,
# """@deprecated, use prompt_extend in parameters """
extend_prompt: bool = True,
negative_prompt: str = None,
Expand All @@ -194,6 +206,7 @@ def async_call(cls,
negative_prompt (str): The negative prompt is the opposite of the prompt meaning.
template (str): LoRa input, such as gufeng, katong, etc.
img_url (str): The input image url, Generate the URL of the image referenced by the video.
audio_url (str): The input audio url.
api_key (str, optional): The api api_key. Defaults to None.
workspace (str): The dashscope workspace id.
extra_input (Dict): The extra input parameters.
Expand All @@ -215,7 +228,7 @@ def async_call(cls,
task_group, function = _get_task_group_and_task(__name__)

inputs, kwargs, task = cls._get_input(
model, prompt, img_url, extend_prompt, negative_prompt, template, api_key,
model, prompt, img_url, audio_url, extend_prompt, negative_prompt, template, api_key,
extra_input, task, function, head_frame, tail_frame,
first_frame_url, last_frame_url, **kwargs)

Expand Down Expand Up @@ -339,6 +352,7 @@ async def call(cls,
model: str,
prompt: Any = None,
img_url: str = None,
audio_url: str = None,
# """@deprecated, use prompt_extend in parameters """
extend_prompt: bool = True,
negative_prompt: str = None,
Expand All @@ -361,6 +375,7 @@ async def call(cls,
negative_prompt (str): The negative prompt is the opposite of the prompt meaning.
template (str): LoRa input, such as gufeng, katong, etc.
img_url (str): The input image url, Generate the URL of the image referenced by the video.
audio_url (str): The input audio url.
api_key (str, optional): The api api_key. Defaults to None.
workspace (str): The dashscope workspace id.
extra_input (Dict): The extra input parameters.
Expand All @@ -380,7 +395,7 @@ async def call(cls,
"""
task_group, f = _get_task_group_and_task(__name__)
inputs, kwargs, task = VideoSynthesis._get_input(
model, prompt, img_url, extend_prompt, negative_prompt, template, api_key,
model, prompt, img_url, audio_url, extend_prompt, negative_prompt, template, api_key,
extra_input, task, f, head_frame, tail_frame,
first_frame_url, last_frame_url, **kwargs)
response = await super().call(model, inputs, task_group, task, f, api_key, workspace, **kwargs)
Expand All @@ -391,6 +406,7 @@ async def async_call(cls,
model: str,
prompt: Any = None,
img_url: str = None,
audio_url: str = None,
# """@deprecated, use prompt_extend in parameters """
extend_prompt: bool = True,
negative_prompt: str = None,
Expand All @@ -413,6 +429,7 @@ async def async_call(cls,
negative_prompt (str): The negative prompt is the opposite of the prompt meaning.
template (str): LoRa input, such as gufeng, katong, etc.
img_url (str): The input image url, Generate the URL of the image referenced by the video.
audio_url (str): The input audio url.
api_key (str, optional): The api api_key. Defaults to None.
workspace (str): The dashscope workspace id.
extra_input (Dict): The extra input parameters.
Expand All @@ -434,7 +451,7 @@ async def async_call(cls,
task_group, function = _get_task_group_and_task(__name__)

inputs, kwargs, task = VideoSynthesis._get_input(
model, prompt, img_url, extend_prompt, negative_prompt, template, api_key,
model, prompt, img_url, audio_url, extend_prompt, negative_prompt, template, api_key,
extra_input, task, function, head_frame, tail_frame,
first_frame_url, last_frame_url, **kwargs)

Expand Down
25 changes: 25 additions & 0 deletions samples/test_video_synthesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from http import HTTPStatus
from dashscope import VideoSynthesis
import os

prompt = "一幅史诗级可爱的场景。一只小巧可爱的卡通小猫将军,身穿细节精致的金色盔甲,头戴一个稍大的头盔,勇敢地站在悬崖上。他骑着一匹虽小但英勇的战马。悬崖下方,一支由老鼠组成的、数量庞大、无穷无尽的军队正带着临时制作的武器向前冲锋。这是一个戏剧性的、大规模的战斗场景,灵感来自中国古代的战争史诗。远处的雪山上空,天空乌云密布。整体氛围是“可爱”与“霸气”的搞笑和史诗般的融合"
audio_url = 'https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250925/ozwpvi/rap.mp3'
api_key = os.getenv("DASHSCOPE_API_KEY")


def simple_call():
print('----sync call, please wait a moment----')
rsp = VideoSynthesis.call(api_key=api_key,
model="wan2.5-t2v-preview",
prompt=prompt,
audio_url=audio_url)
if rsp.status_code == HTTPStatus.OK:

print('response: %s' % rsp)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and consistency with modern Python practices, consider using an f-string for string formatting.

Suggested change
print('response: %s' % rsp)
print(f'response: {rsp}')

else:
print('sync_call Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
Comment on lines +20 to +21
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and consistency with modern Python practices, consider using an f-string for string formatting. It also allows you to write this on a single line.

        print(f'sync_call Failed, status_code: {rsp.status_code}, code: {rsp.code}, message: {rsp.message}')



if __name__ == '__main__':
simple_call()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's a standard convention to end files with a single newline character. Please add one here.

    simple_call()