Skip to content

Commit

Permalink
fix: video thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
sylv committed May 16, 2024
1 parent b340c79 commit 109e536
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 4 additions & 4 deletions packages/api/src/modules/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class StorageService implements OnApplicationBootstrap {
return stream;
}

private getPathFromHash(hash: string) {
public getPathFromHash(hash: string) {
return path.join(config.storagePath, this.getFolderFromHash(hash));
}

Expand All @@ -162,7 +162,7 @@ export class StorageService implements OnApplicationBootstrap {
}
}

@Interval(ms("5s"))
@Interval(1000)
@CreateRequestContext()
@dedupe()
protected async uploadToExternalStorage(): Promise<void> {
Expand Down Expand Up @@ -233,7 +233,7 @@ export class StorageService implements OnApplicationBootstrap {
}
}

@Interval(ms("5s"))
@Interval(1000)
@CreateRequestContext()
@dedupe()
protected async purgeFiles() {
Expand Down Expand Up @@ -273,7 +273,7 @@ export class StorageService implements OnApplicationBootstrap {
if (files[0]) {
this.logger.log(`Purged ${files.length} files`);
} else {
await setTimeout(ms("5m"));
await setTimeout(ms("10m"));
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/api/src/modules/thumbnail/thumbnail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,23 @@ export class ThumbnailService {
}

private async createVideoThumbnail(file: FileEntity) {
if (file.external) throw new Error("Cannot generate thumbnails for external videos.");

const supported = ThumbnailService.VIDEO_TYPES.has(file.type);
if (!supported) throw new Error("Unsupported video type.");

const tempId = randomUUID();
const tempDir = join(tmpdir(), `.thumbnail-workspace-${tempId}`);
const fileStream = await this.storageService.createReadStream(file);
// const fileStream = await this.storageService.createReadStream(file);
const filePath = this.storageService.getPathFromHash(file.hash);
this.log.debug(`Generating video thumbnail at "${tempDir}"`);

// i have no clue why but the internet told me that doing it in multiple invocations is faster
// and it is so whatever. maybe there is a way to do this faster, but this is already pretty fast.
const positions = ["5%", "10%", "20%", "40%"];
const size = `${ThumbnailService.THUMBNAIL_SIZE}x?`;
for (const [positionIndex, percent] of positions.entries()) {
const stream = ffmpeg(fileStream).screenshot({
const stream = ffmpeg(filePath).screenshot({
count: 1,
timemarks: [percent],
folder: tempDir,
Expand Down Expand Up @@ -179,7 +182,7 @@ export class ThumbnailService {
.send(thumbnail.data.unwrap());
}

@Interval(ms("5s"))
@Interval(1000)
@CreateRequestContext()
@dedupe()
protected async generateThumbnail(): Promise<void> {
Expand Down

0 comments on commit 109e536

Please sign in to comment.