Advertisement
YaBoiSwayZ

TikTok Video Downloader (without watermark)

May 26th, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.78 KB | Source Code | 0 0
  1. const tiktok = require('@deiutr/tiktok-dl');
  2. const fs = require('fs').promises;
  3. const axios = require('axios');
  4. const process = require('process');
  5. const cliProgress = require('cli-progress');
  6.  
  7. const METADATA_FILE_PREFIX = 'TikTokVideoMetadata';
  8. const VIDEO_FILE_PREFIX = 'TikTokVideo';
  9. const VIDEO_FILE_EXTENSION = '.mp4';
  10.  
  11. (async () => {
  12.     if (process.argv.length <= 2) {
  13.         console.error('Usage: node downloadTikTok.js <TikTok Video URL>');
  14.         process.exit(-1);
  15.     }
  16.  
  17.     const videoUrl = process.argv[2];
  18.     await downloadVideo(videoUrl);
  19. })();
  20.  
  21. async function downloadVideo(url) {
  22.     try {
  23.         const videoInfo = await getVideoInfo(url);
  24.         const directUrl = videoInfo.videoUrl;
  25.         const videoMetadata = extractMetadata(videoInfo);
  26.  
  27.         await saveVideoMetadata(videoMetadata);
  28.         await downloadAndSaveVideo(directUrl);
  29.     } catch (error) {
  30.         handleError(error);
  31.     }
  32. }
  33.  
  34. async function getVideoInfo(url) {
  35.     const videoInfo = await tiktok.getInfo(url);
  36.     if (!videoInfo || videoInfo.collector.length === 0) {
  37.         throw new Error('Invalid video URL or video not found.');
  38.     }
  39.     return videoInfo.collector[0];
  40. }
  41.  
  42. function extractMetadata(videoInfo) {
  43.     return {
  44.         title: videoInfo.text,
  45.         author: videoInfo.authorMeta.name,
  46.         description: videoInfo.description,
  47.         createTime: videoInfo.createTime,
  48.     };
  49. }
  50.  
  51. async function saveVideoMetadata(metadata) {
  52.     const fileName = `${METADATA_FILE_PREFIX}-${Date.now()}.json`;
  53.     await fs.writeFile(fileName, JSON.stringify(metadata, null, 2));
  54. }
  55.  
  56. async function downloadAndSaveVideo(url) {
  57.     const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
  58.     progressBar.start(100, 0);
  59.  
  60.     console.log('Downloading video...');
  61.     const response = await axios.get(url, { responseType: 'stream' });
  62.     const totalLength = response.headers['content-length'];
  63.  
  64.     await saveVideoFile(response.data, totalLength, progressBar);
  65. }
  66.  
  67. async function saveVideoFile(stream, totalLength, progressBar) {
  68.     const outputFileName = `${VIDEO_FILE_PREFIX}-${Date.now()}${VIDEO_FILE_EXTENSION}`;
  69.     const writer = fs.createWriteStream(outputFileName);
  70.  
  71.     stream.on('data', (chunk) => {
  72.         progressBar.increment((chunk.length * 100) / totalLength);
  73.     });
  74.  
  75.     stream.pipe(writer);
  76.  
  77.     return new Promise((resolve, reject) => {
  78.         writer.on('finish', () => {
  79.             progressBar.stop();
  80.             console.log(`Download complete. File saved as ${outputFileName}`);
  81.             resolve();
  82.         });
  83.  
  84.         writer.on('error', (error) => {
  85.             progressBar.stop();
  86.             reject(error);
  87.         });
  88.     });
  89. }
  90.  
  91. function handleError(error) {
  92.     console.error('Error:', error.message);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement