Video Generation With Java: Leverage Nova and Amazon Bedrock
Learn how to generate videos using Amazon Nova on Amazon Bedrock with Java. This step-by-step guide covers AWS setup and Java code for video creation.
Join the DZone community and get the full member experience.
Join For FreeAmazon Nova is a new generation of state-of-the-art (SOTA) foundation models (FMs) that deliver frontier intelligence and industry-leading price performance, released on December 24. It is available exclusively on Amazon Bedrock.
As this model is quite new, there are not a lot of examples of its use, especially in Java. As of this writing, the official AWS docs contain only Python code examples, and there are a couple.
Today, we are going to build a Java application that initiates video generation using a text prompt, using Amazon Bedrock API and Amazon Nova Foundational model. The resulting video will be saved in AWS S3.
Note: Please be aware that executing this application may cost you some money for running AWS Bedrock.
Step 1: Generate AWS Keys and Enable the Foundational Model to Use
If you don’t have an active AWS access key, do the following steps (copied and pasted from this SOF thread):
- Go to: http://aws.amazon.com/.
- Sign up and create a new account (they'll give you the option for a 1-year trial or similar).
- Go to your AWS account overview.
- The account menu is in the upper right (it has your name on it).
- sub-menu: Security Credentials.
After you have your keys generated, you should choose and enable the foundational model in Bedrock. Go to Amazon Bedrock, and from the Model Access menu on the left, configure access to the Nova Reel model.
Please be aware that, as of the writing of this article, Amazon Nova models are available in Amazon Bedrock only in the US East (N. Virginia) AWS Region.
Step 2: Install and Configure AWS CLI
AWS Java SDK uses the AWS CLI configuration to grab user credentials (the access key we configured in step 1). To avoid providing keys directly in code, it is a better (and safer) approach to configure AWS CLI first and then start writing code.
If you don’t have AWS CLI configured, complete the following steps:
- Download and install the AWS CLI.
- Configure the CLI with AWS configure. During the configuration process, paste the key information you received during step one. As a default region, I suggest setting us-east-1, as the Amazon Nova Reel model is only available in this region.
Step 3: Create an S3 Bucket
Our video will not be created automatically; rather, the API call will just trigger a new execution. The result will be generated in a couple of minutes and stored in the S3 bucket.
As part of the configuration, we will have to provide an S3 bucket where this video should be stored. When video generation is complete, the video is stored in the Amazon S3 bucket we specified in this step. Amazon Nova creates a folder for each invocation ID. This folder contains the manifest.json and output.mp4 files created by the video generation request.
Creating an S3 bucket is quite a straightforward process, but if you need help, please find additional instructions here. Remember the bucket name you create, as we will need it in the next steps.
Because Amazon Bedrock writes a file to an Amazon S3 bucket on your behalf, the AWS role that you use needs permissions configured to allow the appropriate Amazon Bedrock and Amazon S3 actions and the s3:PutObject
action. The minimum action permissions required to generate a video are:
bedrock:InvokeModel
s3:PutObject
However, AWS recommends the following additional actions so you can track the status of video generation jobs:
bedrock:GetAsyncInvoke
bedrock:ListAsyncInvokes
Step 4. Setting Up Dependencies
As Amazon Nova is a quite new foundational model and its introduction caused some extension of existing interfaces provided by AWS SDK, we may not be able to use old versions of Bedrock SDK. As of writing, the latest version of the AWS Bedrock runtime is 2.30.35
and this is the version I’m going to use.
Make sure you are adding this dependency in your pom.xml
if you are using Maven. For Gradle, the syntax will be different, but ensure you are using the same or a later version of the Bedrock dependency.
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bedrockruntime</artifactId>
<version>2.30.35</version>
</dependency>
</dependencies>
Step 5: Writing Code
Below, you can find the application code. As the main purpose of this article is to explore the way to integrate Java with the AWS Bedrock/Nova Reel model, we will create a simple static main method that will contain all the necessary code.
Of course, if you are building a production-ready application you should pay more attention to OOP concepts and SOLID principles.
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.document.Document;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.*;
import software.amazon.awssdk.services.bedrockruntime.model.*;
import java.util.concurrent.CompletableFuture;
public class NovaReelDemo {
public static void main(String[] args) {
BedrockRuntimeAsyncClient bedrockClient = BedrockRuntimeAsyncClient.builder()
.region(Region.US_EAST_1)
.credentialsProvider(ProfileCredentialsProvider.create())
.build();
String s3Bucket = "s3://{your_bucket_name}";
String prompt = "Fluffy cat relaxing on the beach next to the ocean under the blue sky. There should be an airplane flying in the sky";
//Create request as an instance of Document class
Document novaRequest = prepareDocument(prompt);
// Create request
StartAsyncInvokeRequest request = StartAsyncInvokeRequest.builder()
.modelId("amazon.nova-reel-v1:0")
.modelInput(novaRequest)
.outputDataConfig(AsyncInvokeOutputDataConfig.builder()
.s3OutputDataConfig(AsyncInvokeS3OutputDataConfig.builder().s3Uri(s3Bucket).build())
.build())
.build();
try {
CompletableFuture<StartAsyncInvokeResponse> startAsyncInvokeResponseCompletableFuture = bedrockClient.startAsyncInvoke(request);
StartAsyncInvokeResponse startAsyncInvokeResponse = startAsyncInvokeResponseCompletableFuture.get();
System.out.println("invocation ARN: " +startAsyncInvokeResponse.invocationArn());
} catch (Exception e) {
System.out.println(e);
} finally {
bedrockClient.close();
}
}
private static Document prepareDocument(String prompt) {
Document textToVideoParams = Document.mapBuilder()
.putString("text", prompt)
.build();
Document videoGenerationConfig = Document.mapBuilder()
.putNumber("durationSeconds", 6)
.putNumber("fps", 24)
.putString("dimension", "1280x720")
.build();
Document novaRequest = Document.mapBuilder()
.putString("taskType", "TEXT_VIDEO")
.putDocument("textToVideoParams", textToVideoParams)
.putDocument("videoGenerationConfig", videoGenerationConfig)
.build();
return novaRequest;
}
}
Let’s go block by block:
- We are creating
BedrockRuntimeAsyncClient
, providingus-east-1
as a region because it is the only region where Amazon Nova models are available at the moment. Also, usingcredentialsProvider(ProfileCredentialsProvider.create())
, we are setting up authentication using the AWS CLI config we set up in step 1. String s3Bucket = "s3://{your_bucket_name}"
– Put the bucket name you created in step 3. This is the s3 bucket where our generated video will be stored.String prompt
is the actual prompt that will be used for video generation.Document novaRequest = prepareDocument(prompt)
– To pass all the necessary parameters, we need to create an instance of the document class. Below you can find JSON, which might be used if we try to execute the same command using AWS CLI. When using Java, we need to wrap it in the instance of a document.
{
"taskType": "TEXT_VIDEO",
"textToVideoParams": {
"text": "Fluffy cat relaxing on the beach next to the ocean under the blue sky. There should be an airplane flying in the sky"
},
"videoGenerationConfig": {
"durationSeconds": 6,
"fps": 24,
"dimension": "1280x720"
}
}
Let’s deep dive into the configs we provided:
taskType TEXT_VIDEO
– Should be constant for us as we are planning only to create a video from prompts.text
(required) – A text prompt to generate the video; must be 1-512 characters in length.durationSeconds
(required) – Duration of the output video. 6 is the only supported value at the moment of writing this article.fps
(required) – Frame rate of the output video. 24 is the only supported value at the moment of writing this article.dimension
(required) – Width and height of the output video. "1280x720" is the only supported value currently.
You may find more information on video generation input parameters in official AWS docs.
StartAsyncInvokeRequest
– Request object, which includes all the info we previously configured, including request and S3 bucket where the result video will be created and stored. Also, this request includesmodelId
. At the moment,amazon.nova-reel-v1:0
is the only available model for generating video in Amazon Bedrock.- Request invocation using
bedrockClient.startAsyncInvoke(request)
returns an instance ofCompletableFuture
. When theCompletableFuture
is completed, we can fetch the Invocation ARN to track the status of the video generation job.
Step 6: Run Our Application
To run our application, we need to simply execute its main method. This is a standard Java application with a public static void main(String[] args)
entry point, meaning it can be launched directly from the command line or an IDE. No additional setup is required — just compile and run it as a typical Java program.
In case of successful execution, you should see the invocation ARN printed in the console. You may track the status of the invocation leveraging the getAsyncInvoke command.
As soon as you trigger a new invocation, you may check the S3 bucket we created in step 3. As previously mentioned, a new folder should have been created for the invocation. If you dive into this folder, you will see a manifest.json file.
It usually takes up to 5 to 6 minutes for the video to be generated. Try to refresh the page after this period of time, and you will be able to see the result. Congratulations!
Conclusion
In this article, we explored how to integrate Java with Amazon Bedrock and the Amazon Nova Reel model to generate videos from text prompts. We walked through the necessary steps, including setting up AWS credentials, configuring the Bedrock API, creating an S3 bucket for storing results, and writing the Java code to invoke the model.
Since Amazon Nova is a relatively new foundational model, Java-specific examples are scarce, making this guide a valuable resource for developers looking to work with AWS Bedrock in Java. While this was a simple implementation, it laid the groundwork for more advanced use cases, such as integrating video generation into a larger application or optimizing the process for production-ready environments.
As AWS continues to expand its AI capabilities, we can expect further enhancements and new features for Amazon Nova. Stay tuned for updates, and happy coding!
Opinions expressed by DZone contributors are their own.
Comments