|
| 1 | +import { type XaiVideoModelOptions, xai } from '@ai-sdk/xai'; |
| 2 | +import { experimental_generateVideo as generateVideo } from 'ai'; |
| 3 | +import { presentVideos } from '../../lib/present-video'; |
| 4 | +import { run } from '../../lib/run'; |
| 5 | +import { withSpinner } from '../../lib/spinner'; |
| 6 | + |
| 7 | +// Video extension: continue an existing video from its last frame. |
| 8 | +// The `duration` controls the length of the *extension* only, not the total. |
| 9 | +// `aspectRatio` and `resolution` are not supported in extension mode — the |
| 10 | +// output inherits those from the input video. |
| 11 | +run(async () => { |
| 12 | + // Step 1: Generate a short source video. |
| 13 | + const source = await withSpinner('Step 1: Generating source video...', () => |
| 14 | + generateVideo({ |
| 15 | + model: xai.video('grok-imagine-video'), |
| 16 | + prompt: 'A cat sitting on a sunlit windowsill, tail gently swishing.', |
| 17 | + duration: 5, |
| 18 | + aspectRatio: '16:9', |
| 19 | + providerOptions: { |
| 20 | + xai: { pollTimeoutMs: 600000 } satisfies XaiVideoModelOptions, |
| 21 | + }, |
| 22 | + }), |
| 23 | + ); |
| 24 | + |
| 25 | + const sourceUrl = source.providerMetadata?.xai?.videoUrl as |
| 26 | + | string |
| 27 | + | undefined; |
| 28 | + if (sourceUrl == null) { |
| 29 | + throw new Error('xAI provider metadata did not include a source videoUrl.'); |
| 30 | + } |
| 31 | + |
| 32 | + console.log('Source video URL:', sourceUrl); |
| 33 | + await presentVideos(source.videos); |
| 34 | + |
| 35 | + // Step 2: Extend the video with a new scene. |
| 36 | + const extended = await withSpinner( |
| 37 | + 'Step 2: Extending video with a new scene...', |
| 38 | + () => |
| 39 | + generateVideo({ |
| 40 | + model: xai.video('grok-imagine-video'), |
| 41 | + prompt: |
| 42 | + 'The cat slowly turns its head, notices a butterfly, and leaps off the windowsill.', |
| 43 | + duration: 6, |
| 44 | + providerOptions: { |
| 45 | + xai: { |
| 46 | + mode: 'extend-video', |
| 47 | + videoUrl: sourceUrl, |
| 48 | + pollTimeoutMs: 600000, |
| 49 | + } satisfies XaiVideoModelOptions, |
| 50 | + }, |
| 51 | + }), |
| 52 | + ); |
| 53 | + |
| 54 | + console.log('\nExtended video (original 5s + 6s extension = 11s total):'); |
| 55 | + console.log( |
| 56 | + 'Provider metadata:', |
| 57 | + JSON.stringify(extended.providerMetadata, null, 2), |
| 58 | + ); |
| 59 | + await presentVideos(extended.videos); |
| 60 | +}); |
0 commit comments