| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import { Injectable } from '@angular/core';
- import { VideoGenerationCapability } from '../models/video-generation-capability.model';
- @Injectable({ providedIn: 'root' })
- export class VideoDurationService {
- private readonly fps = 24;
- secondsToFrames(seconds: number): number {
- const normalized = Math.max(1, Number(seconds) || 5);
- return Math.max(1, Math.round(normalized * this.fps) + 1);
- }
- framesToSeconds(frames: number): number {
- const normalized = Math.max(1, Number(frames) || 121);
- return Math.max(1, Math.round((normalized - 1) / this.fps));
- }
- normalizeSeconds(seconds: number, capability: VideoGenerationCapability): number {
- const raw = Number(seconds);
- const fallback = capability.defaultSeconds || 5;
- const value = Number.isFinite(raw) && raw > 0 ? raw : fallback;
- return Math.max(capability.minSeconds, Math.min(capability.maxSeconds, Math.round(value)));
- }
- normalizeToNearestAllowedSeconds(seconds: number, allowedSeconds: number[] = [5, 10], fallback = 5): number {
- const value = Math.round(Number(seconds) || fallback);
- const candidates = allowedSeconds.filter((item) => Number.isFinite(item) && item > 0);
- if (!candidates.length) return fallback;
- return candidates.reduce((best, current) => {
- const bestDistance = Math.abs(best - value);
- const currentDistance = Math.abs(current - value);
- return currentDistance < bestDistance ? current : best;
- }, candidates[0]);
- }
- validateDuration(seconds: number, capability: VideoGenerationCapability): string {
- if (seconds < capability.minSeconds) return `最短支持 ${capability.minSeconds} 秒`;
- if (seconds > capability.maxSeconds) return `最长支持 ${capability.maxSeconds} 秒`;
- return '';
- }
- }
|