Rianofc commited on
Commit
48daa4f
·
verified ·
1 Parent(s): 2af8e84

Create brat.js

Browse files
Files changed (1) hide show
  1. brat.js +66 -0
brat.js ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { exec } = require('child_process');
4
+ const { chromium } = require('playwright');
5
+
6
+ const TEMP_DIR = '/tmp';
7
+ if (!fs.existsSync(TEMP_DIR)) fs.mkdirSync(TEMP_DIR, { recursive: true });
8
+
9
+ async function brat(text) {
10
+ const browser = await chromium.launch({ headless: true });
11
+ const page = await browser.newPage();
12
+
13
+ await page.goto('https://www.bratgenerator.com/');
14
+ await page.click('#toggleButtonWhite');
15
+ await page.fill('#textInput', text);
16
+ await page.waitForTimeout(500);
17
+
18
+ const imgPath = path.join(TEMP_DIR, `brat_${Date.now()}.png`);
19
+ await page.locator('#textOverlay').screenshot({ path: imgPath });
20
+
21
+ await browser.close();
22
+
23
+ console.log(imgPath);
24
+ }
25
+
26
+ async function bratVid(text) {
27
+ const words = text.split(" ");
28
+ let currentText = "";
29
+ const framePaths = [];
30
+
31
+ const browser = await chromium.launch({ headless: true });
32
+ const page = await browser.newPage();
33
+
34
+ await page.goto('https://www.bratgenerator.com/');
35
+ await page.click('#toggleButtonWhite');
36
+
37
+ for (let i = 0; i < Math.min(words.length, 40); i++) {
38
+ currentText += (i === 0 ? "" : " ") + words[i];
39
+ await page.fill('#textInput', currentText);
40
+ await page.waitForTimeout(500);
41
+
42
+ const framePath = path.join(TEMP_DIR, `brat_${Date.now()}_${i}.png`);
43
+ await page.locator('#textOverlay').screenshot({ path: framePath });
44
+ framePaths.push(framePath);
45
+ }
46
+
47
+ await browser.close();
48
+
49
+ if (!framePaths.length) throw new Error("Gagal membuat gambar");
50
+
51
+ const fileListPath = path.join(TEMP_DIR, 'filelist.txt');
52
+ fs.writeFileSync(fileListPath, framePaths.map(f => `file '${f}'\nduration 0.7\n`).join('') +
53
+ `file '${framePaths.at(-1)}'\nduration 2\n`);
54
+
55
+ const outputPath = path.join(TEMP_DIR, `brat_${Date.now()}.mp4`);
56
+ await new Promise((resolve, reject) => {
57
+ exec(`ffmpeg -y -f concat -safe 0 -i "${fileListPath}" -vf "fps=30,scale=512:512" -c:v libx264 -preset ultrafast -pix_fmt yuv420p "${outputPath}"`,
58
+ (error) => error ? reject(error) : resolve());
59
+ });
60
+
61
+ console.log(outputPath); // Output video path ke stdout
62
+ }
63
+
64
+ const args = process.argv.slice(2);
65
+ if (args[0] === 'vid') bratVid(args[1]).catch(console.error);
66
+ else brat(args[0]).catch(console.error);