[fix] pinterest videos can be downloaded without problems
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
import { spawn } from "child_process";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
|
||||
const so = os.platform();
|
||||
const platform = os.platform();
|
||||
|
||||
export async function get_video(url, id) {
|
||||
const cmd = so === "win32" ? ".\\bin\\yt-dlp.exe" : "./bin/yt-dlp";
|
||||
// garante que a pasta exista
|
||||
const downloadsDir = path.resolve("downloads");
|
||||
fs.mkdirSync(downloadsDir, { recursive: true });
|
||||
|
||||
const cmd = platform === "win32" ? ".\\bin\\yt-dlp.exe" : "./bin/yt-dlp";
|
||||
const args = [
|
||||
'--extractor-args', 'youtube:player_client=android',
|
||||
'-f', 'bv+ba/best',
|
||||
'--print', 'after_move:filepath',
|
||||
'--output', `downloads/${id}.%(ext)s`,
|
||||
'--output', path.join(downloadsDir, `${id}.%(ext)s`),
|
||||
'--cookies', 'cookies.txt',
|
||||
'--add-header', 'User-Agent:Mozilla/5.0',
|
||||
'--add-header', 'Referer:https://www.youtube.com/',
|
||||
@@ -17,7 +23,6 @@ export async function get_video(url, id) {
|
||||
'--fragment-retries', '5',
|
||||
'--socket-timeout', '15',
|
||||
'--sleep-interval', '1', '--max-sleep-interval', '4',
|
||||
'--allow-unplayable-formats',
|
||||
url
|
||||
];
|
||||
|
||||
@@ -30,7 +35,20 @@ async function runCmd(cmd, args) {
|
||||
let stdout = "";
|
||||
|
||||
proc.stdout.on("data", data => stdout += data.toString());
|
||||
proc.stderr.on("data", data => console.error("[cmd ERR]", data.toString()));
|
||||
proc.on("close", code => code === 0 ? resolve(stdout.trim()) : reject(new Error("Processo saiu com código "+code)));
|
||||
proc.stderr.on("data", data => console.error("[yt-dlp ERR]", data.toString()));
|
||||
|
||||
proc.on("close", code => {
|
||||
if (code !== 0) return reject(new Error("yt-dlp saiu com código " + code));
|
||||
|
||||
// Pega a última linha, que é o caminho final do arquivo
|
||||
const lines = stdout.trim().split("\n").filter(l => l.trim());
|
||||
const filepath = lines[lines.length - 1];
|
||||
|
||||
if (!fs.existsSync(filepath)) {
|
||||
return reject(new Error("Arquivo não encontrado: " + filepath));
|
||||
}
|
||||
|
||||
resolve(filepath);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user