diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ff2e8ac..62c1359d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,11 +15,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). - **webui**: Only show the center play button and darkened tint on initial video open before playback starts; once played, pausing or scrubbing back to start keeps the video canvas clean and unobstructed for review - **transcode**: Fix an issue where videos with orientation or rotation metadata (such as smartphone vertical videos) were incorrectly displayed with stretched aspect ratios in the video player -- **docker**: Fix an issue where the bundled PostgreSQL container published port 5432 on the host, causing deployment to fail on machines where that port was already in use; the database is now only reachable by Shumai itself - **cli**: Only manage and check PID files in daemon mode, preventing stale PID files, crash-loop conflicts, or spurious process checks during normal foreground and Docker execution ### Changed +- **transcode**: Improve video proxy visual quality and disable B-frames for smoother frame-by-frame seeking and scrubbing + + ## [0.4.7] - 2026-09-19 ### Added diff --git a/packages/core/src/transcode/transcode.test.ts b/packages/core/src/transcode/transcode.test.ts index 9be66423..59598ed0 100644 --- a/packages/core/src/transcode/transcode.test.ts +++ b/packages/core/src/transcode/transcode.test.ts @@ -1312,7 +1312,7 @@ describe('TranscodeService', () => { const encoder = await transcodeService.selectH264Encoder('auto', 'darwin') expect(encoder.name).toBe('h264_videotoolbox') - expect(encoder.presetArgs).toEqual(['-q:v', '74']) + expect(encoder.presetArgs).toEqual([]) }) it('selectH264Encoder should select h264_nvenc on linux when available', async () => { @@ -1448,10 +1448,10 @@ describe('TranscodeService', () => { const encoder = await transcodeService.selectH264Encoder('auto', 'linux') expect(encoder.name).toBe('libx264') - expect(encoder.presetArgs).toEqual(['-preset', 'fast', '-crf', '26']) + expect(encoder.presetArgs).toEqual(['-preset', 'fast', '-crf', '23', '-bf', '0']) }) - it('transcodeVideo with hardwareAcceleration off should use libx264, preset fast, crf 26, maxrate, and yuv420p', async () => { + it('transcodeVideo with hardwareAcceleration off should use libx264, preset fast, crf 23, -bf 0, maxrate, and yuv420p', async () => { vi.mocked(execFile).mockImplementation( ( _cmd: unknown, @@ -1487,7 +1487,9 @@ describe('TranscodeService', () => { '-preset', 'fast', '-crf', - '26', + '23', + '-bf', + '0', '-maxrate', '720k', '-bufsize', @@ -1499,7 +1501,7 @@ describe('TranscodeService', () => { ) }) - it('transcodeVideo with hardwareAcceleration auto and videotoolbox should use -q:v 74 and -maxrate', async () => { + it('transcodeVideo with hardwareAcceleration auto and videotoolbox should use -b:v and no maxrate', async () => { vi.spyOn(transcodeService, 'selectH264Encoder').mockResolvedValue( H264_ENCODER_CONFIGS.h264_videotoolbox, ) @@ -1536,17 +1538,16 @@ describe('TranscodeService', () => { expect.arrayContaining([ '-c:v', 'h264_videotoolbox', - '-q:v', - '74', - '-maxrate', + '-b:v', '720k', - '-bufsize', - '1440k', '-pix_fmt', 'yuv420p', ]), expect.any(Function), ) + const callArgs = vi.mocked(child_process.execFile).mock.calls[0][1] as string[] + expect(callArgs).not.toContain('-maxrate') + expect(callArgs).not.toContain('-bufsize') }) it('transcodeVideo with hardwareAcceleration auto and nvenc should use -preset p4, -rc:v vbr, -cq:v 26, -b:v 0, and -maxrate', async () => { @@ -1695,7 +1696,9 @@ describe('TranscodeService', () => { '-preset', 'fast', '-crf', - '26', + '23', + '-bf', + '0', '-b:v', '1500k', '-pix_fmt', diff --git a/packages/core/src/transcode/transcode.ts b/packages/core/src/transcode/transcode.ts index 0a2e2b6e..3a20d6d5 100644 --- a/packages/core/src/transcode/transcode.ts +++ b/packages/core/src/transcode/transcode.ts @@ -156,11 +156,11 @@ export const H264_ENCODER_CONFIGS: Record = { }, h264_videotoolbox: { name: 'h264_videotoolbox', - presetArgs: ['-q:v', '74'], + presetArgs: [], }, libx264: { name: 'libx264', - presetArgs: ['-preset', 'fast', '-crf', '26'], + presetArgs: ['-preset', 'fast', '-crf', '23', '-bf', '0'], }, } @@ -196,12 +196,12 @@ export function getDefaultBitrate(height: number, width?: number): string { return `${Math.round(bps / 1000)}k` } -export function calculateMaxBitrate( +export function calculateEffectiveBitrateBps( targetHeight: number, targetWidth?: number, sourceVideoBitrate?: number, targetFps?: number | string, -): { maxrate: string; bufsize: string } { +): number { let configuredMaxBps = getDefaultBitrateBps(targetHeight, targetWidth) // If frame rate is downsampled (e.g. 180p preview with < 24 fps), scale the bitrate ceiling proportionally @@ -223,13 +223,27 @@ export function calculateMaxBitrate( } } - let effectiveMaxBps = + const effectiveMaxBps = sourceVideoBitrate && sourceVideoBitrate > 0 ? Math.min(configuredMaxBps, Math.round(sourceVideoBitrate * 1.2)) : configuredMaxBps const minFloor = targetFps ? 50_000 : 100_000 - effectiveMaxBps = Math.max(minFloor, effectiveMaxBps) + return Math.max(minFloor, effectiveMaxBps) +} + +export function calculateMaxBitrate( + targetHeight: number, + targetWidth?: number, + sourceVideoBitrate?: number, + targetFps?: number | string, +): { maxrate: string; bufsize: string } { + const effectiveMaxBps = calculateEffectiveBitrateBps( + targetHeight, + targetWidth, + sourceVideoBitrate, + targetFps, + ) const maxrateKbps = Math.max(50, Math.round(effectiveMaxBps / 1000)) const bufsizeKbps = maxrateKbps * 2 @@ -780,16 +794,31 @@ export class TranscodeService { args.push(...encoder.presetArgs) } - if (params.videoBitrate) { - args.push('-b:v', params.videoBitrate) + if (encoder.name === 'h264_videotoolbox') { + if (params.videoBitrate) { + args.push('-b:v', params.videoBitrate) + } else { + const targetBps = calculateEffectiveBitrateBps( + params.height, + params.width, + params.sourceVideoBitrate, + params.frameRate, + ) + const targetKbps = Math.max(50, Math.round(targetBps / 1000)) + args.push('-b:v', `${targetKbps}k`) + } } else { - const { maxrate, bufsize } = calculateMaxBitrate( - params.height, - params.width, - params.sourceVideoBitrate, - params.frameRate, - ) - args.push('-maxrate', maxrate, '-bufsize', bufsize) + if (params.videoBitrate) { + args.push('-b:v', params.videoBitrate) + } else { + const { maxrate, bufsize } = calculateMaxBitrate( + params.height, + params.width, + params.sourceVideoBitrate, + params.frameRate, + ) + args.push('-maxrate', maxrate, '-bufsize', bufsize) + } } args.push('-pix_fmt', 'yuv420p')