Skip to content

Commit

Permalink
Fix transcoding downloaded tracks to mp3
Browse files Browse the repository at this point in the history
  • Loading branch information
nukeop committed Aug 4, 2024
1 parent 56be6aa commit 48fba60
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
12 changes: 5 additions & 7 deletions packages/main/src/controllers/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NuclearMeta, IpcEvents } from '@nuclear/core';
import { IpcMessageEvent, DownloadItem } from 'electron';
import { inject } from 'inversify';
import prism from 'prism-media';
import { createWriteStream } from 'fs';
import { createWriteStream, createReadStream } from 'fs';
import { rm } from 'fs/promises';

import { ipcController, ipcEvent } from '../utils/decorators';
Expand Down Expand Up @@ -85,19 +85,17 @@ class DownloadIpcCtrl {
onCompleted: (file) => {
this.window?.send(IpcEvents.DOWNLOAD_FINISHED, uuid);
this.logger.log(`Download success: ${artistName} - ${title}, path: ${file.path}`);
this.logger.log('');
this.downloadItems = this.downloadItems.filter((item) => item.uuid !== uuid);

const inputStream = createReadStream(file.path);
const outputFilename = file.path.replace(/\.[^.]+$/, '.mp3');
const transcoder = new prism.FFmpeg({args: [
'-i', file.path,
'-f', 'mp3',
'-vn',
'-q:a', '2',
'-ac', '2',
'-ar', '48000',
'-y',
'-',
outputFilename
'-y'
]});

transcoder
Expand All @@ -110,7 +108,7 @@ class DownloadIpcCtrl {
this.logger.log(`Removed after conversion: ${file.path}`);
});

transcoder.pipe(createWriteStream(outputFilename));
inputStream.pipe(transcoder).pipe(createWriteStream(outputFilename));
}
});
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/services/download/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Download {
}

/**
* Download a son using Youtube
* Download a track using Youtube
*/
async start({
query,
Expand Down
3 changes: 2 additions & 1 deletion packages/main/src/services/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const errorLogStream = rts.createStream(
path.join(app.getPath('userData'), 'logs', 'nuclear-error.log'),
{
size: '5M',
compress: 'gzip'
compress: 'gzip',
rotate: 5
}
);

Expand Down
6 changes: 3 additions & 3 deletions packages/main/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ module.exports = (env: BuildEnv): webpack.Configuration => {
resolve: {
extensions: ['.ts', '.js', '.json', '.node'],
alias: {
jsbi: path.resolve(__dirname, '..', '..', 'node_modules', 'jsbi', 'dist', 'jsbi-cjs.js'),
...(IS_PROD ? {'@nuclear/scanner': 'scanner.node' } : {})
jsbi: path.resolve(__dirname, '..', '..', 'node_modules', 'jsbi', 'dist', 'jsbi-cjs.js')
},
fallback: {
fs: false
Expand All @@ -42,7 +41,8 @@ module.exports = (env: BuildEnv): webpack.Configuration => {
},
externals: {
'sqlite3': 'commonjs sqlite3',
'@nuclear/scanner': 'commonjs ./scanner.node'
'@nuclear/scanner': 'commonjs ./scanner.node',
'ffmpeg-static': 'commonjs ffmpeg-static'
},
output: {
path: path.resolve(__dirname, outputDir),
Expand Down

0 comments on commit 48fba60

Please sign in to comment.