Skip to content

Commit db91b46

Browse files
author
Guiners
committed
adding videogen samples and tests
1 parent 62dbb27 commit db91b46

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const {Storage} = require('@google-cloud/storage');
20+
21+
const location = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
const projectId = process.env.CAIP_PROJECT_ID;
23+
const sample = require('../video-generation/videogen-with-img.js');
24+
const {delay} = require('./util');
25+
26+
const storage = new Storage();
27+
28+
const GCS_OUTPUT_BUCKET = 'nodejs-docs-samples-tests';
29+
30+
async function gcs_output_uri() {
31+
const dt = new Date();
32+
const prefix = `text_output/${dt.toISOString()}`;
33+
const fullUri = `gs://${GCS_OUTPUT_BUCKET}/${prefix}`;
34+
35+
return {
36+
uri: fullUri,
37+
async cleanup() {
38+
const [files] = await storage.bucket(GCS_OUTPUT_BUCKET).getFiles({
39+
prefix,
40+
});
41+
for (const file of files) {
42+
await file.delete();
43+
}
44+
},
45+
};
46+
}
47+
48+
describe('videogen-with-img', async () => {
49+
it('should generate video content from an image', async function () {
50+
this.timeout(180000);
51+
this.retries(4);
52+
await delay(this.test);
53+
const gscOutput = gcs_output_uri();
54+
const gscUri = (await gscOutput).uri;
55+
const output = await sample.generateVideo(gscUri, projectId, location);
56+
console.log('output', output);
57+
assert(output);
58+
});
59+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const {Storage} = require('@google-cloud/storage');
20+
21+
const location = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
const projectId = process.env.CAIP_PROJECT_ID;
23+
const sample = require('../video-generation/videogen-with-txt.js');
24+
const {delay} = require('./util');
25+
26+
const storage = new Storage();
27+
28+
const GCS_OUTPUT_BUCKET = 'nodejs-docs-samples-tests';
29+
30+
async function gcs_output_uri() {
31+
const dt = new Date();
32+
const prefix = `text_output/${dt.toISOString()}`;
33+
const fullUri = `gs://${GCS_OUTPUT_BUCKET}/${prefix}`;
34+
35+
return {
36+
uri: fullUri,
37+
async cleanup() {
38+
const [files] = await storage.bucket(GCS_OUTPUT_BUCKET).getFiles({
39+
prefix,
40+
});
41+
for (const file of files) {
42+
await file.delete();
43+
}
44+
},
45+
};
46+
}
47+
48+
describe('videogen-with-txt', async () => {
49+
it('should generate video content from a text prompt', async function () {
50+
this.timeout(180000);
51+
this.retries(4);
52+
await delay(this.test);
53+
const gscOutput = gcs_output_uri();
54+
const gscUri = (await gscOutput).uri;
55+
const output = await sample.generateVideo(gscUri, projectId, location);
56+
console.log('output', output);
57+
assert(output);
58+
});
59+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_videogen_with_img]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateVideo(
24+
outputGcsUri,
25+
projectId = GOOGLE_CLOUD_PROJECT,
26+
location = GOOGLE_CLOUD_LOCATION
27+
) {
28+
const client = new GoogleGenAI({
29+
vertexai: true,
30+
project: projectId,
31+
location: location,
32+
});
33+
34+
let operation = await client.models.generateVideos({
35+
model: 'veo-3.0-generate-preview',
36+
prompt:
37+
'Extreme close-up of a cluster of vibrant wildflowers swaying gently in a sun-drenched meadow',
38+
image: {
39+
gcsUri: 'gs://cloud-samples-data/generative-ai/image/flowers.png',
40+
mimeType: 'image/png',
41+
},
42+
config: {
43+
aspectRatio: '16:9',
44+
outputGcsUri: outputGcsUri,
45+
},
46+
});
47+
48+
while (!operation.done) {
49+
await new Promise(resolve => setTimeout(resolve, 15000));
50+
operation = await client.operations.get({operation: operation});
51+
console.log(operation);
52+
}
53+
54+
if (operation.response) {
55+
console.log(operation.response.generatedVideos[0].video.uri);
56+
}
57+
return operation;
58+
}
59+
60+
//todo
61+
// output GenerateVideosOperation {
62+
// name: 'projects/cloud-ai-devrel-softserve/locations/us-central1/publishers/google/models/veo-3.0-generate-preview/operations/adf19783-9f21-49c4-a4af-3265c2df4326',
63+
// metadata: undefined,
64+
// done: true,
65+
// error: {
66+
// code: 7,
67+
// message: "[email protected] does not have storage.objects.create access to the Google Cloud Storage object. Permission 'storage.objects.create' denied on resource (or it may not exist). [email protected] does not have storage.objects.create access to the Google Cloud Storage object. Permission 'storage.objects.create' denied on resource (or it may not exist)."
68+
// }
69+
// }
70+
71+
// [END googlegenaisdk_videogen_with_img]
72+
73+
module.exports = {
74+
generateVideo,
75+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_videogen_with_txt]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateVideo(
24+
outputGcsUri,
25+
projectId = GOOGLE_CLOUD_PROJECT,
26+
location = GOOGLE_CLOUD_LOCATION
27+
) {
28+
const client = new GoogleGenAI({
29+
vertexai: true,
30+
project: projectId,
31+
location: location,
32+
});
33+
34+
let operation = await client.models.generateVideos({
35+
model: 'veo-3.0-generate-001',
36+
prompt: 'a cat reading a book',
37+
config: {
38+
aspectRatio: '16:9',
39+
outputGcsUri: outputGcsUri,
40+
},
41+
});
42+
43+
while (!operation.done) {
44+
await new Promise(resolve => setTimeout(resolve, 15000));
45+
operation = await client.operations.get({operation: operation});
46+
console.log(operation);
47+
}
48+
49+
if (operation.response) {
50+
console.log(operation.response.generatedVideos[0].video.uri);
51+
}
52+
return operation;
53+
}
54+
55+
//todo
56+
// output GenerateVideosOperation {
57+
// name: 'projects/cloud-ai-devrel-softserve/locations/us-central1/publishers/google/models/veo-3.0-generate-001/operations/33bcfb22-c402-4bae-80d8-e2f5b53e3be6',
58+
// metadata: undefined,
59+
// done: true,
60+
// error: {
61+
// code: 7,
62+
// message: "[email protected] does not have storage.objects.create access to the Google Cloud Storage object. Permission 'storage.objects.create' denied on resource (or it may not exist). [email protected] does not have storage.objects.create access to the Google Cloud Storage object. Permission 'storage.objects.create' denied on resource (or it may not exist)."
63+
// }
64+
// }
65+
66+
// [END googlegenaisdk_videogen_with_txt]
67+
68+
module.exports = {
69+
generateVideo,
70+
};

0 commit comments

Comments
 (0)