diff options
| author | Max Isom <[email protected]> | 2020-03-13 20:36:42 -0500 |
|---|---|---|
| committer | Max Isom <[email protected]> | 2020-03-13 20:36:42 -0500 |
| commit | fb91c8e89cb34465315ac3c9f4f11e27ec577348 (patch) | |
| tree | 66fef9fdceaee601d02f8f13eb91e47292c59546 /src/utils | |
| parent | c446e0fd57cec0ea2fb0211bd99841e5ddde0cf6 (diff) | |
| download | muse-fb91c8e89cb34465315ac3c9f4f11e27ec577348.tar.xz muse-fb91c8e89cb34465315ac3c9f4f11e27ec577348.zip | |
Add better caching, seek command
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/get-youtube-stream.ts | 78 |
1 files changed, 0 insertions, 78 deletions
diff --git a/src/utils/get-youtube-stream.ts b/src/utils/get-youtube-stream.ts deleted file mode 100644 index 1360dce..0000000 --- a/src/utils/get-youtube-stream.ts +++ /dev/null @@ -1,78 +0,0 @@ -import {promises as fs, createReadStream, createWriteStream} from 'fs'; -import {Readable, PassThrough} from 'stream'; -import path from 'path'; -import hasha from 'hasha'; -import ytdl from 'ytdl-core'; -import prism from 'prism-media'; -import {CACHE_DIR} from './config'; - -const nextBestFormat = (formats: ytdl.videoFormat[]): ytdl.videoFormat => { - formats = formats - .filter(format => format.averageBitrate) - .sort((a, b) => b.averageBitrate - a.averageBitrate); - return formats.find(format => !format.bitrate) ?? formats[0]; -}; - -// TODO: are some videos not available in webm/opus? -export default async (url: string): Promise<Readable> => { - const hash = hasha(url); - const cachedPath = path.join(CACHE_DIR, `${hash}.webm`); - - const info = await ytdl.getInfo(url); - - const {formats} = info; - - const filter = (format: ytdl.videoFormat): boolean => format.codecs === 'opus' && format.container === 'webm' && format.audioSampleRate !== undefined && parseInt(format.audioSampleRate, 10) === 48000; - - let format = formats.find(filter); - let canDirectPlay = true; - - if (!format) { - format = nextBestFormat(info.formats); - canDirectPlay = false; - } - - try { - // Test if file exists - await fs.access(cachedPath); - - // If so, return cached stream - return createReadStream(cachedPath); - } catch (_) { - // Not yet cached, must download - const cacheTempPath = path.join('/tmp', `${hash}.webm`); - const cacheStream = createWriteStream(cacheTempPath); - - const pass = new PassThrough(); - - pass.pipe(cacheStream).on('finish', async () => { - await fs.rename(cacheTempPath, cachedPath); - }); - - if (canDirectPlay) { - return ytdl.downloadFromInfo(info, {format}).pipe(pass); - } - - const transcoder = new prism.FFmpeg({ - args: [ - '-reconnect', - '1', - '-reconnect_streamed', - '1', - '-reconnect_delay_max', - '5', - '-i', - format.url, - '-loglevel', - 'verbose', - '-vn', - '-acodec', - 'libopus', - '-f', - 'webm' - ] - }); - - return transcoder.pipe(pass); - } -}; |
