diff --git a/Metadata.ts b/Metadata.ts index 20d199d..5c21a4d 100644 --- a/Metadata.ts +++ b/Metadata.ts @@ -1,8 +1,7 @@ import axios from 'axios'; import { terminal as term } from 'terminal-kit'; import { Metadata, Session } from './Types'; -import fs from 'fs'; -import os from 'os'; +import { drawThumbnail } from './Thumbnail'; export async function getVideoMetadata(videoGuids: string[], session: Session): Promise { @@ -28,7 +27,7 @@ export async function getVideoMetadata(videoGuids: string[], session: Session): .map((item: { [x: string]: string }) => { return item["playbackUrl"]; })[0]; posterImage = response.data["posterImage"]["medium"]["url"]; - + term.brightMagenta(`\n title = ${title}\n playbackUrl = ${playbackUrl}\n`); metadata.push({ diff --git a/Thumbnail.ts b/Thumbnail.ts index a9d0a63..1e6003d 100644 --- a/Thumbnail.ts +++ b/Thumbnail.ts @@ -1,27 +1,16 @@ -import os from 'os'; import { terminal as term } from 'terminal-kit'; +import { execSync } from 'child_process'; -if (os.platform() !== "win32") { - term.brightWhite("Platform is not Windows, let's draw some thumbnails!\n"); - let a = async () => { - response = await axios.get(posterImageUrl, { - headers: { - Authorization: `Bearer ${session.AccessToken}` - }, - responseType: 'stream' - }); - - const writer = fs.createWriteStream('.thumbnail.png'); - response.data.pipe(writer); - - return new Promise((resolve, reject) => { - writer.on('finish', resolve); - writer.on('error', reject); - }); - - }; - await a(); +export function drawThumbnail(posterImage: string, accessToken: string): void { + let fetchCmd = `ffmpeg -hide_banner -loglevel warning ` + + `-headers "Authorization: Bearer ${accessToken}\r\n" ` + + `-i "${posterImage}" -y .thumbnail.png`; + execSync(fetchCmd, { stdio: 'inherit' }); + try { + term.drawImage('.thumbnail.png', { shrink: { width: 50, height: 50 } }); + } + catch (e) { + console.error(e); + } } - -term.drawImage('.thumbnail.png', { shrink: { width: 50, height: 50 } }); \ No newline at end of file diff --git a/destreamer.ts b/destreamer.ts index 7e16315..cec333e 100644 --- a/destreamer.ts +++ b/destreamer.ts @@ -2,6 +2,7 @@ import { BrowserTests } from './Tests'; import { TokenCache } from './TokenCache'; import { getVideoMetadata } from './Metadata'; import { Metadata, Session } from './Types'; +import { drawThumbnail } from './Thumbnail'; import { execSync } from 'child_process'; import puppeteer from 'puppeteer'; @@ -171,9 +172,13 @@ async function downloadVideo(videoUrls: string[], outputDirectory: string, sessi console.log("Fetching title and HLS URL..."); let metadata: Metadata[] = await getVideoMetadata(videoGuids, session); - metadata.forEach(video => { + await Promise.all(metadata.map(async video => { video.title = sanitize(video.title); term.blue(`\nDownloading Video: ${video.title}\n`); + + drawThumbnail(video.posterImage, session.AccessToken); + await sleep(100); // there's something wrong with drawThumbnail we should not need this + console.log('Spawning youtube-dl with cookie and HLS URL...'); const format = argv.format ? `-f "${argv.format}"` : ""; var youtubedlCmd = 'youtube-dl --no-call-home --no-warnings ' + format + @@ -185,7 +190,7 @@ async function downloadVideo(videoUrls: string[], outputDirectory: string, sessi } execSync(youtubedlCmd, { stdio: 'inherit' }); - }); + })); }