File size: 3,058 Bytes
e0454ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Roda a renderizacao EM LOTE: pega varios videos e aplica o mesmo molde,
// atualizando o progresso no banco. Processa poucos por vez (CPU).
import fs from 'node:fs';
import path from 'node:path';
import { get, all, run, insert } from '../db.js';
import { DIRS } from '../config.js';
import { renderOne } from './render.js';
import { probe } from '../ffmpeg.js';

const CONCURRENCY = 2;

// Cria o job e comeca a processar em background. Retorna o jobId na hora.
// ownerId: dono (usuario logado) — carimbado no job, nos itens e nos videos prontos.
export function startBatch({ moldId, sources, focus, audioPath = null, ownerId }) {
  const mold = get('SELECT * FROM molds WHERE id = ? AND owner_id = ?', [moldId, ownerId]);
  if (!mold) throw new Error('Molde nao encontrado');

  const outputDir = path.join(DIRS.output, `job-${Date.now()}`);
  fs.mkdirSync(outputDir, { recursive: true });

  const jobId = insert(
    'INSERT INTO render_jobs (owner_id, mold_id, status, total, output_dir) VALUES (?,?,?,?,?)',
    [ownerId, moldId, 'running', sources.length, outputDir]
  );
  for (const src of sources) {
    insert('INSERT INTO render_items (owner_id, job_id, source_path, status) VALUES (?,?,?,?)', [ownerId, jobId, src, 'pending']);
  }

  processJob(jobId, mold, outputDir, focus, audioPath, ownerId); // nao espera (roda em background)
  return jobId;
}

async function processJob(jobId, mold, outputDir, focus, audioPath, ownerId) {
  const items = all('SELECT * FROM render_items WHERE job_id = ?', [jobId]);
  let index = 0;

  async function worker() {
    while (index < items.length) {
      const item = items[index++];
      const base = path.parse(item.source_path).name.replace(/[^\w.-]/g, '_');
      const outPath = path.join(outputDir, `${base}_dark.mp4`);
      try {
        await renderOne(item.source_path, mold, outPath, focus, audioPath);
        const info = await probe(outPath).catch(() => ({}));
        insert(
          'INSERT INTO media_assets (owner_id, kind, file_path, label, duration, width, height) VALUES (?,?,?,?,?,?,?)',
          [ownerId, 'rendered', outPath, base, info.duration || null, info.width || null, info.height || null]
        );
        run('UPDATE render_items SET status = ?, output_path = ? WHERE id = ?', ['done', outPath, item.id]);
      } catch (err) {
        run('UPDATE render_items SET status = ?, error = ? WHERE id = ?', ['failed', String(err.message || err), item.id]);
      }
      run('UPDATE render_jobs SET done = done + 1 WHERE id = ?', [jobId]);
    }
  }

  const workers = Array.from({ length: Math.min(CONCURRENCY, items.length) }, () => worker());
  await Promise.all(workers);
  run('UPDATE render_jobs SET status = ? WHERE id = ?', ['done', jobId]);
}

export function jobStatus(jobId, ownerId) {
  const job = get('SELECT * FROM render_jobs WHERE id = ? AND owner_id = ?', [jobId, ownerId]);
  if (!job) return null;
  const items = all('SELECT id, source_path, output_path, status, error FROM render_items WHERE job_id = ?', [jobId]);
  return { ...job, items };
}