File size: 6,189 Bytes
a65e95e
 
 
 
 
5dfc565
a65e95e
 
 
 
5dfc565
a65e95e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dfc565
a65e95e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dfc565
a65e95e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dfc565
a65e95e
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import path from "node:path"

import { v4 as uuidv4 } from "uuid"
import tmpDir from "temp-dir"

import { downloadFileToTmp } from "../utils/downloadFileToTmp.mts"
import { generateAudio } from "./generateAudio.mts"
import { generateVideo } from "./generateVideo.mts"
import { upscaleVideo } from "./upscaleVideo.mts"
import { generateVoice } from "./generateVoice.mts"
import { generateSeed } from "../utils/generateSeed.mts"
import { mergeAudio } from "./mergeAudio.mts"
import { addAudioToVideo } from "./addAudioToVideo.mts"
import { interpolateVideo } from "./interpolateVideo.mts"
import { postInterpolation } from "./postInterpolation.mts"


export const generateShot = async ({
  seed = 0,
  shotId = "",
  actorPrompt = "",
  shotPrompt = "",
  backgroundAudioPrompt = "",
  foregroundAudioPrompt = "",
  actorDialoguePrompt = "",
  actorVoicePrompt = "",
  duration = 2,
  nbFrames = 24,
  resolution = 576,
  nbSteps = 35,
  upscale = true,
  interpolate = true,
  noise = true,
}: {
  seed?: number;
  shotId?: string;
  actorPrompt?: string;
  shotPrompt?: string;
  backgroundAudioPrompt?: string;
  foregroundAudioPrompt?: string;
  actorDialoguePrompt?: string;
  actorVoicePrompt?: string;
  duration?: number; // 2 seconds
  nbFrames?: number; // 24 FPS
  resolution?: number; // 256, 320, 512, 576, 720, 1080..
  nbSteps?: number;
  upscale?: boolean;
  interpolate?: boolean;
  noise?: boolean;
}) => {
  seed = seed || generateSeed()
  shotId = shotId || uuidv4()

  const shotFileName = `${shotId}.mp4`

  console.log("generating video shot:", {
    seed,
    shotId,
    actorPrompt,
    shotPrompt,
    backgroundAudioPrompt,
    foregroundAudioPrompt,
    actorDialoguePrompt,
    actorVoicePrompt,
    duration,
    nbFrames,
    resolution,
    nbSteps,
    upscale,
    interpolate,
    noise,
  })


  if (actorPrompt) {
    console.log("generating actor..")
    const actorIdentityFileName = `actor_${Date.now()}.png`
    // await generateActor(actorPrompt, actorIdentityFileName, seed)
  }

  console.log("generating base video ..")
  let generatedVideoUrl = ""

  // currenty the base model is incapable of generating more than 24 FPS,
  // because otherwise the upscaler will have trouble

  // so for now, we fix it to 24 frames
  // const nbFramesForBaseModel = Math.min(3, Math.max(1, Math.round(duration))) * 8
  const nbFramesForBaseModel = 24

  try {
    generatedVideoUrl = await generateVideo(shotPrompt, {
      seed,
      nbFrames: nbFramesForBaseModel,
      nbSteps
    })
  } catch (err) {
    // upscaling can be finicky, if it fails we try again
    console.log('- trying again to generate base shot..')
    generatedVideoUrl = await generateVideo(shotPrompt, {
      seed,
      nbFrames: nbFramesForBaseModel,
      nbSteps
    })
  }

  console.log("downloading video..")

  const videoFileName = await downloadFileToTmp(generatedVideoUrl, shotFileName)

  if (upscale) {
    console.log("upscaling video..")
    try {
      await upscaleVideo(videoFileName, shotPrompt)
    } catch (err) {
      // upscaling can be finicky, if it fails we try again
      console.log('- trying again to upscale shot..')
      await upscaleVideo(videoFileName, shotPrompt)
    }
  }

  if (interpolate) {
    console.log("upscaling video..")
    // ATTENTION 1:
    // the interpolation step always create a SLOW MOTION video
    // it means it can last a lot longer (eg. 2x, 3x, 4x.. longer)
    // than the duration generated by the original video model

    // ATTENTION 2:
    // the interpolation step generates videos in 910x512!

    // ATTENTION 3:
    // the interpolation step parameters are currently not passed to the space,
    // so changing those two variables below will have no effect!
    const interpolationSteps = 3
    const interpolatedFramesPerSecond = 24
    await interpolateVideo(
      task,
      interpolationSteps,
      interpolatedFramesPerSecond
    )
    console.log('creating slow-mo video (910x512 @ 24 FPS)')

    // with our current interpolation settings, the 3 seconds video generated by the model
    // become a 7 seconds video, at 24 FPS
  
    // so we want to scale it back to the desired duration length
    // also, as a last trick we want to upscale it (without AI) and add some FXs
    console.log('performing final scaling (1280x720 @ 24 FPS)')
    await postInterpolation(videoFileName, duration, nbFrames)
  }
  
  let backgroundAudioFileName = ''
  if (backgroundAudioPrompt) {
    console.log("generating background audio..")
    backgroundAudioFileName = await generateAudio(backgroundAudioPrompt, `shot_${shotId}_audio_${uuidv4}.m4a`)
  }

  let foregroundAudioFileName = ''
  if (foregroundAudioPrompt) {
    console.log("generating foreground audio..")
    foregroundAudioFileName = await generateAudio(foregroundAudioPrompt, `shot_${shotId}_audio_${uuidv4()}.m4a`)
  }


  let voiceAudioFileName = ''
  if (actorDialoguePrompt) {
    console.log("configuring dialogue..")
    if (actorVoicePrompt) {
      console.log("configuring voice..")
      // well.. that's a TODO!
      // for now let's always use the same voice model

      console.log('TODO this should be done in the sequence, not the prompt!')
      voiceAudioFileName = await generateVoice(actorDialoguePrompt, `shot_${shotId}_voice_${uuidv4()}.m4a`)
    }
  }

  console.log('merging audio with video..')
  if (backgroundAudioFileName || foregroundAudioFileName) {
    let audioFileName = ''

    // we have both background and foreground
    if (backgroundAudioFileName && foregroundAudioFileName) {
      audioFileName = await mergeAudio({
        input1FileName: backgroundAudioFileName, 
        input1Volume: 0.2,// 20% volume
        input2FileName: foregroundAudioFileName,
        input2Volume: 0.7, // 70% volume
      })
    } else if (backgroundAudioFileName) {
      audioFileName = backgroundAudioFileName
    } else if (foregroundAudioFileName) {
      audioFileName = foregroundAudioFileName
    }

    await addAudioToVideo(task, audioFileName)
  }

  console.log("returning result to user..")

  const filePath = path.resolve(tmpDir, videoFileName)

  return {
    shotId,
    filePath,
    videoFileName
  }
}