replace exec with execFIle

(Should've done this long ago)
This commit is contained in:
Supositware 2024-02-02 03:28:46 +01:00
parent 35b57c219f
commit 76f7e40d8f
7 changed files with 25 additions and 23 deletions

View file

@ -36,8 +36,8 @@ export default {
if (!res.ok) return interaction.editReply('An error has occured while trying to download your image.'); if (!res.ok) return interaction.editReply('An error has occured while trying to download your image.');
await streamPipeline(res.body, fs.createWriteStream(`${os.tmpdir()}/${args.audio.name}`)); await streamPipeline(res.body, fs.createWriteStream(`${os.tmpdir()}/${args.audio.name}`));
await utils.ffmpeg(`-i ${os.tmpdir()}/${args.audio.name} -sample_rate 44100 -ac 1 -f s16le -acodec pcm_s16le ${os.tmpdir()}/${args.audio.name}.sw`); await utils.ffmpeg(['-i', `${os.tmpdir()}/${args.audio.name}`, '-sample_rate', '44100', '-ac', '1', '-f', 's16le', '-acodec', 'pcm_s16le', `${os.tmpdir()}/${args.audio.name}.sw`]);
await utils.ffmpeg(`-pixel_format rgb24 -video_size 128x128 -f rawvideo -i ${os.tmpdir()}/${args.audio.name}.sw -frames:v 1 ${os.tmpdir()}/${args.audio.name}.png`); await utils.ffmpeg(['-pixel_format', 'rgb24', '-video_size', '128x128', '-f', 'rawvideo', '-i', `${os.tmpdir()}/${args.audio.name}.sw`, '-frames:v', '1', `${os.tmpdir()}/${args.audio.name}.png`]);
const file = fs.statSync(`${os.tmpdir()}/${args.audio.name}.png`); const file = fs.statSync(`${os.tmpdir()}/${args.audio.name}.png`);
const fileSize = (file.size / 1000000.0).toFixed(2); const fileSize = (file.size / 1000000.0).toFixed(2);

View file

@ -35,8 +35,8 @@ export default {
if (!res.ok) return interaction.editReply('An error has occured while trying to download your image.'); if (!res.ok) return interaction.editReply('An error has occured while trying to download your image.');
await streamPipeline(res.body, fs.createWriteStream(`${os.tmpdir()}/${args.img.name}`)); await streamPipeline(res.body, fs.createWriteStream(`${os.tmpdir()}/${args.img.name}`));
await utils.ffmpeg(`-i ${os.tmpdir()}/${args.img.name} -f rawvideo ${os.tmpdir()}/1${args.img.name}`); await utils.ffmpeg(['-i', `${os.tmpdir()}/${args.img.name}`, '-f', 'rawvideo', `${os.tmpdir()}/1${args.img.name}`]);
await utils.ffmpeg(`-sample_rate 44100 -ac 1 -f s16le -i ${os.tmpdir()}/1${args.img.name} ${os.tmpdir()}/${args.img.name}.mp3`); await utils.ffmpeg(['-sample_rate', '44100', '-ac', '1', '-f', 's16le', '-i', `${os.tmpdir()}/1${args.img.name}`, `${os.tmpdir()}/${args.img.name}.mp3`]);
const file = fs.statSync(`${os.tmpdir()}/${args.img.name}.mp3`); const file = fs.statSync(`${os.tmpdir()}/${args.img.name}.mp3`);
const fileSize = (file.size / 1000000.0).toFixed(2); const fileSize = (file.size / 1000000.0).toFixed(2);

View file

@ -1,6 +1,6 @@
import { SlashCommandBuilder } from 'discord.js'; import { SlashCommandBuilder } from 'discord.js';
import { EmbedBuilder } from 'discord.js'; import { EmbedBuilder } from 'discord.js';
import { exec } from 'node:child_process'; import { execFile } from 'node:child_process';
import db from '../../models/index.js'; import db from '../../models/index.js';
const donator = db.donator; const donator = db.donator;
@ -40,7 +40,7 @@ export default {
// description += '\nThanks to Jetbrains for providing their IDE!'; // description += '\nThanks to Jetbrains for providing their IDE!';
exec('git rev-parse --short HEAD', (err, stdout) => { execFile('git', ['rev-parse', '--short', 'HEAD'], (err, stdout) => {
const aboutEmbed = new EmbedBuilder() const aboutEmbed = new EmbedBuilder()
.setColor(interaction.member ? interaction.member.displayHexColor : 'Navy') .setColor(interaction.member ? interaction.member.displayHexColor : 'Navy')
.setAuthor({ name: client.user.username, iconURL: client.user.displayAvatarURL(), url: 'https://libtar.de' }) .setAuthor({ name: client.user.username, iconURL: client.user.displayAvatarURL(), url: 'https://libtar.de' })

View file

@ -1,5 +1,5 @@
import { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, StringSelectMenuBuilder } from 'discord.js'; import { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, StringSelectMenuBuilder } from 'discord.js';
import { exec } from 'node:child_process'; import { execFile } from 'node:child_process';
import fs from 'node:fs'; import fs from 'node:fs';
import os from 'node:os'; import os from 'node:os';
import utils from '../../utils/videos.js'; import utils from '../../utils/videos.js';
@ -50,7 +50,7 @@ export default {
if (format) { if (format) {
let qualitys = await new Promise((resolve, reject) => { let qualitys = await new Promise((resolve, reject) => {
exec(`./bin/yt-dlp "${url}" --print "%()j"`, (err, stdout, stderr) => { execFile('./bin/yt-dlp', [url, '--print', '%()j'], (err, stdout, stderr) => {
if (err) { if (err) {
reject(stderr); reject(stderr);
} }
@ -137,7 +137,7 @@ async function download(url, interaction, originalInteraction, format = undefine
const compressInteraction = originalInteraction ? originalInteraction : interaction; const compressInteraction = originalInteraction ? originalInteraction : interaction;
if (compressInteraction.doCompress) { if (compressInteraction.doCompress) {
const presets = [ 'Social 8 MB 3 Minutes 360p30', 'Social 50 MB 10 Minutes 480p30', 'Social 50 MB 5 Minutes 720p30', 'Social 100 MB 5 Minutes 1080p30' ]; const presets = [ 'Social 25 MB 5 Minutes 360p60', 'Social 50 MB 10 Minutes 480p30', 'Social 50 MB 5 Minutes 720p30', 'Social 100 MB 5 Minutes 1080p30' ];
const options = []; const options = [];
presets.forEach(p => { presets.forEach(p => {
@ -176,7 +176,7 @@ async function download(url, interaction, originalInteraction, format = undefine
if (bannedFormats.includes(codec)) { if (bannedFormats.includes(codec)) {
const oldOutput = output; const oldOutput = output;
output = `${os.tmpdir()}/264${file}`; output = `${os.tmpdir()}/264${file}`;
await utils.ffmpeg(`-i ${oldOutput} -vcodec libx264 -acodec aac ${output}`); await utils.ffmpeg(['-i', oldOutput, '-vcodec', 'libx264', '-acodec', 'aac', output]);
} }
const fileStat = fs.statSync(output); const fileStat = fs.statSync(output);

View file

@ -3,7 +3,7 @@ import utils from '../../utils/videos.js';
import fs from 'node:fs'; import fs from 'node:fs';
import os from 'node:os'; import os from 'node:os';
import path from 'node:path'; import path from 'node:path';
import { exec } from 'node:child_process'; import { execFile } from 'node:child_process';
const { NODE_ENV } = process.env; const { NODE_ENV } = process.env;
@ -50,7 +50,7 @@ export default {
const gifsicleOutput = output.replace(path.extname(output), 'gifsicle.gif'); const gifsicleOutput = output.replace(path.extname(output), 'gifsicle.gif');
// Extract every frame for gifski // Extract every frame for gifski
await utils.ffmpeg(`-i ${output} ${os.tmpdir()}/frame${interaction.id}%04d.png`); await utils.ffmpeg(['-i', output, `${os.tmpdir()}/frame${interaction.id}%04d.png`]);
// Make it look better // Make it look better
await gifski(gifskiOutput, `${os.tmpdir()}/frame${interaction.id}*`, quality); await gifski(gifskiOutput, `${os.tmpdir()}/frame${interaction.id}*`, quality);
// Optimize it // Optimize it
@ -79,7 +79,8 @@ export default {
async function gifski(output, input, quality) { async function gifski(output, input, quality) {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
exec(`gifski --quality ${quality ? quality : 70} -o ${output} ${input}`, (err, stdout, stderr) => { // Shell: true should be fine as no user input is being passed
execFile('gifski', ['--quality', quality ? quality : 70, '-o', output, input], { shell: true }, (err, stdout, stderr) => {
if (err) { if (err) {
reject(stderr); reject(stderr);
} }
@ -94,7 +95,8 @@ async function gifski(output, input, quality) {
async function gifsicle(input, output, loop = false) { async function gifsicle(input, output, loop = false) {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
exec(`gifsicle --colors 256 ${loop ? '--no-loopcount' : ''} -i ${input} -o ${output}`, (err, stdout, stderr) => { // Shell: true should be fine as no user input is being passed
execFile('gifsicle', ['--colors', '256', loop ? '--no-loopcount' : '', '-i', input, '-o', output], { shell: true }, (err, stdout, stderr) => {
if (err) { if (err) {
reject(stderr); reject(stderr);
} }

View file

@ -1,4 +1,4 @@
import { exec } from 'node:child_process'; import { execFile } from 'node:child_process';
const { statusChannel, NODE_ENV } = process.env; const { statusChannel, NODE_ENV } = process.env;
import { version } from 'discord.js'; import { version } from 'discord.js';
@ -10,7 +10,7 @@ export default {
global.boards = {}; global.boards = {};
const ytdlpVersion = await new Promise((resolve, reject) => { const ytdlpVersion = await new Promise((resolve, reject) => {
exec('./bin/yt-dlp --version', (err, stdout, stderr) => { execFile('./bin/yt-dlp', ['--version'], (err, stdout, stderr) => {
if (err) { if (err) {
reject(stderr); reject(stderr);
} }

View file

@ -1,5 +1,5 @@
import os from 'node:os'; import os from 'node:os';
import { exec } from 'node:child_process'; import { execFile } from 'node:child_process';
const { NODE_ENV } = process.env; const { NODE_ENV } = process.env;
const ytdlpMaxResolution = 2160; const ytdlpMaxResolution = 2160;
@ -15,7 +15,7 @@ export default {
}; };
async function downloadVideo(urlArg, output, format = `bestvideo[height<=?${ytdlpMaxResolution}]+bestaudio/best`) { async function downloadVideo(urlArg, output, format = `bestvideo[height<=?${ytdlpMaxResolution}]+bestaudio/best`) {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
exec(`./bin/yt-dlp -f "${format}" "${urlArg}" -o "${os.tmpdir()}/${output}.%(ext)s" --force-overwrites --no-playlist --remux-video=mp4/webm/mov --no-warnings`, (err, stdout, stderr) => { execFile('./bin/yt-dlp', ['-f', format, urlArg, '-o', `${os.tmpdir()}/${output}.%(ext)s`, '--force-overwrites', '--no-playlist', '--remux-video=mp4/webm/mov', '--no-warnings'], (err, stdout, stderr) => {
if (err) { if (err) {
return reject(stderr); return reject(stderr);
} }
@ -32,7 +32,7 @@ async function downloadVideo(urlArg, output, format = `bestvideo[height<=?${ytdl
async function upload(file) { async function upload(file) {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
exec(`./bin/upload.sh ${file}`, (err, stdout, stderr) => { execFile('./bin/upload.sh', [file], (err, stdout, stderr) => {
if (err) { if (err) {
reject(stderr); reject(stderr);
} }
@ -46,7 +46,7 @@ async function upload(file) {
async function ffmpeg(command) { async function ffmpeg(command) {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
exec(`ffmpeg -hide_banner ${command}`, (err, stdout, stderr) => { execFile('ffmpeg', ['-hide_banner', ...command], (err, stdout, stderr) => {
if (err) { if (err) {
reject(stderr); reject(stderr);
} }
@ -71,7 +71,7 @@ async function stringIsAValidurl(s) {
async function compressVideo(input, output, preset) { async function compressVideo(input, output, preset) {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
exec(`./bin/HandBrakeCLI -i "${input}" -Z "${preset}" -o "${os.tmpdir()}/${output}"`, (err, stdout, stderr) => { execFile('./bin/HandBrakeCLI', ['-i', input, '-Z', preset, '-o', `${os.tmpdir()}/${output}`], (err, stdout, stderr) => {
if (err) { if (err) {
reject(stderr); reject(stderr);
} }
@ -85,7 +85,7 @@ async function compressVideo(input, output, preset) {
} }
async function getVideoCodec(input) { async function getVideoCodec(input) {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
exec(`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "${input}"`, (err, stdout, stderr) => { execFile('ffprobe', ['-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=codec_name', '-of', 'default=noprint_wrappers=1:nokey=1', input], (err, stdout, stderr) => {
if (err) { if (err) {
reject(stderr); reject(stderr);
} }
@ -99,7 +99,7 @@ async function getVideoCodec(input) {
async function getVideoSize(urlArg, format = `bestvideo[height<=?${ytdlpMaxResolution}]+bestaudio/best`) { async function getVideoSize(urlArg, format = `bestvideo[height<=?${ytdlpMaxResolution}]+bestaudio/best`) {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
exec(`./bin/yt-dlp "${urlArg}" -f "${format}" --no-warnings -O "%(filesize,filesize_approx)s"`, (err, stdout, stderr) => { execFile('./bin/yt-dlp', [urlArg, '-f', format, '--no-warnings', '-O', '%(filesize,filesize_approx)s'], (err, stdout, stderr) => {
if (err) { if (err) {
reject(stderr); reject(stderr);
} }