Images and Videos¶
client.images() (wanx series) and client.videos() (wan series) are async-task APIs:
a generation request submits a task, and the result is polled by task id. Both resources offer
a one-call generate (submit + wait) and the manual jobs() flow.
Billing
Generation tasks incur charges. The snippets below are adapted from the runnable ImagesGenerate and VideosGenerate examples.
Full runnable example: ImagesGenerate.java (auto-synced)
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.examples.atomic.images;
import com.alibaba.dashscope.DashScopeClient;
import com.alibaba.dashscope.RequestOptions;
import com.alibaba.dashscope.examples.ExampleSupport;
import com.alibaba.dashscope.resources.jobs.Job;
import com.alibaba.dashscope.types.common.Page;
import com.alibaba.dashscope.types.images.ImageGenerateParams;
/**
* Image generation (wanx series): one-call generate (submit + wait), then the manual async-task
* submit/wait/fetch flow, then list tasks. Translation of the Python blueprint example
* 01_atomic_capabilities/image_video_synthesis/images.py.
*/
public final class ImagesGenerate {
public static void main(String[] args) throws Exception {
String model = System.getenv().getOrDefault("DASHSCOPE_MODEL", "wanx2.1-t2i-turbo");
String prompt = "一间有着精致窗户的花店,漂亮的木质门,摆放着花朵";
try (DashScopeClient client = ExampleSupport.client()) {
// One-call generate: submits the task and waits for the result
System.out.println("----- generate (submit + wait) -----");
Job job =
client
.images()
.generate(
ImageGenerateParams.builder()
.model(model)
.prompt(prompt)
.n(1)
.size("1024*1024")
.build());
if (job.isSucceeded()) {
for (String url : job.getImageUrls()) {
System.out.println("image url: " + url);
}
} else {
System.out.println("job failed: " + job.getOutput());
}
// Manual async-task flow: submit first, then wait/fetch by task_id
System.out.println("----- jobs.create + wait + fetch -----");
Job pending =
client
.images()
.jobs()
.create(
ImageGenerateParams.builder()
.model(model)
.prompt(prompt)
.n(1)
.size("1024*1024")
.build());
if (pending.getOutput() == null) {
throw new IllegalStateException("pending.getOutput() is null");
}
String taskId = pending.getTaskId();
System.out.println("submitted task_id: " + taskId);
Job finished = client.images().jobs().wait(taskId, RequestOptions.NONE);
System.out.println("wait finished, succeeded: " + finished.isSucceeded());
Job snapshot = client.images().jobs().fetch(taskId, RequestOptions.NONE);
System.out.println("fetch output: " + snapshot.getOutput());
// List historical tasks
System.out.println("----- jobs.list -----");
Page<Job> result = client.images().jobs().list(null, RequestOptions.NONE);
System.out.println("total tasks: " + result.getTotalCount());
result.getItems().stream()
.limit(3)
.forEach(
task ->
System.out.println(
"task_id=" + task.getTaskId() + ", status=" + task.getTaskStatus()));
}
}
}
Full runnable example: VideosGenerate.java (auto-synced)
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.examples.atomic.videos;
import com.alibaba.dashscope.DashScopeClient;
import com.alibaba.dashscope.RequestOptions;
import com.alibaba.dashscope.examples.ExampleSupport;
import com.alibaba.dashscope.resources.jobs.Job;
import com.alibaba.dashscope.types.common.Page;
import com.alibaba.dashscope.types.videos.VideoGenerateParams;
/**
* Video generation (wan series): one-call generate (submit + wait), then the manual async-task
* submit/wait/fetch flow, then list tasks. Translation of the Python blueprint example
* 01_atomic_capabilities/image_video_synthesis/videos.py.
*
* <p>Note: video-generation tasks take a while; please be patient.
*/
public final class VideosGenerate {
public static void main(String[] args) throws Exception {
String model = System.getenv().getOrDefault("DASHSCOPE_MODEL", "wan2.7-t2v");
String prompt = "一只小猫在月光下奔跑";
try (DashScopeClient client = ExampleSupport.client()) {
// One-call generate: submits the task and waits for the result
System.out.println("----- generate (submit + wait) -----");
Job job =
client
.videos()
.generate(VideoGenerateParams.builder().model(model).prompt(prompt).build());
if (job.isSucceeded()) {
System.out.println("video url: " + job.getVideoUrl());
} else {
System.out.println("job failed: " + job.getOutput());
}
// Manual async-task flow: submit first, then wait/fetch by task_id
System.out.println("----- jobs.create + wait + fetch -----");
Job pending =
client
.videos()
.jobs()
.create(VideoGenerateParams.builder().model(model).prompt(prompt).build());
if (pending.getOutput() == null) {
throw new IllegalStateException("pending.getOutput() is null");
}
String taskId = pending.getTaskId();
System.out.println("submitted task_id: " + taskId);
Job finished = client.videos().jobs().wait(taskId, RequestOptions.NONE);
System.out.println("wait finished, succeeded: " + finished.isSucceeded());
System.out.println("video url: " + finished.getVideoUrl());
Job snapshot = client.videos().jobs().fetch(taskId, RequestOptions.NONE);
System.out.println("fetch output: " + snapshot.getOutput());
// List historical tasks
System.out.println("----- jobs.list -----");
Page<Job> result = client.videos().jobs().list(null, RequestOptions.NONE);
System.out.println("total tasks: " + result.getTotalCount());
result.getItems().stream()
.limit(3)
.forEach(
task ->
System.out.println(
"task_id=" + task.getTaskId() + ", status=" + task.getTaskStatus()));
}
}
}
Image generation¶
import com.alibaba.dashscope.resources.jobs.Job;
import com.alibaba.dashscope.types.images.ImageGenerateParams;
// One-call generate: submits the task and waits for the result.
Job job = client.images().generate(
ImageGenerateParams.builder()
.model("wanx2.1-t2i-turbo")
.prompt("A flower shop with elegant windows and a wooden door")
.n(1)
.size("1024*1024")
.build());
if (job.isSucceeded()) {
for (String url : job.getImageUrls()) {
System.out.println("image url: " + url);
}
} else {
System.out.println("job failed: " + job.getOutput());
}
Manual async-task flow¶
Submit first, then wait or fetch by task_id — useful when you want to persist the task id and
poll from another process:
import com.alibaba.dashscope.RequestOptions;
import com.alibaba.dashscope.types.common.Page;
Job pending = client.images().jobs().create(params);
String taskId = pending.getTaskId();
Job finished = client.images().jobs().wait(taskId, RequestOptions.NONE); // blocks until done
Job snapshot = client.images().jobs().fetch(taskId, RequestOptions.NONE); // one status poll
Page<Job> history = client.images().jobs().list(null, RequestOptions.NONE); // historical tasks
Video generation¶
Same shape, different result accessor. Video tasks take a while — be patient with wait:
import com.alibaba.dashscope.types.videos.VideoGenerateParams;
Job job = client.videos().generate(
VideoGenerateParams.builder()
.model("wan2.7-t2v")
.prompt("A kitten running under the moonlight")
.build());
if (job.isSucceeded()) {
System.out.println("video url: " + job.getVideoUrl());
}
client.videos().jobs().create / wait / fetch / list mirror the image task flow exactly.
Job semantics¶
Job.isSucceeded()/isFinished()/getTaskStatus()report task state; on failuregetOutput()carries the server-side detail.generate=jobs().create+jobs().waitin one call.- The 2.x
ImageSynthesis/VideoSynthesisentry points map here — see Migrating from 2.x.