mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 04:14:30 +00:00
feat(agent): support 3D degree comments, markup annotations, and config3d in read_asset (#473)
* feat(agent): support AI embedding, autofill, and read_asset for 3D assets * feat(agent): support 3D degree comments, markup annotations, and config3d in read_asset * fix(agent): resolve 3D session annotations to turntable degree frame time
This commit is contained in:
@@ -32,4 +32,5 @@ export * from './media-generation/media-generation'
|
||||
export * from './sandbox'
|
||||
export * from './asset/comments-export'
|
||||
export * from './turntable/turntable'
|
||||
export * from './utils/model3d'
|
||||
export * from './integrations/helper'
|
||||
|
||||
@@ -4432,6 +4432,89 @@ describe('TranscodeService', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('generate3dViews', () => {
|
||||
it('executes ffmpeg with 2x2 grid filter, libwebp encoder, and q=85', async () => {
|
||||
let executedArgs: string[] = []
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(child_process.execFile as any).mockImplementation(
|
||||
(
|
||||
_cmd: string,
|
||||
args: string[],
|
||||
cb: (err: unknown, result: { stdout: string; stderr: string }) => void,
|
||||
) => {
|
||||
executedArgs = args
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
},
|
||||
)
|
||||
|
||||
const inputPath = path.join(tempDir, 'turntable.mp4')
|
||||
const outputPath = path.join(tempDir, 'views.webp')
|
||||
|
||||
await transcodeService.generate3dViews(inputPath, outputPath)
|
||||
|
||||
expect(child_process.execFile).toHaveBeenCalledWith(
|
||||
'ffmpeg',
|
||||
expect.any(Array),
|
||||
expect.any(Function),
|
||||
)
|
||||
expect(executedArgs).toEqual([
|
||||
'-y',
|
||||
'-loglevel',
|
||||
'warning',
|
||||
'-i',
|
||||
inputPath,
|
||||
'-filter_complex',
|
||||
"[0:v]select='eq(n\\,0)+eq(n\\,6)+eq(n\\,12)+eq(n\\,18)',scale=1440:1440,tile=2x2[views_out]",
|
||||
'-map',
|
||||
'[views_out]',
|
||||
'-frames:v',
|
||||
'1',
|
||||
'-c:v',
|
||||
'libwebp',
|
||||
'-q:v',
|
||||
'85',
|
||||
outputPath,
|
||||
])
|
||||
})
|
||||
|
||||
it('passes abort signal to execFile when provided', async () => {
|
||||
const controller = new AbortController()
|
||||
let optionsPassed: unknown = null
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(child_process.execFile as any).mockImplementation(
|
||||
(_cmd: string, _args: string[], optionsOrCb: unknown, maybeCb?: unknown) => {
|
||||
const cb = (typeof optionsOrCb === 'function' ? optionsOrCb : maybeCb) as (
|
||||
err: unknown,
|
||||
result: { stdout: string; stderr: string },
|
||||
) => void
|
||||
if (typeof optionsOrCb !== 'function') {
|
||||
optionsPassed = optionsOrCb
|
||||
}
|
||||
cb(null, { stdout: '', stderr: '' })
|
||||
},
|
||||
)
|
||||
|
||||
const inputPath = path.join(tempDir, 'turntable.mp4')
|
||||
const outputPath = path.join(tempDir, 'views.webp')
|
||||
|
||||
await transcodeService.generate3dViews(inputPath, outputPath, controller.signal)
|
||||
|
||||
expect(optionsPassed).toMatchObject({ signal: controller.signal })
|
||||
})
|
||||
|
||||
it('rejects with cancellation error if aborted before execution', async () => {
|
||||
const controller = new AbortController()
|
||||
controller.abort()
|
||||
|
||||
const inputPath = path.join(tempDir, 'aborted.mp4')
|
||||
const outputPath = path.join(tempDir, 'views.webp')
|
||||
|
||||
await expect(
|
||||
transcodeService.generate3dViews(inputPath, outputPath, controller.signal),
|
||||
).rejects.toThrow('3D views generation cancelled')
|
||||
})
|
||||
})
|
||||
|
||||
describe('HLS utilities', () => {
|
||||
it('builds master playlist correctly with bandwidth and codecs', () => {
|
||||
const playlist = buildHlsMasterPlaylist([
|
||||
|
||||
@@ -3192,6 +3192,37 @@ export class TranscodeService {
|
||||
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args])
|
||||
}
|
||||
}
|
||||
|
||||
async generate3dViews(
|
||||
inputFile: string,
|
||||
outputViews: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<void> {
|
||||
if (signal?.aborted) {
|
||||
throw new Error('3D views generation cancelled')
|
||||
}
|
||||
const filterComplex = `[0:v]select='eq(n\\,0)+eq(n\\,6)+eq(n\\,12)+eq(n\\,18)',scale=1440:1440,tile=2x2[views_out]`
|
||||
const args = [
|
||||
'-i',
|
||||
inputFile,
|
||||
'-filter_complex',
|
||||
filterComplex,
|
||||
'-map',
|
||||
'[views_out]',
|
||||
'-frames:v',
|
||||
'1',
|
||||
'-c:v',
|
||||
'libwebp',
|
||||
'-q:v',
|
||||
'85',
|
||||
outputViews,
|
||||
]
|
||||
if (signal) {
|
||||
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args], { signal })
|
||||
} else {
|
||||
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function renderAnnotationsToSvg(
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
calculateTurntableFrameCenterTime,
|
||||
degreeToSecond,
|
||||
degreeToTurntableFrame,
|
||||
isValidTurntableDegree,
|
||||
secondToDegree,
|
||||
secondToTurntableFrame,
|
||||
TOTAL_TURNTABLE_FRAMES,
|
||||
TURNTABLE_FPS,
|
||||
} from './model3d'
|
||||
|
||||
describe('model3d utils', () => {
|
||||
describe('secondToTurntableFrame', () => {
|
||||
it('converts playback seconds to frame indices (0..23)', () => {
|
||||
expect(secondToTurntableFrame(0)).toBe(0)
|
||||
expect(secondToTurntableFrame(0.167)).toBe(1)
|
||||
expect(secondToTurntableFrame(0.5)).toBe(3)
|
||||
expect(secondToTurntableFrame(2.0)).toBe(12)
|
||||
expect(secondToTurntableFrame(3.9)).toBe(23)
|
||||
})
|
||||
|
||||
it('clamps out-of-range seconds', () => {
|
||||
expect(secondToTurntableFrame(-1)).toBe(0)
|
||||
expect(secondToTurntableFrame(10)).toBe(TOTAL_TURNTABLE_FRAMES - 1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('secondToDegree', () => {
|
||||
it('converts seconds to turntable degrees (0, 15, ..., 345)', () => {
|
||||
expect(secondToDegree(0)).toBe(0)
|
||||
expect(secondToDegree(1 / TURNTABLE_FPS)).toBe(15)
|
||||
expect(secondToDegree(0.5)).toBe(45)
|
||||
expect(secondToDegree(2.0)).toBe(180)
|
||||
expect(secondToDegree(3.9)).toBe(345)
|
||||
})
|
||||
})
|
||||
|
||||
describe('degreeToTurntableFrame', () => {
|
||||
it('converts discrete degrees to frame indices', () => {
|
||||
expect(degreeToTurntableFrame(0)).toBe(0)
|
||||
expect(degreeToTurntableFrame(15)).toBe(1)
|
||||
expect(degreeToTurntableFrame(45)).toBe(3)
|
||||
expect(degreeToTurntableFrame(90)).toBe(6)
|
||||
expect(degreeToTurntableFrame(180)).toBe(12)
|
||||
expect(degreeToTurntableFrame(345)).toBe(23)
|
||||
})
|
||||
|
||||
it('normalizes degrees outside [0, 360)', () => {
|
||||
expect(degreeToTurntableFrame(360)).toBe(0)
|
||||
expect(degreeToTurntableFrame(-15)).toBe(23)
|
||||
expect(degreeToTurntableFrame(720)).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('calculateTurntableFrameCenterTime and degreeToSecond', () => {
|
||||
it('calculates frame center times within bounds', () => {
|
||||
const centerTime0 = calculateTurntableFrameCenterTime(0)
|
||||
expect(centerTime0).toBeCloseTo(0.5 / TURNTABLE_FPS)
|
||||
|
||||
const centerTime23 = calculateTurntableFrameCenterTime(23)
|
||||
expect(centerTime23).toBeLessThan(TOTAL_TURNTABLE_FRAMES / TURNTABLE_FPS)
|
||||
expect(centerTime23).toBeGreaterThan(23 / TURNTABLE_FPS)
|
||||
|
||||
expect(degreeToSecond(0)).toBeCloseTo(centerTime0)
|
||||
expect(degreeToSecond(45)).toBeCloseTo((3 + 0.5) / TURNTABLE_FPS)
|
||||
expect(degreeToSecond(345)).toBeCloseTo(centerTime23)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isValidTurntableDegree', () => {
|
||||
it('returns true for integer multiples of 15 from 0 to 345', () => {
|
||||
for (let deg = 0; deg < 360; deg += 15) {
|
||||
expect(isValidTurntableDegree(deg)).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
it('returns false for non-multiples of 15', () => {
|
||||
expect(isValidTurntableDegree(10)).toBe(false)
|
||||
expect(isValidTurntableDegree(44)).toBe(false)
|
||||
expect(isValidTurntableDegree(45.5)).toBe(false)
|
||||
})
|
||||
|
||||
it('returns false for out of range or negative degrees', () => {
|
||||
expect(isValidTurntableDegree(-15)).toBe(false)
|
||||
expect(isValidTurntableDegree(360)).toBe(false)
|
||||
expect(isValidTurntableDegree(720)).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,58 @@
|
||||
export const TOTAL_TURNTABLE_FRAMES = 24
|
||||
export const TURNTABLE_FPS = 6
|
||||
export const TURNTABLE_FRAME_DURATION = 1 / TURNTABLE_FPS
|
||||
|
||||
/**
|
||||
* Converts a playback second into a turntable frame index (0..23), clamped.
|
||||
*/
|
||||
export function secondToTurntableFrame(second: number): number {
|
||||
return Math.min(
|
||||
Math.max(0, Math.floor(second * TURNTABLE_FPS + 1e-4)),
|
||||
TOTAL_TURNTABLE_FRAMES - 1,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a turntable playback second to turntable degree (0, 15, 30, ..., 345).
|
||||
*/
|
||||
export function secondToDegree(second: number): number {
|
||||
const frameIndex = secondToTurntableFrame(second)
|
||||
return (frameIndex * 15) % 360
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the presentation time within a turntable frame.
|
||||
* Seeking near frame center avoids PTS boundary inaccuracies in video decoders.
|
||||
*/
|
||||
export function calculateTurntableFrameCenterTime(frameIndex: number): number {
|
||||
const normalizedFrame =
|
||||
((frameIndex % TOTAL_TURNTABLE_FRAMES) + TOTAL_TURNTABLE_FRAMES) % TOTAL_TURNTABLE_FRAMES
|
||||
if (normalizedFrame === TOTAL_TURNTABLE_FRAMES - 1) {
|
||||
return normalizedFrame * TURNTABLE_FRAME_DURATION + TURNTABLE_FRAME_DURATION * 0.25
|
||||
}
|
||||
return (normalizedFrame + 0.5) * TURNTABLE_FRAME_DURATION
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a rotation degree (0..345) to a turntable frame index (0..23).
|
||||
*/
|
||||
export function degreeToTurntableFrame(degree: number): number {
|
||||
const normalizedDegree = ((Math.round(degree) % 360) + 360) % 360
|
||||
return Math.round(normalizedDegree / 15) % TOTAL_TURNTABLE_FRAMES
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a rotation degree into a turntable video playback second (center time of that frame).
|
||||
*/
|
||||
export function degreeToSecond(degree: number): number {
|
||||
const frameIndex = degreeToTurntableFrame(degree)
|
||||
return calculateTurntableFrameCenterTime(frameIndex)
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates whether a degree is a valid discrete turntable angle:
|
||||
* integer multiple of 15 between 0 and 345 inclusive (0, 15, 30, ..., 345).
|
||||
*/
|
||||
export function isValidTurntableDegree(degree: number): boolean {
|
||||
return Number.isInteger(degree) && degree >= 0 && degree <= 345 && degree % 15 === 0
|
||||
}
|
||||
Reference in New Issue
Block a user