Skip to content

Commit

Permalink
Merge branch 'refs/heads/优化-convert'
Browse files Browse the repository at this point in the history
  • Loading branch information
solidSpoon committed May 25, 2024
2 parents ef72ff7 + e20d37e commit 8d507c5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/backend/services/ConvertService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ export default class ConvertService {
const srtName = mp4File.replace('.mp4', '.srt');
if (!fs.existsSync(srtName)) {
try {
// await FfmpegService.extractSubtitles({
// inputFile: file,
// outputFile: srtName,
// });
await FfmpegService.extractSubtitles({
taskId,
inputFile: file,
onProgress
});
} catch (e) {
console.error('提取字幕失败', e);
}
Expand Down
61 changes: 41 additions & 20 deletions src/backend/services/FfmpegService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ export default class FfmpegService {
/**
* mkv转mp4
* ffmpeg -i "vid.mkv" -map 0 -c copy -c:a aac "MP4/vid.mp4"
* ffmpeg -i output.mkv -map 0:v -map 0:a -c:v copy -c:a aac -ac 1 output.mp4
*/
public static async mkvToMp4({
taskId,
Expand All @@ -290,9 +291,11 @@ export default class FfmpegService {
await new Promise((resolve, reject) => {
const command = ffmpeg(inputFile)
.outputOptions([
'-map', '0',
'-c', 'copy',
'-c:a', 'aac'
'-map', '0:v',
'-map', '0:a',
'-c:v', 'copy',
'-c:a', 'aac',
'-ac', '1'
])
.output(output)
.on('progress', (progress) => {
Expand All @@ -314,28 +317,46 @@ export default class FfmpegService {
return output;
}

/**
* 提取字幕
* ffmpeg -i "vid.mkv" -map 0:s:m:language:eng? -map 0:s:m:language:und? -c:s srt "vid.srt"
*/
public static async extractSubtitles({
taskId,
inputFile,
outputFile,
language = 'eng'
onProgress
}: {
taskId: number,
inputFile: string,
outputFile: string,
language?: string
}): Promise<void> {
onProgress?: (progress: number) => void
}): Promise<string> {
const outputSubtitle = inputFile.replace(path.extname(inputFile), '.srt');
await Lock.sync('ffmpeg', async () => {
await new Promise((resolve, reject) => {
ffmpeg(inputFile)
.outputOptions([
'-map', `0:s:m:language:${language}?`,
'-c:s', 'srt'
])
.output(outputFile)
.on('end', resolve)
.on('error', reject)
.run();
});
});
await new Promise((resolve, reject) => {
const command = ffmpeg(inputFile)
.outputOptions([
'-map', '0:s:m:language:eng?', // 优先选择英文字幕
'-map', '0:s:m:language:und?', // 如果没有英文字幕则选择默认字幕
'-c:s', 'srt' // 输出格式为 srt
])
.output(outputSubtitle)
.on('progress', (progress) => {
if (progress.percent) {
console.log('progress', progress.percent);
if (onProgress) {
onProgress(progress.percent);
}
}
})
.on('end', resolve)
.on('error', reject);
ProcessService.registerFfmpeg(taskId, [command]);
command
.run();
});
}
);
return outputSubtitle;
}
}

0 comments on commit 8d507c5

Please sign in to comment.