const axios = require('axios'); const FormData = require('form-data'); const { fromBuffer } = require('file-type'); class BgBye { constructor() { this.url = 'https://bgbye2.fyrean.com'; this.method = ['bria', 'inspyrenet', 'u2net', 'tracer', 'basnet', 'deeplab', 'u2net_human_seg', 'ormbg', 'isnet-general-use', 'isnet-anime']; } image = async function (buffer, { method = 'bria' } = {}) { try { if (!buffer || !Buffer.isBuffer(buffer)) throw new Error('Image buffer is required'); if (!this.method.includes(method)) throw new Error(`Available methods: ${this.method.join(', ')}`); const { mime } = await fromBuffer(buffer); if (!/image/.test(mime)) throw new Error('Image must be a buffer'); const form = new FormData(); form.append('file', buffer, `${Date.now()}_rynn.jpg`); form.append('method', method) const { data } = await axios.post(`${this.url}/remove_background/`, form, { headers: form.getHeaders(), responseType: 'arraybuffer' }); return data; } catch (error) { throw new Error(error.message); } } video = async function (buffer, { method = 'bria' } = {}) { try { if (!buffer || !Buffer.isBuffer(buffer)) throw new Error('Video buffer is required'); if (!this.method.includes(method)) throw new Error(`Available methods: ${this.method.join(', ')}`); const { mime } = await fromBuffer(buffer); if (!/video/.test(mime)) throw new Error('Video must be a buffer'); const form = new FormData(); form.append('file', buffer, `${Date.now()}_rynn.mp4`); form.append('method', method) const { data: task } = await axios.post(`${this.url}/remove_background_video/`, form, { headers: form.getHeaders() }); while (true) { const { data: poll } = await axios.get(`${this.url}/status/${task.video_id}`); if (poll?.status !== 'processing' || !poll.status) { const { data } = await axios.get(`${this.url}/status/${task.video_id}`, { responseType: 'arraybuffer' }); return data; } await new Promise(res => setTimeout(res, 1000)); } } catch (error) { throw new Error(error.message); } } } // Usage: const fs = require('fs'); const b = new BgBye(); const resp = await b.video(fs.readFileSync('./videoo.mp4')); await fs.writeFileSync('./bgbye.webm', resp);