Skip to content

Instantly share code, notes, and snippets.

@codersidprogrammer
Created May 12, 2023 08:12
Show Gist options
  • Save codersidprogrammer/a6ee7c650021d77d0520f84bf7a8b297 to your computer and use it in GitHub Desktop.
Save codersidprogrammer/a6ee7c650021d77d0520f84bf7a8b297 to your computer and use it in GitHub Desktop.
import { Injectable } from '@nestjs/common';
import ffmpeg from 'fluent-ffmpeg';
@Injectable()
export class VideoSplitService {
splitVideo(inputPath: string, outputPath: string): void {
ffmpeg(inputPath, {
timeout: 4320000,
})
.addOption([
'-profile:v baseline',
'-level 3.0',
'-start_number 0',
'-hls_time 10',
'-hls_list_size 0',
'-f hls',
])
.output(outputPath)
.on('end', () => console.log('success'))
.run();
}
}
import { Module } from '@nestjs/common';
import { VideoSplitService } from './services/video-split.service';
import { VideoProcessor } from './services/video.processor';
import { VideoService } from './services/video.service';
@Module({
providers: [VideoSplitService, VideoProcessor, VideoService],
exports: [VideoService],
})
export class VideoModule {}
import { Process, Processor } from '@nestjs/bull';
import { Job } from 'bull';
import { VideoSplitService } from './video-split.service';
import { VideoDto } from './video.service';
@Processor('video-encoder')
export class VideoProcessor {
constructor(private readonly videoSplitService: VideoSplitService) {}
@Process('360')
handleVideoSplit(job: Job<VideoDto>): void {
this.videoSplitService.splitVideo(job.data.filePath, job.data.outputPath);
}
}
import { InjectQueue } from '@nestjs/bull';
import { Injectable } from '@nestjs/common';
import { Queue } from 'bull';
export interface VideoDto {
filePath: string;
outputPath: string;
}
@Injectable()
export class VideoService {
constructor(
@InjectQueue('video-encoder')
private readonly queue: Queue,
) {}
async registerVideoEncoding(
queueName: string,
data: VideoDto,
): Promise<void> {
await this.queue.add(queueName, data, {
delay: 100,
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment