|
import path from "node:path" |
|
|
|
import { v4 as uuidv4 } from "uuid" |
|
import tmpDir from "temp-dir" |
|
import puppeteer from "puppeteer" |
|
|
|
import { downloadFileToTmp } from '../utils/downloadFileToTmp.mts' |
|
import { pendingFilesDirFilePath } from '../config.mts' |
|
import { moveFileFromTmpToPending } from "../utils/moveFileFromTmpToPending.mts" |
|
|
|
const instances: string[] = [ |
|
process.env.VC_VIDEO_UPSCALE_SPACE_API_URL |
|
] |
|
|
|
|
|
export async function upscaleVideo(fileName: string, prompt: string) { |
|
const instance = instances.shift() |
|
instances.push(instance) |
|
|
|
const browser = await puppeteer.launch({ |
|
|
|
protocolTimeout: 800000, |
|
}) |
|
|
|
try { |
|
const page = await browser.newPage() |
|
|
|
await page.goto(instance, { |
|
waitUntil: 'networkidle2', |
|
}) |
|
|
|
const promptField = await page.$('textarea') |
|
await promptField.type(prompt) |
|
|
|
const inputFilePath = path.join(pendingFilesDirFilePath, fileName) |
|
|
|
|
|
await new Promise(r => setTimeout(r, 3000)) |
|
|
|
const fileField = await page.$('input[type=file]') |
|
|
|
|
|
await fileField.uploadFile(inputFilePath) |
|
|
|
|
|
const submitButton = await page.$('button.lg') |
|
|
|
|
|
await submitButton.click() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await page.waitForSelector('a[download="xl_result.mp4"]', { |
|
timeout: 800000, |
|
}) |
|
|
|
const upscaledFileUrl = await page.$$eval('a[download="xl_result.mp4"]', el => el.map(x => x.getAttribute("href"))[0]) |
|
|
|
|
|
|
|
|
|
const tmpFileName = `${uuidv4()}.mp4` |
|
|
|
await downloadFileToTmp(upscaledFileUrl, tmpFileName) |
|
await moveFileFromTmpToPending(tmpFileName, fileName) |
|
} catch (err) { |
|
throw err |
|
} finally { |
|
await browser.close() |
|
} |
|
} |
|
|