File size: 12,214 Bytes
b2bd451
 
ccd48b8
e4e0e54
5dfc565
 
 
 
b2bd451
 
5dfc565
e4e0e54
 
5dfc565
e4e0e54
 
ccd48b8
e4e0e54
 
5dfc565
e4e0e54
 
585f76c
e4e0e54
585f76c
e4e0e54
 
 
 
 
585f76c
 
 
 
 
e4e0e54
ccd48b8
 
 
 
 
 
 
 
e4e0e54
 
ccd48b8
e4e0e54
 
 
 
 
ccd48b8
e4e0e54
585f76c
 
5dfc565
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e0e54
5dfc565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719147a
5dfc565
 
 
585f76c
 
e4e0e54
ccd48b8
 
 
e4e0e54
5dfc565
 
 
e4e0e54
 
ccd48b8
 
 
5dfc565
 
 
 
ccd48b8
 
 
 
 
 
 
5dfc565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
585f76c
 
e4e0e54
5dfc565
ccd48b8
57adb5c
e4e0e54
5dfc565
 
 
e4e0e54
 
 
5dfc565
 
 
 
 
 
 
 
 
 
 
 
585f76c
 
e4e0e54
ccd48b8
 
 
e4e0e54
719147a
5dfc565
 
 
e4e0e54
 
 
5dfc565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccd48b8
 
 
5dfc565
 
 
 
 
 
 
 
 
585f76c
 
e4e0e54
5dfc565
9571f2e
ccd48b8
5dfc565
ccd48b8
 
5dfc565
ccd48b8
 
 
5dfc565
ccd48b8
 
 
 
 
 
 
 
9571f2e
ccd48b8
 
 
 
 
 
5dfc565
ccd48b8
5dfc565
ccd48b8
e4e0e54
5dfc565
 
 
 
25b858c
e4e0e54
25b858c
 
 
 
 
 
 
 
 
 
 
 
e4e0e54
25b858c
 
 
ccd48b8
25b858c
e4e0e54
25b858c
 
 
 
e4e0e54
 
25b858c
 
 
 
 
 
 
e4e0e54
 
25b858c
 
 
5dfc565
 
585f76c
 
e4e0e54
5dfc565
e4e0e54
5dfc565
 
 
e4e0e54
 
5dfc565
e4e0e54
5dfc565
ccd48b8
e4e0e54
ccd48b8
e4e0e54
 
ccd48b8
 
 
 
 
 
 
 
e4e0e54
5dfc565
e4e0e54
5dfc565
7bea24d
e4e0e54
5dfc565
e4e0e54
 
 
 
 
 
5dfc565
e4e0e54
5dfc565
e4e0e54
5dfc565
e4e0e54
 
 
 
 
 
 
5dfc565
 
ccd48b8
5dfc565
585f76c
e4e0e54
 
5dfc565
 
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { v4 as uuidv4 } from "uuid"

import { Video, VideoShot } from "../types.mts"

import { generateVideo } from "../production/generateVideo.mts"
import { upscaleVideo } from "../production/upscaleVideo.mts"
import { interpolateVideo } from "../production/interpolateVideo.mts"
import { postInterpolation } from "../production/postInterpolation.mts"
import { generateAudio } from "../production/generateAudio.mts"
import { addAudioToVideo } from "../production/addAudioToVideo.mts"

import { downloadFileToTmp } from "../utils/downloadFileToTmp.mts"
import { copyVideoFromTmpToPending } from "../utils/copyVideoFromTmpToPending.mts"

import { saveAndCheckIfNeedToStop } from "./saveAndCheckIfNeedToStop.mts"
import { enrichVideoSpecsUsingLLM } from "../llm/enrichVideoSpecsUsingLLM.mts"
import { updateShotPreview } from "./updateShotPreview.mts"

