frostpv 1.0.15 → 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +261 -54
  2. package/package.json +2 -1
package/index.js CHANGED
@@ -4,10 +4,11 @@ const path = require("path");
4
4
  const os = require("os");
5
5
  const FormData = require("form-data");
6
6
  const crypto = require("crypto");
7
- const { igdl, ttdl, fbdown, mediafire, capcut, gdrive, pinterest, twitter } = require("btch-downloader");
7
+ const { igdl, ttdl, fbdown, mediafire, capcut, youtube, gdrive, pinterest, twitter } = require("btch-downloader");
8
8
  const { TwitterDL } = require("twitter-downloader");
9
9
  const btch = require("btch-downloader");
10
10
  const btchOld = require("btch-downloader-old");
11
+ const ytdl = require("@distube/ytdl-core");
11
12
 
12
13
 
13
14
  const Tiktok = require("@tobyg74/tiktok-api-dl");
@@ -43,6 +44,10 @@ const videoPlatforms = [
43
44
  "https://fb.watch",
44
45
  "https://www.fb.watch",
45
46
  "https://fb.com",
47
+ "https://www.youtube.com",
48
+ "https://youtube.com",
49
+ "https://www.youtu.be",
50
+ "https://youtu.be",
46
51
  "https://www.fb.com",
47
52
  "https://web.facebook.com",
48
53
  "https://www.mediafire.com",
@@ -84,6 +89,7 @@ const defaultConfig = {
84
89
  autocrop: false,
85
90
  limitSizeMB: null,
86
91
  rotation: null,
92
+ YTBmaxduration: 1200,
87
93
  outputFormat: null,
88
94
  };
89
95
 
@@ -271,7 +277,7 @@ function getPlatformType(url) {
271
277
  if (lowerUrl.includes("facebook.com") || lowerUrl.includes("fb.watch") || lowerUrl.includes("fb.com")) return "facebook";
272
278
  if (lowerUrl.includes("mediafire.com")) return "mediafire";
273
279
  if (lowerUrl.includes("x.com") || lowerUrl.includes("twitter.com")) return "twitter";
274
- if (lowerUrl.includes("youtube.com") || lowerUrl.includes("you112t12u.be") || lowerUrl.includes("m.y11outu314be.com")) return "youtu1354be";
280
+ if (lowerUrl.includes("youtube.com") || lowerUrl.includes("youtu.be") || lowerUrl.includes("www.youtube.com")) return "youtube";
275
281
 
276
282
  return "unknown";
277
283
  }
@@ -749,58 +755,80 @@ const MediaDownloader = async (url, options = {}) => {
749
755
  throw new Error(`URL not supported: ${blacklisted.reason}`);
750
756
  }
751
757
 
752
- // Verificar se o link está na lista de plataformas suportadas
753
- if (!isVideoLink(url)) {
754
- throw new Error("This URL is not from a supported platform. Supported platforms: Instagram, X(Twitter), TikTok, Facebook");
755
- }
758
+ await deleteTempVideos(); // Clean up previous temp files
756
759
 
757
- // Verificar se é YouTube e lançar erro customizado
758
- const platform = getPlatformType(url);
759
- if (platform === "youtube") {
760
- throw new Error("This URL is not from a supported platform. Supported platforms: Instagram, X(Twitter), TikTok, Facebook");
760
+ if (url.includes('youtube') || url.includes('youtu.be')) {
761
+ let cookies = options.YTBcookies || (options.YTBcookie ? [options.YTBcookie] : []);
762
+
763
+ // Automatically load external cookies if none provided
764
+ if (cookies.length === 0) {
765
+ cookies = loadExternalCookies();
766
+ }
767
+
768
+ if (cookies.length === 0) {
769
+ throw new Error("YouTube download requires a session. Please Provide a valid session in 'session_data.json'.");
770
+ }
771
+
772
+ // Determine if audio-only is requested
773
+ const isAudioOnly = options.outputFormat === 'mp3' || options.downloadAudio === true || config.downloadAudio === true;
774
+
775
+ const videofile = await downloadYoutubeVideo(url, config, cookies, config.YTBmaxduration, isAudioOnly);
776
+ if (videofile) {
777
+ const result = await uploadToGoFileIfNeeded(videofile);
778
+ return result;
779
+ } else {
780
+ throw new Error("URL not supported. Please provide a video URL from a valid platform.");
781
+ }
761
782
  }
762
783
 
763
- await cleanupTempFiles(); // Clean up previous temp files
784
+ else if (url.includes("http")) {
785
+ const blacklisted = blacklistLink(url);
786
+ if (blacklisted) {
787
+ throw new Error(`URL not supported: ${blacklisted.reason}`);
788
+ }
764
789
 
765
- let downloadedFilePath = null;
790
+ // Verificar se o link está na lista de plataformas suportadas
791
+ if (!isVideoLink(url)) {
792
+ throw new Error("This URL is not from a supported platform. Supported platforms: Instagram, X(Twitter), TikTok, Facebook");
793
+ }
794
+
795
+ let downloadedFilePath = null;
766
796
 
767
- try {
768
- // Try primary download method
769
797
  try {
770
- downloadedFilePath = await downloadSmartVideo(url, config);
771
- } catch (error) {
772
- const platform = getPlatformType(url);
798
+ // Try primary download method
799
+ try {
800
+ downloadedFilePath = await downloadSmartVideo(url, config);
801
+ } catch (error) {
802
+ const platform = getPlatformType(url);
773
803
 
774
- // YouTube doesn't use fallback method - it has its own specific implementation
775
- if (platform === "youtube") {
776
- throw new Error(`YouTube download failed: ${error.message}`);
777
- }
804
+ try {
805
+ const fallbackUrl = await tryFallbackDownload(url);
778
806
 
779
- try {
780
- const fallbackUrl = await tryFallbackDownload(url);
807
+ // Validate fallback URL
808
+ const isValid = await validateVideoUrl(fallbackUrl, platform);
809
+ if (!isValid) {
810
+ throw new Error("Fallback URL validation failed");
811
+ }
781
812
 
782
- // Validate fallback URL
783
- const isValid = await validateVideoUrl(fallbackUrl, platform);
784
- if (!isValid) {
785
- throw new Error("Fallback URL validation failed");
813
+ downloadedFilePath = await downloadDirectVideo(fallbackUrl, config);
814
+ } catch (fallbackError) {
815
+ throw new Error(`Failed to download video with both methods: ${fallbackError.message}`);
786
816
  }
787
-
788
- downloadedFilePath = await downloadDirectVideo(fallbackUrl, config);
789
- } catch (fallbackError) {
790
- throw new Error(`Failed to download video with both methods: ${fallbackError.message}`);
791
817
  }
792
- }
793
818
 
794
- if (downloadedFilePath) {
795
- const result = await uploadToGoFileIfNeeded(downloadedFilePath);
796
- return result;
797
- } else {
798
- throw new Error("Failed to obtain downloaded file path.");
819
+ if (downloadedFilePath) {
820
+ const result = await uploadToGoFileIfNeeded(downloadedFilePath);
821
+ return result;
822
+ } else {
823
+ throw new Error("Failed to obtain downloaded file path.");
824
+ }
825
+ } catch (error) {
826
+ // Clean up any remaining temp files on error
827
+ await cleanupTempFiles();
828
+ throw new Error(`Error in downloader videos: ${error.message}`);
799
829
  }
800
- } catch (error) {
801
- // Clean up any remaining temp files on error
802
- await cleanupTempFiles();
803
- throw new Error(`Error in downloader videos: ${error.message}`);
830
+ } else {
831
+ throw new Error("Please specify a video URL from Instagram, facebook or TikTok...");
804
832
  }
805
833
  };
806
834
 
@@ -1169,9 +1197,187 @@ async function rotateVideo(fileName, rotation) {
1169
1197
 
1170
1198
  // Function to extract URL from string
1171
1199
  function extractUrlFromString(text) {
1172
- const urlRegex = /(https?:\/\/[^\s]+)/;
1200
+ const urlRegex = /(https?:\/\/[^\s]+)/g;
1173
1201
  const match = text.match(urlRegex);
1174
- return match ? match[0] : null;
1202
+ return match ? match[0] : text;
1203
+ }
1204
+
1205
+ async function downloadYoutubeVideo(url, config, cookies, YTBmaxduration, isAudioOnly = false) {
1206
+ let lastError = null;
1207
+
1208
+ for (let i = 0; i < Math.min(cookies.length, 6); i++) {
1209
+ const currentCookie = cookies[i];
1210
+ try {
1211
+ const parsedCookies = parseCookies(currentCookie);
1212
+
1213
+ if (parsedCookies.length === 0) {
1214
+ continue;
1215
+ }
1216
+
1217
+ const agent = ytdl.createAgent(parsedCookies);
1218
+ const info = await ytdl.getInfo(url, { agent });
1219
+
1220
+ const durationSeconds = parseInt(info.videoDetails.lengthSeconds, 10);
1221
+ if (durationSeconds > YTBmaxduration) {
1222
+ throw new Error(`❌ The video is longer than ${YTBmaxduration} seconds. Aborting.`);
1223
+ }
1224
+
1225
+ let bestFormat;
1226
+ if (isAudioOnly) {
1227
+ // Filter for audio-only formats
1228
+ const audioFormats = info.formats.filter(f => !f.hasVideo && f.hasAudio);
1229
+ bestFormat = audioFormats.sort((a, b) => (b.audioBitrate || 0) - (a.audioBitrate || 0))[0];
1230
+
1231
+ if (!bestFormat) {
1232
+ // Fallback: any format with audio
1233
+ bestFormat = info.formats.filter(f => f.hasAudio).sort((a, b) => (b.audioBitrate || 0) - (a.audioBitrate || 0))[0];
1234
+ }
1235
+ if (!bestFormat) {
1236
+ throw new Error('❌ No suitable audio format found.');
1237
+ }
1238
+ } else {
1239
+ let formats = info.formats.filter(format => {
1240
+ return format.contentLength && parseInt(format.contentLength) <= 10 * 1024 * 1024 && // ≤ 10 MB
1241
+ format.hasAudio && format.hasVideo;
1242
+ });
1243
+
1244
+ if (formats.length === 0) {
1245
+ formats = info.formats.filter(format => {
1246
+ return format.hasAudio && format.hasVideo;
1247
+ });
1248
+ }
1249
+
1250
+ if (formats.length === 0) {
1251
+ throw new Error('❌ No suitable video format found.');
1252
+ }
1253
+
1254
+ bestFormat = formats.sort((a, b) => b.height - a.height)[0];
1255
+ }
1256
+
1257
+ const ext = isAudioOnly ? 'mp3' : 'mp4';
1258
+ // Create minimal unique file name in output dir
1259
+ let fileName = path.join(OUTPUT_DIR, `v_${uuidv4().slice(0, 4)}.${ext}`);
1260
+
1261
+ const videoStream = ytdl(url, {
1262
+ format: bestFormat,
1263
+ agent
1264
+ });
1265
+
1266
+ const videoWriter = fs.createWriteStream(fileName);
1267
+ videoStream.pipe(videoWriter);
1268
+
1269
+ return await new Promise((resolve, reject) => {
1270
+ videoWriter.on('finish', async () => {
1271
+ try {
1272
+ let finalFilePath = fileName;
1273
+ if (!isAudioOnly) {
1274
+ finalFilePath = await processDownloadedFile(fileName, config, "youtube");
1275
+ }
1276
+ resolve(finalFilePath);
1277
+ } catch (error) {
1278
+ reject(error);
1279
+ }
1280
+ });
1281
+ videoWriter.on('error', (error) => reject(error));
1282
+ });
1283
+
1284
+ } catch (error) {
1285
+ console.error(`Attempt with cookie ${i + 1} failed: ${error.message}`);
1286
+ lastError = error;
1287
+ // If the error is about duration, don't retry with other cookies
1288
+ if (error.message.includes('longer than')) {
1289
+ throw error;
1290
+ }
1291
+ continue; // Try next cookie
1292
+ }
1293
+ }
1294
+
1295
+ throw new Error(`An error occurred while downloading the YouTube video (all ${Math.min(cookies.length, 10)} cookies failed): ${lastError?.message}`);
1296
+ }
1297
+
1298
+ /**
1299
+ * Automatically loads cookies from cookies.json if present
1300
+ * @returns {Array} Array of cookie sets (one or more accounts)
1301
+ */
1302
+ function loadExternalCookies() {
1303
+ const cookiesPath = path.join(process.cwd(), 'session_data.json');
1304
+ if (!fs.existsSync(cookiesPath)) return [];
1305
+
1306
+ try {
1307
+ const fileContent = fs.readFileSync(cookiesPath, 'utf8');
1308
+ const parsed = JSON.parse(fileContent);
1309
+ if (!Array.isArray(parsed)) return [];
1310
+
1311
+ // If it's an array of objects, it's ONE account.
1312
+ // If it's an array of arrays, it's MULTIPLE accounts.
1313
+ if (parsed.length > 0) {
1314
+ if (Array.isArray(parsed[0])) {
1315
+ // Multiple accounts: return them as strings for the rotation logic
1316
+ return parsed.map(acc => JSON.stringify(acc));
1317
+ } else {
1318
+ // Single account: return the whole JSON as a string
1319
+ return [fileContent];
1320
+ }
1321
+ }
1322
+ } catch (e) {
1323
+ console.error("Error loading external cookies:", e.message);
1324
+ }
1325
+ return [];
1326
+ }
1327
+
1328
+ /**
1329
+ * Parses cookies in various formats (JSON, Netscape, or Header String)
1330
+ * @param {string} cookieInput
1331
+ * @returns {Array} Array of cookie objects
1332
+ */
1333
+ function parseCookies(cookieInput) {
1334
+ if (!cookieInput) return [];
1335
+
1336
+ const trimmedInput = cookieInput.trim();
1337
+
1338
+ // 1. Try JSON (EditThisCookie export)
1339
+ try {
1340
+ const parsed = JSON.parse(trimmedInput);
1341
+ if (Array.isArray(parsed)) return parsed;
1342
+ if (typeof parsed === 'object') return [parsed];
1343
+ } catch (e) { }
1344
+
1345
+ // 2. Try Netscape format (tab-separated)
1346
+ if (trimmedInput.includes('\t')) {
1347
+ const lines = trimmedInput.split(/\r?\n/);
1348
+ const cookies = [];
1349
+ for (const line of lines) {
1350
+ if (!line.trim() || line.startsWith('#')) continue;
1351
+ const parts = line.split('\t');
1352
+ if (parts.length >= 7) {
1353
+ cookies.push({
1354
+ domain: parts[0],
1355
+ path: parts[2],
1356
+ secure: parts[3].toUpperCase() === 'TRUE',
1357
+ expirationDate: parseInt(parts[4]),
1358
+ name: parts[5],
1359
+ value: parts[6]
1360
+ });
1361
+ }
1362
+ }
1363
+ if (cookies.length > 0) return cookies;
1364
+ }
1365
+
1366
+ // 3. Fallback: Cookie header string (name=value; name2=value2)
1367
+ return trimmedInput.split(';').map(v => v.split('=')).reduce((acc, v) => {
1368
+ if (v.length >= 2) {
1369
+ const name = v[0].trim();
1370
+ const value = v.slice(1).join('=').trim();
1371
+ if (name && value) {
1372
+ acc.push({
1373
+ name: name,
1374
+ value: value,
1375
+ domain: '.youtube.com'
1376
+ });
1377
+ }
1378
+ }
1379
+ return acc;
1380
+ }, []);
1175
1381
  }
1176
1382
 
1177
1383
  // Function to delete temporary videos
@@ -1179,7 +1385,7 @@ async function deleteTempVideos() {
1179
1385
  try {
1180
1386
  if (!fs.existsSync(TEMP_DIR)) return;
1181
1387
  const files = fs.readdirSync(TEMP_DIR);
1182
- const tempVideoFiles = files.filter(file => /^temp_video.*\.mp4$/.test(file));
1388
+ const tempVideoFiles = files.filter(file => /^temp_video.*\.mp4$/.test(file) || /-player-script\.js$/.test(file));
1183
1389
  for (const file of tempVideoFiles) {
1184
1390
  safeUnlink(path.join(TEMP_DIR, file));
1185
1391
  }
@@ -1200,7 +1406,8 @@ async function cleanupTempFiles() {
1200
1406
  /_audio\.mp3$/.test(file) ||
1201
1407
  /_rotated\.mp4$/.test(file) ||
1202
1408
  /_cropped\.mp4$/.test(file) ||
1203
- /_compressed\.mp4$/.test(file)
1409
+ /_compressed\.mp4$/.test(file) ||
1410
+ /-player-script\.js$/.test(file)
1204
1411
  );
1205
1412
  for (const file of tempFiles) {
1206
1413
  await safeUnlinkWithRetry(path.join(TEMP_DIR, file));
@@ -1440,17 +1647,17 @@ const AudioDownloader = async (url, options = {}) => {
1440
1647
  let audioFilePath = null;
1441
1648
 
1442
1649
  try {
1443
- let platform = getPlatformType(url);
1444
- if (platform === "y124outube") {
1650
+ if (url.includes('youtube') || url.includes('youtu.be')) {
1651
+ let cookies = options.YTBcookies || (options.YTBcookie ? [options.YTBcookie] : []);
1652
+ if (cookies.length === 0) {
1653
+ cookies = loadExternalCookies();
1654
+ }
1445
1655
 
1446
- let fileName = "temp_audio.mp3";
1447
- let count = 1;
1448
- while (fs.existsSync(fileName)) {
1449
- fileName = `temp_audio_${count}.mp3`;
1450
- count++;
1656
+ if (cookies.length === 0) {
1657
+ throw new Error("YouTube download requires a cookie. Please Provide a valid cookie.");
1451
1658
  }
1452
- await downloadYoutubeAudio(url, fileName);
1453
- audioFilePath = fileName;
1659
+
1660
+ audioFilePath = await downloadYoutubeVideo(url, config, cookies, config.YTBmaxduration, true);
1454
1661
  const result = await uploadToGoFileIfNeeded(audioFilePath);
1455
1662
  return result;
1456
1663
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frostpv",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "downloads",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,6 +12,7 @@
12
12
  "author": "Delta",
13
13
  "license": "ISC",
14
14
  "dependencies": {
15
+ "@distube/ytdl-core": "^4.16.12",
15
16
  "@tobyg74/tiktok-api-dl": "^1.3.7",
16
17
  "ab-downloader": "^1.0.1",
17
18
  "axios": "1.12.0",