Spaces:
Paused
Paused
Julian Bilcke
commited on
Commit
·
e2472ff
1
Parent(s):
9658ad9
seems to work, nice
Browse files- src/index.mts +26 -15
- src/services/downloadVideo.mts +6 -1
- src/test.mts +23 -0
src/index.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import { promises as fs } from 'fs'
|
|
|
|
| 2 |
|
|
|
|
| 3 |
import express from 'express'
|
| 4 |
|
| 5 |
import { generateVideo } from './services/generateVideo.mts'
|
|
@@ -17,16 +19,18 @@ app.use(express.json())
|
|
| 17 |
app.post('/shot', async (req, res) => {
|
| 18 |
const query = req.body as MakeShot
|
| 19 |
|
|
|
|
| 20 |
const token = `${query.token || ''}`
|
| 21 |
if (token !== process.env.VS_SECRET_ACCESS_TOKEN) {
|
|
|
|
| 22 |
res.write(JSON.stringify({ error: true, message: 'access denied' }))
|
| 23 |
res.end()
|
| 24 |
return
|
| 25 |
}
|
| 26 |
|
| 27 |
const shotPrompt = `${query.shotPrompt || ''}`
|
| 28 |
-
if (shotPrompt.length) {
|
| 29 |
-
res.write(JSON.stringify({ error: true, message: 'prompt too short' }))
|
| 30 |
res.end()
|
| 31 |
return
|
| 32 |
}
|
|
@@ -37,32 +41,36 @@ app.post('/shot', async (req, res) => {
|
|
| 37 |
// optional audio prompt
|
| 38 |
const audioPrompt = `${query.audioPrompt || ''}`
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
|
| 46 |
// should we upscale or not?
|
| 47 |
const upscale = `${query.upscale || 'false'}` === 'true'
|
| 48 |
|
| 49 |
// duration of the prompt, in seconds
|
| 50 |
-
const
|
|
|
|
| 51 |
const maybeDuration = Number(durationStr)
|
| 52 |
-
const duration = Math.min(3, Math.max(1, isNaN(maybeDuration) || !isFinite(maybeDuration) ?
|
| 53 |
|
| 54 |
-
const
|
|
|
|
| 55 |
const maybeSteps = Number(stepsStr)
|
| 56 |
-
const nbSteps = Math.min(60, Math.max(1, isNaN(maybeSteps) || !isFinite(maybeSteps) ?
|
| 57 |
|
| 58 |
// const frames per second
|
| 59 |
-
const
|
|
|
|
| 60 |
const maybeFps = Number(fpsStr)
|
| 61 |
-
const fps = Math.min(60, Math.max(8, isNaN(maybeFps) || !isFinite(maybeFps) ?
|
| 62 |
|
| 63 |
-
const
|
|
|
|
| 64 |
const maybeResolution = Number(resolutionStr)
|
| 65 |
-
const resolution = Math.min(1080, Math.max(256, isNaN(maybeResolution) || !isFinite(maybeResolution) ?
|
| 66 |
|
| 67 |
|
| 68 |
const shotFileName = `${Date.now()}.mp4`
|
|
@@ -101,7 +109,10 @@ app.post('/shot', async (req, res) => {
|
|
| 101 |
}
|
| 102 |
|
| 103 |
console.log('returning result to user..')
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
res.setHeader('Content-Type', 'media/mp4')
|
| 106 |
res.setHeader('Content-Length', buffer.length)
|
| 107 |
res.end(buffer)
|
|
|
|
| 1 |
import { promises as fs } from 'fs'
|
| 2 |
+
import path from 'node:path'
|
| 3 |
|
| 4 |
+
import tmpDir from 'temp-dir'
|
| 5 |
import express from 'express'
|
| 6 |
|
| 7 |
import { generateVideo } from './services/generateVideo.mts'
|
|
|
|
| 19 |
app.post('/shot', async (req, res) => {
|
| 20 |
const query = req.body as MakeShot
|
| 21 |
|
| 22 |
+
console.log('received query:', query)
|
| 23 |
const token = `${query.token || ''}`
|
| 24 |
if (token !== process.env.VS_SECRET_ACCESS_TOKEN) {
|
| 25 |
+
console.log("couldn't find access token in the query")
|
| 26 |
res.write(JSON.stringify({ error: true, message: 'access denied' }))
|
| 27 |
res.end()
|
| 28 |
return
|
| 29 |
}
|
| 30 |
|
| 31 |
const shotPrompt = `${query.shotPrompt || ''}`
|
| 32 |
+
if (shotPrompt.length < 5) {
|
| 33 |
+
res.write(JSON.stringify({ error: true, message: 'prompt too short (must be at least 5 in length)' }))
|
| 34 |
res.end()
|
| 35 |
return
|
| 36 |
}
|
|
|
|
| 41 |
// optional audio prompt
|
| 42 |
const audioPrompt = `${query.audioPrompt || ''}`
|
| 43 |
|
| 44 |
+
// optional seed
|
| 45 |
+
const seedStr = Number(`${query.seed || ''}`)
|
| 46 |
+
const maybeSeed = Number(seedStr)
|
| 47 |
+
const seed = isNaN(maybeSeed) || ! isFinite(maybeSeed) ? generateSeed() : maybeSeed
|
| 48 |
|
| 49 |
|
| 50 |
// should we upscale or not?
|
| 51 |
const upscale = `${query.upscale || 'false'}` === 'true'
|
| 52 |
|
| 53 |
// duration of the prompt, in seconds
|
| 54 |
+
const defaultDuration = 3
|
| 55 |
+
const durationStr = Number(`${query.duration || defaultDuration}`)
|
| 56 |
const maybeDuration = Number(durationStr)
|
| 57 |
+
const duration = Math.min(3, Math.max(1, isNaN(maybeDuration) || !isFinite(maybeDuration) ? defaultDuration : maybeDuration))
|
| 58 |
|
| 59 |
+
const defaultSteps = 35
|
| 60 |
+
const stepsStr = Number(`${query.steps || defaultSteps}`)
|
| 61 |
const maybeSteps = Number(stepsStr)
|
| 62 |
+
const nbSteps = Math.min(60, Math.max(1, isNaN(maybeSteps) || !isFinite(maybeSteps) ? defaultSteps : maybeSteps))
|
| 63 |
|
| 64 |
// const frames per second
|
| 65 |
+
const defaultFps = 24
|
| 66 |
+
const fpsStr = Number(`${query.fps || defaultFps}`)
|
| 67 |
const maybeFps = Number(fpsStr)
|
| 68 |
+
const fps = Math.min(60, Math.max(8, isNaN(maybeFps) || !isFinite(maybeFps) ? defaultFps : maybeFps))
|
| 69 |
|
| 70 |
+
const defaultResolution = 576
|
| 71 |
+
const resolutionStr = Number(`${query.resolution || defaultResolution}`)
|
| 72 |
const maybeResolution = Number(resolutionStr)
|
| 73 |
+
const resolution = Math.min(1080, Math.max(256, isNaN(maybeResolution) || !isFinite(maybeResolution) ? defaultResolution : maybeResolution))
|
| 74 |
|
| 75 |
|
| 76 |
const shotFileName = `${Date.now()}.mp4`
|
|
|
|
| 109 |
}
|
| 110 |
|
| 111 |
console.log('returning result to user..')
|
| 112 |
+
|
| 113 |
+
const filePath = path.resolve(tmpDir, videoFileName)
|
| 114 |
+
|
| 115 |
+
const buffer = await fs.readFile(filePath)
|
| 116 |
res.setHeader('Content-Type', 'media/mp4')
|
| 117 |
res.setHeader('Content-Length', buffer.length)
|
| 118 |
res.end(buffer)
|
src/services/downloadVideo.mts
CHANGED
|
@@ -7,8 +7,13 @@ export const downloadVideo = async (remoteUrl: string, fileName: string): Promis
|
|
| 7 |
|
| 8 |
const filePath = path.resolve(tmpDir, fileName)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
// download the video
|
| 11 |
-
const response = await fetch(remoteUrl
|
|
|
|
|
|
|
| 12 |
|
| 13 |
// write it to the disk
|
| 14 |
const arrayBuffer = await response.arrayBuffer()
|
|
|
|
| 7 |
|
| 8 |
const filePath = path.resolve(tmpDir, fileName)
|
| 9 |
|
| 10 |
+
const controller = new AbortController()
|
| 11 |
+
const timeoutId = setTimeout(() => controller.abort(), 15 * 60 * 60 * 1000) // 15 minutes
|
| 12 |
+
|
| 13 |
// download the video
|
| 14 |
+
const response = await fetch(remoteUrl, {
|
| 15 |
+
signal: controller.signal
|
| 16 |
+
})
|
| 17 |
|
| 18 |
// write it to the disk
|
| 19 |
const arrayBuffer = await response.arrayBuffer()
|
src/test.mts
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { promises as fs } from "node:fs"
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
console.log('generating shot..')
|
| 5 |
+
const response = await fetch("http://localhost:7860/shot", {
|
| 6 |
+
method: "POST",
|
| 7 |
+
headers: {
|
| 8 |
+
"Accept": "application/json",
|
| 9 |
+
"Content-Type": "application/json"
|
| 10 |
+
},
|
| 11 |
+
body: JSON.stringify({
|
| 12 |
+
token: process.env.VS_SECRET_ACCESS_TOKEN,
|
| 13 |
+
shotPrompt: "video of a dancing cat"
|
| 14 |
+
})
|
| 15 |
+
});
|
| 16 |
+
|
| 17 |
+
console.log('response:', response)
|
| 18 |
+
const buffer = await (response as any).buffer()
|
| 19 |
+
|
| 20 |
+
fs.writeFile(`./test-juju.mp4`, buffer)
|
| 21 |
+
|
| 22 |
+
// if called from an API, we ùight want to use streams instead:
|
| 23 |
+
// https://stackoverflow.com/questions/15713424/how-can-i-download-a-video-mp4-file-using-node-js
|