File size: 5,229 Bytes
9844ee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFfmpegPath = getFfmpegPath;
exports.convertToMP4GIF = convertToMP4GIF;
/*
 * This file is part of WPPConnect.
 *
 * WPPConnect is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WPPConnect is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with WPPConnect.  If not, see <https://www.gnu.org/licenses/>.
 */
const execa_1 = __importDefault(require("execa"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const rimraf_1 = __importDefault(require("rimraf"));
const tmp = __importStar(require("tmp"));
const lookpath_1 = require("lookpath");
const tmpDir = tmp.dirSync({});
let i = 0;
process.on('exit', () => {
    // Remove only on exit signal
    try {
        // Use rimraf because it is synchronous
        rimraf_1.default.sync(tmpDir.name);
    }
    catch (error) { }
});
async function getFfmpegPath() {
    let ffmpegPath = process.env['FFMPEG_PATH'];
    if (ffmpegPath) {
        const isExecutable = await fs.promises
            .access(ffmpegPath, fs.constants.X_OK)
            .then(() => true)
            .catch(() => false);
        if (isExecutable) {
            return ffmpegPath;
        }
    }
    ffmpegPath = await (0, lookpath_1.lookpath)('ffmpeg', {
        include: [
            'C:\\FFmpeg\\bin',
            'C:\\FFmpeg\\FFmpeg\\bin',
            'C:\\Program Files\\ffmpeg\\bin',
            'C:\\Program Files (x86)\\ffmpeg\\bin',
        ],
    });
    if (!ffmpegPath) {
        try {
            ffmpegPath = require('ffmpeg-static');
        }
        catch (error) { }
    }
    if (!ffmpegPath) {
        throw new Error('Error: FFMPEG not found. Please install ffmpeg or define the env FFMPEG_PATH or install ffmpeg-static');
    }
    return ffmpegPath;
}
/**
 * Convert a file to a compatible MP4-GIF for WhatsApp
 * @param inputBase64 Gif in base64 format
 * @returns base64 of a MP4-GIF for WhatsApp
 */
async function convertToMP4GIF(inputBase64) {
    const inputPath = path.join(tmpDir.name, `${i++}`);
    const outputPath = path.join(tmpDir.name, `${i++}.mp4`);
    if (inputBase64.includes(',')) {
        inputBase64 = inputBase64.split(',')[1];
    }
    fs.writeFileSync(inputPath, Buffer.from(inputBase64, 'base64'));
    /**
     * fluent-ffmpeg is a good alternative,
     * but to work with MP4 you must use fisical file or ffmpeg will not work
     * Because of that, I made the choice to use temporary file
     */
    const ffmpegPath = await getFfmpegPath();
    try {
        const out = await (0, execa_1.default)(ffmpegPath, [
            '-i',
            inputPath,
            '-movflags',
            'faststart',
            '-pix_fmt',
            'yuv420p',
            '-vf',
            'scale=trunc(iw/2)*2:trunc(ih/2)*2',
            '-f',
            'mp4',
            outputPath,
        ]);
        if (out.exitCode === 0) {
            const outputBase64 = fs.readFileSync(outputPath);
            return 'data:video/mp4;base64,' + outputBase64.toString('base64');
        }
        throw out.stderr;
    }
    finally {
        (0, rimraf_1.default)(inputPath, () => null);
        (0, rimraf_1.default)(outputPath, () => null);
    }
    return '';
}