MhankBarBar / whatsapp-bot

WhatsApp Bot
Apache License 2.0
725 stars 837 forks source link

!sgif errors sometimes #93

Closed LuCa-0 closed 3 years ago

LuCa-0 commented 3 years ago

Hi! Awesome project! I really love it. Sadly sometimes when i try to convert a video to animated sticker i get the following error:

~> [EXEC] 29/10 20:54:48 !sgif from Luca
(node:33468) UnhandledPromiseRejectionWarning: Error: Evaluation failed: t
    at ExecutionContext._evaluateInternal (/Users/luca/Downloads/whatsapp-bot-master/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:217:19)
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
    at async ExecutionContext.evaluate (/Users/luca/Downloads/whatsapp-bot-master/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:106:16)
(node:33468) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:33468) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Any idea on how to solve this?

LuCa-0 commented 3 years ago

I did some tests and seems like it happens when the converted .gif is more than 3Mb. Maybe the base64 conversion has issues? Actually just tried and it works with a 3.9Mb gif, I don't know... Thank you in advance

LuCa-0 commented 3 years ago

Why did you close this? It's still an issue...

416145 commented 3 years ago

Hi LuCa-0!

I had the same issue yesterday. With some debugging I found out it only happens when you try to convert a large video. Small videos to animated stickers are no problem. Is this the same for you? I think the issue is that the base64 conversion only can handle files op to a certain file size.

Anyway, I fixed it by adding some try/catches. So when the program can't convert it to a sticker it tries it again with less FPS. The code isn't very clean, but at least it works ;)

if (isMedia) {
    const meta = {
        author: '[YOUR NAME HERE]',
        pack: '[STICKERPACK NAME HERE]',
        keepScale: true
    };

    if (mimetype === 'video/mp4' && message.duration < 10 || mimetype === 'image/gif' && message.duration < 10) {
        const mediaData = await decryptMedia(message, uaOverride)
        client.reply(from, 'wait a sec, your video is being converted', id)
        const filename = `./media/aswu.${mimetype.split('/')[1]}`
        await fs.writeFileSync(filename, mediaData)

        await exec(`gify ${filename} ./media/output.gif --fps=30 --scale=240:240`, async function (error, stdout, stderr) {
            try {
                const gif = await fs.readFileSync('./media/output.gif', { encoding: "base64" })
                await client.sendImageAsSticker(from, `data:image/gif;base64,${gif.toString('base64')}`, meta)
                console.log('30FPS')
            } catch {
                await exec(`gify ${filename} ./media/output.gif --fps=15 --scale=240:240`, async function (error, stdout, stderr) {
                    try {
                        const gif = await fs.readFileSync('./media/output.gif', { encoding: "base64" })
                        await client.sendImageAsSticker(from, `data:image/gif;base64,${gif.toString('base64')}`)
                        console.log('15FPS')
                    } catch {
                        await exec(`gify ${filename} ./media/output.gif --fps=5 --scale=240:240`, async function (error, stdout, stderr) {
                            try {
                                const gif = await fs.readFileSync('./media/output.gif', {encoding: "base64"})
                                await client.sendImageAsSticker(from, `data:image/gif;base64,${gif.toString('base64')}`)
                                console.log('5FPS')
                            } catch {
                                client.reply(from, 'file is too big, can't convert it :(', id)
                            }
                        })
                    }
                })
            }
        })
    } else (
        client.reply(from, 'max duration of a video or GIF is 10 seconds' , id)
    )
}