Browse Source

Update cache naming

Use appropriate naming for files downloaded via yt-dlp.
master
TheoryOfNekomata 1 year ago
parent
commit
97bcf351e3
4 changed files with 30 additions and 1 deletions
  1. +3
    -1
      src/video-types/youtube/clipper.ts
  2. +6
    -0
      src/video-types/youtube/downloader.ts
  3. +2
    -0
      src/video-types/youtube/errors.ts
  4. +19
    -0
      src/video-types/youtube/url.ts

+ 3
- 1
src/video-types/youtube/clipper.ts View File

@@ -8,6 +8,7 @@ import {
import { VIDEO_TYPE } from './common';
import { constructDefaultDownloadArgs, getFileExtension } from './downloader';
import { DownloaderFailedToStartError, DownloaderNotFoundError } from './errors';
import {retrieveVideoId} from './url';

export interface CreateClipperParams extends CreateBaseClipperParams {
type: typeof VIDEO_TYPE;
@@ -24,7 +25,8 @@ export const createVideoClipper = (createClipperParams: Omit<CreateClipperParams
createClipperParams.downloaderExecutablePath,
clipVideoParams.url,
);
const cacheFilename = `output.${fileExtension}`; // todo label this on the cache
const videoId = retrieveVideoId(clipVideoParams.url);
const cacheFilename = `output.${encodeURIComponent(videoId)}.${fileExtension}`;
const downloadArgs = constructDefaultDownloadArgs(
cacheFilename,
clipVideoParams.url,


+ 6
- 0
src/video-types/youtube/downloader.ts View File

@@ -51,6 +51,12 @@ export const getFileExtension = (downloaderExecutablePath: string, url: string)
throw result.error;
}

const errorMessage = result.stderr.toString('utf-8').trim();

if (errorMessage.length > 0) {
throw new Error(errorMessage);
}

return result.stdout.toString('utf-8').trim();
};



+ 2
- 0
src/video-types/youtube/errors.ts View File

@@ -1,3 +1,5 @@
export class DownloaderNotFoundError extends Error {}

export class DownloaderFailedToStartError extends Error {}

export class InvalidVideoIdError extends Error {}

+ 19
- 0
src/video-types/youtube/url.ts View File

@@ -0,0 +1,19 @@
import { InvalidVideoIdError } from './errors';

const STANDARD_YOUTUBE_VIDEO_ID_LENGTH = 11 as const;

export const RE_YOUTUBE = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v?:i?=|&v?:i?=))([^#&?]*).*/im;

export const retrieveVideoId = (videoId: string): string => {
if (typeof (videoId as unknown) !== 'string') {
throw new InvalidVideoIdError('The video ID must be a string.');
}
if (videoId.length === STANDARD_YOUTUBE_VIDEO_ID_LENGTH) {
return videoId;
}
const matchId = videoId.match(RE_YOUTUBE);
if (matchId && matchId.length > 1) {
return matchId[1];
}
throw new InvalidVideoIdError('Impossible to retrieve Youtube video ID.');
};

Loading…
Cancel
Save