fix(transcode): handle high-precision FPS using rational fractions

This commit is contained in:
yiling
2026-06-17 22:55:01 +08:00
parent 0be74dd4ab
commit 1ad5720f42
3 changed files with 27 additions and 4 deletions
@@ -145,6 +145,29 @@ describe('TranscodeService', () => {
)
})
it('should handle rational fraction frame rate for video transcoding', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(child_process.execFile as any).mockImplementation((file: string, args: string[], cb: any) => {
cb(null, { stdout: '', stderr: '' })
})
const outputFile = path.join(tempDir, 'output_rational.mp4')
await transcodeService.transcodeVideo({
inputFile: 'input.mp4',
outputFile,
width: 1280,
height: 720,
frameRate: '160000/142512',
disableAudio: true,
})
expect(child_process.execFile).toHaveBeenCalledWith(
'ffmpeg',
expect.arrayContaining(['-filter_complex', expect.stringContaining('fps=160000/142512')]),
expect.any(Function),
)
})
it('should use sharp for image transcoding', async () => {
const outputFile = path.join(tempDir, 'output.webp')
await transcodeService.transcodeImage('input.png', outputFile, 480, 80)
+1 -1
View File
@@ -32,7 +32,7 @@ export interface TranscodeVideoParams {
outputFile: string
width: number
height: number
frameRate?: number
frameRate?: number | string
disableAudio?: boolean
}
@@ -166,7 +166,7 @@ export async function transcodeVideoActivity(
const outputFile = path.join(tmpDir, `video-${params.videoSpec.height}p.mp4`)
try {
let targetFps = 0
let targetFps: number | string = 0
let disableAudio = false
if (params.videoSpec.resolution === '180p') {
@@ -175,8 +175,8 @@ export async function transcodeVideoActivity(
if (params.originalFps > 0 && params.originalFps < 24.0) {
targetFps = params.originalFps
}
if (params.duration * targetFps > 1600) {
targetFps = 1600 / params.duration
if (params.duration * (targetFps as number) > 1600) {
targetFps = `160000/${Math.floor(params.duration * 100)}`
}
}