configurable audio formats

This commit is contained in:
raiper34
2024-07-20 15:34:02 +02:00
parent da78603db2
commit ac038245e2
5 changed files with 1489 additions and 1263 deletions
+1467 -1251
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "spooty", "name": "spooty",
"workspaces": ["src/*"], "workspaces": ["src/backend", "src/frontend"],
"scripts": { "scripts": {
"start:be": "npm run start:dev -w backend", "start:be": "npm run start:dev -w backend",
"start:fe": "npm run start -w frontend", "start:fe": "npm run start -w frontend",
+2
View File
@@ -30,7 +30,9 @@
"@nestjs/serve-static": "^4.0.2", "@nestjs/serve-static": "^4.0.2",
"@nestjs/typeorm": "^10.0.2", "@nestjs/typeorm": "^10.0.2",
"@nestjs/websockets": "^10.3.10", "@nestjs/websockets": "^10.3.10",
"@types/fluent-ffmpeg": "^2.1.24",
"@types/yt-search": "^2.10.3", "@types/yt-search": "^2.10.3",
"fluent-ffmpeg": "^2.1.3",
"isomorphic-unfetch": "^4.0.2", "isomorphic-unfetch": "^4.0.2",
"reflect-metadata": "^0.2.0", "reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1", "rxjs": "^7.8.1",
+4 -3
View File
@@ -26,9 +26,10 @@ export class TrackController {
@Get('download/:id') @Get('download/:id')
async getFile(@Res({ passthrough: true }) res: Response, @Param('id') id: number): Promise<StreamableFile> { async getFile(@Res({ passthrough: true }) res: Response, @Param('id') id: number): Promise<StreamableFile> {
const track = await this.service.findOne(id); const track = await this.service.findOne(id);
const file = createReadStream(resolve(__dirname, '..', this.configService.get<string>('DOWNLOADS'), `${track.artist} - ${track.song}.mp3`)); const fileName = `${track.artist} - ${track.song}.${this.configService.get<string>('FORMAT')}`;
res.set({'Content-Disposition': `attachment; filename="${track.artist} - ${track.song}.mp3"`,}); const filePath = createReadStream(resolve(__dirname, '..', this.configService.get<string>('DOWNLOADS'), fileName));
return new StreamableFile(file); res.set({'Content-Disposition': `attachment; filename="${fileName}`});
return new StreamableFile(filePath);
} }
@Delete(':id') @Delete(':id')
+15 -8
View File
@@ -13,6 +13,7 @@ import {ConfigService} from "@nestjs/config";
import {resolve} from "path"; import {resolve} from "path";
import {WebSocketGateway, WebSocketServer} from "@nestjs/websockets"; import {WebSocketGateway, WebSocketServer} from "@nestjs/websockets";
import {Server} from "socket.io"; import {Server} from "socket.io";
import * as ffmpeg from 'fluent-ffmpeg';
@WebSocketGateway() @WebSocketGateway()
@Injectable() @Injectable()
@@ -102,13 +103,19 @@ export class TrackService {
} }
private youtubeDownload(track: TrackEntity): Promise<void> { private youtubeDownload(track: TrackEntity): Promise<void> {
return new Promise((res, reject) => return new Promise((res, reject) => {
ytdl(track.youtubeUrl, {quality: "highestaudio", filter: "audioonly"}) const audio = ytdl(track.youtubeUrl, {quality: "highestaudio", filter: "audioonly"})
.on('error', (err) => reject(err)).pipe( .on('error', (err) => reject(err));
fs.createWriteStream(resolve(__dirname, '..', this.configService.get<string>('DOWNLOADS'), `${track.artist} - ${track.song.replace('/', '')}.mp3`)) ffmpeg(audio).format(this.configService.get<string>('FORMAT'))
.on('finish', () => res()) .on('error', (err) => reject(err))
.on('error', (err) => reject(err)) .pipe(
) fs.createWriteStream(
); resolve(
__dirname, '..', this.configService.get<string>('DOWNLOADS'),
`${track.artist} - ${track.song.replace('/', '')}.${this.configService.get<string>('FORMAT')}`
)
).on('finish', () => res()).on('error', (err) => reject(err))
);
});
} }
} }