export const processVideo = async (video: Video) => {

  // just a an additional precaution, for consistency and robustness
  if (["pause", "completed", "abort", "delete"].includes(video.status)) { return }

  console.log(`processing video video ${video.id}`)

  // always count 2 more steps: 1 for the LLM, 1 for the final assembly

  let nbTotalSteps = 2

  for (const shot of video.shots) {
    nbTotalSteps += shot.nbTotalSteps
  }

  let nbCompletedSteps = 0

  if (!video.hasGeneratedSpecs) {
    try {
      await enrichVideoSpecsUsingLLM(video)
    } catch (err) {
      console.error(`LLM error: ${err}`)
      video.error = `LLM error: ${err}`
      video.status = "delete"
      if (await saveAndCheckIfNeedToStop(video)) { return }
    }

    nbCompletedSteps++
    video.hasGeneratedSpecs = true
    video.progressPercent = Math.round((nbCompletedSteps / nbTotalSteps) * 100)

    if (await saveAndCheckIfNeedToStop(video)) { return }
  }


  for (const shot of video.shots) {
    nbCompletedSteps += shot.nbCompletedSteps

    // skip shots completed previously
    if (shot.completed) {
      continue
    }

    console.log(`need to complete shot ${shot.id}`)

    // currenty we cannot generate too many frames at once,
    // 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


    if (!shot.hasGeneratedPreview) {
      console.log("generating a preview of the final result..")
      let generatedPreviewVideoUrl = ""
      try {
        generatedPreviewVideoUrl = await generateVideo(shot.shotPrompt, {
          seed: shot.seed,
          nbFrames: nbFramesForBaseModel,
          nbSteps: 10, // for the preview, we only give a rough approximation
        })

        console.log("downloading preview video..")

        // download to /tmp
        await downloadFileToTmp(generatedPreviewVideoUrl, shot.fileName)

        await copyVideoFromTmpToPending(shot.fileName)

        shot.hasGeneratedPreview = true
        shot.nbCompletedSteps++
        nbCompletedSteps++
        shot.progressPercent = Math.round((shot.nbCompletedSteps / shot.nbTotalSteps) * 100)
        video.progressPercent = Math.round((nbCompletedSteps / nbTotalSteps) * 100)
       
        await updateShotPreview(video, shot)
    
        if (await saveAndCheckIfNeedToStop(video)) { return }
      } catch (err) {
        console.error(`failed to generate preview for shot ${shot.id} (${err})`)
        // something is wrong, let's put the whole thing back into the queue
        video.error = `failed to generate preview for shot ${shot.id} (will try again later)`
        if (await saveAndCheckIfNeedToStop(video)) { return }

        // always try to yield whenever possible
        return
      }

    }

    const notAllShotsHavePreview = video.shots.some(s => !s.hasGeneratedPreview)

    if (notAllShotsHavePreview) {
      console.log(`step 2 isn't unlocked yet, because not all videos have generated preview`)
      continue
    }

    if (!shot.hasGeneratedVideo) {
      console.log("generating primordial pixel soup (raw video)..")
      let generatedVideoUrl = ""


      const nbFramesForBaseModel = 24

      try {
        generatedVideoUrl = await generateVideo(shot.shotPrompt, {
          seed: shot.seed,
          nbFrames: nbFramesForBaseModel,
          nbSteps: shot.steps,
        })

        console.log("downloading video..")

        await downloadFileToTmp(generatedVideoUrl, shot.fileName)

        await copyVideoFromTmpToPending(shot.fileName)

        shot.hasGeneratedVideo = true
        shot.nbCompletedSteps++
        nbCompletedSteps++
        shot.progressPercent = Math.round((shot.nbCompletedSteps / shot.nbTotalSteps) * 100)
        video.progressPercent = Math.round((nbCompletedSteps / nbTotalSteps) * 100)

        await updateShotPreview(video, shot)

        if (await saveAndCheckIfNeedToStop(video)) { return }
      } catch (err) {
        console.error(`failed to generate shot ${shot.id} (${err})`)
        // something is wrong, let's put the whole thing back into the queue
        video.error = `failed to generate shot ${shot.id} (will try again later)`
        if (await saveAndCheckIfNeedToStop(video)) { return }

        break
      }

    }

    if (!shot.hasUpscaledVideo) {
      console.log("upscaling video..")
      try {
        await upscaleVideo(shot.fileName, shot.shotPrompt)

        shot.hasUpscaledVideo = true
        shot.nbCompletedSteps++
        nbCompletedSteps++
        shot.progressPercent = Math.round((shot.nbCompletedSteps / shot.nbTotalSteps) * 100)
        video.progressPercent = Math.round((nbCompletedSteps / nbTotalSteps) * 100)
        
        await updateShotPreview(video, shot)
    
        if (await saveAndCheckIfNeedToStop(video)) { return }

      } catch (err) {
        console.error(`failed to upscale shot ${shot.id} (${err})`)
        // something is wrong, let's put the whole thing back into the queue
        video.error = `failed to upscale shot ${shot.id} (will try again later)`
        if (await saveAndCheckIfNeedToStop(video)) { return }

        break
      }
    }

    if (!shot.hasInterpolatedVideo) {
      console.log("interpolating 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 = 2
      const interpolatedFramesPerSecond = 30
      console.log('creating slow-mo video (910x512 @ 30 FPS)')
      try {
        await interpolateVideo(
          shot.fileName,
          interpolationSteps,
          interpolatedFramesPerSecond
        )

        shot.hasInterpolatedVideo = true
        shot.nbCompletedSteps++
        nbCompletedSteps++
        shot.progressPercent = Math.round((shot.nbCompletedSteps / shot.nbTotalSteps) * 100)
        video.progressPercent = Math.round((nbCompletedSteps / nbTotalSteps) * 100)


        // note: showing the intermediary result isn't very interesting here 

        // with our current interpolation settings, the 3 seconds video generated by the model
        // become a 7 seconds video, at 30 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 @ 30 FPS)')

        try {
          await postInterpolation(shot.fileName, shot.durationMs, shot.fps, shot.noiseAmount)
      
          shot.hasPostProcessedVideo = true
          shot.nbCompletedSteps++
          nbCompletedSteps++
          shot.progressPercent = Math.round((shot.nbCompletedSteps / shot.nbTotalSteps) * 100)
          video.progressPercent = Math.round((nbCompletedSteps / nbTotalSteps) * 100)

          await updateShotPreview(video, shot)
      
          if (await saveAndCheckIfNeedToStop(video)) { return }
        } catch (err) {
          throw err
        }
      } catch (err) {
        console.error(`failed to interpolate and post-process shot ${shot.id} (${err})`)
        // something is wrong, let's put the whole thing back into the queue
        video.error = `failed to interpolate and shot ${shot.id} (will try again later)`
        if (await saveAndCheckIfNeedToStop(video)) { return }
        break
      }
    }


    let foregroundAudioFileName = `${video.ownerId}_${video.id}_${shot.id}_${uuidv4()}.m4a`

    if (!shot.hasGeneratedForegroundAudio) {
      if (shot.foregroundAudioPrompt) {
        console.log("generating foreground audio..")
    
        try {
          await generateAudio(shot.foregroundAudioPrompt, foregroundAudioFileName)

          shot.hasGeneratedForegroundAudio = true
          shot.nbCompletedSteps++
          nbCompletedSteps++
          shot.progressPercent = Math.round((shot.nbCompletedSteps / shot.nbTotalSteps) * 100)
          video.progressPercent = Math.round((nbCompletedSteps / nbTotalSteps) * 100)

          await addAudioToVideo(shot.fileName, foregroundAudioFileName)

          await updateShotPreview(video, shot)

          if (await saveAndCheckIfNeedToStop(video)) { return }

        } catch (err) {
          console.error(`failed to generate foreground audio for ${shot.id} (${err})`)
          // something is wrong, let's put the whole thing back into the queue
          video.error = `failed to generate foreground audio ${shot.id} (will try again later)`
          if (await saveAndCheckIfNeedToStop(video)) { return }
          break
        }
      } else {
        shot.hasGeneratedForegroundAudio = true
        shot.nbCompletedSteps++
        nbCompletedSteps++
        shot.progressPercent = Math.round((shot.nbCompletedSteps / shot.nbTotalSteps) * 100)
        video.progressPercent = Math.round((nbCompletedSteps / nbTotalSteps) * 100)
        if (await saveAndCheckIfNeedToStop(video)) { return }
      }
    }

    shot.completed = true
    shot.completedAt = new Date().toISOString()
    shot.progressPercent = 100

    video.nbCompletedShots++

    if (await saveAndCheckIfNeedToStop(video)) { return }
  }

  console.log(`end of the loop:`)
  console.log(`nb completed shots: ${video.nbCompletedShots}`)
  console.log(`len of the shot array: ${video.shots.length}`)
  
  // now time to check the end game


  if (video.nbCompletedShots === video.shots.length) {
    console.log(`we have finished each individual shot!`)

    if (!video.hasAssembledVideo) {
      video.hasAssembledVideo = true
    }
    /*
    console.log(`assembling the final..`)
    console.log(`note: this might be redundant..`)
  
    if (!video.hasAssembledVideo) {
      video.hasAssembledVideo = true
    if (video.shots.length === 1) {
      console.log(`we only have one shot, so this gonna be easy`)
      video.hasAssembledVideo = true

      // the single shot (so, the first) becomes the final movie
      await copyVideoFromPendingToCompleted(video.shots[0].fileName, video.fileName)

      if (await saveAndCheckIfNeedToStop(video)) { return }
    } else {
        console.log(`assembling ${video.shots.length} shots together, might take a while`)
        try {
          await assembleShots(video.shots, video.fileName)
          console.log(`finished assembling the ${video.shots.length} shots together!`)

          await copyVideoFromPendingToCompleted(video.fileName)

          video.hasAssembledVideo = true

          if (await saveAndCheckIfNeedToStop(video)) { return }
        } catch (err) {
          console.error(`failed to assemble the shots together (${err})`)
          // something is wrong, let's put the whole thing back into the queue
          video.error = `failed to assemble the shots together (will try again later)`
          if (await saveAndCheckIfNeedToStop(video)) { return }
        }
      }
    }
    */

    nbCompletedSteps++
    video.completed = true
    if (await saveAndCheckIfNeedToStop(video)) { return }
  }
}