| 26 | * @internal |
| 27 | */ |
| 28 | export class ImageExporter implements Exporter { |
| 29 | public static readonly id = '@revideo/core/image-sequence'; |
| 30 | public static readonly displayName = 'Image sequence'; |
| 31 | |
| 32 | public static async create( |
| 33 | project: Project, |
| 34 | settings: RendererSettings, |
| 35 | ): Promise<ImageExporter> { |
| 36 | return new ImageExporter(project.logger, settings); |
| 37 | } |
| 38 | |
| 39 | private static readonly response = new EventDispatcher<ServerResponse>(); |
| 40 | |
| 41 | static { |
| 42 | if (import.meta.hot) { |
| 43 | import.meta.hot.on('revideo:export-ack', response => { |
| 44 | this.response.dispatch(response); |
| 45 | }); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private readonly frameLookup = new Set<number>(); |
| 50 | private readonly projectName: string; |
| 51 | private readonly quality: number; |
| 52 | private readonly fileType: CanvasOutputMimeType; |
| 53 | private readonly groupByScene: boolean; |
| 54 | |
| 55 | public constructor( |
| 56 | private readonly logger: Logger, |
| 57 | settings: RendererSettings, |
| 58 | ) { |
| 59 | if (settings.exporter.name !== ImageExporter.id) { |
| 60 | throw new Error( |
| 61 | `Invalid exporter name: ${settings.exporter.name}. Expected: ${ImageExporter.id}`, |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | const options = settings.exporter.options as ImageExporterOptions; |
| 66 | this.projectName = settings.name; |
| 67 | this.quality = clamp(0, 1, options.quality / 100); |
| 68 | this.fileType = options.fileType; |
| 69 | this.groupByScene = options.groupByScene; |
| 70 | } |
| 71 | |
| 72 | public async start() { |
| 73 | ImageExporter.response.subscribe(this.handleResponse); |
| 74 | } |
| 75 | |
| 76 | public async handleFrame( |
| 77 | canvas: HTMLCanvasElement, |
| 78 | frame: number, |
| 79 | sceneFrame: number, |
| 80 | sceneName: string, |
| 81 | signal: AbortSignal, |
| 82 | ) { |
| 83 | if (this.frameLookup.has(frame)) { |
| 84 | this.logger.warn(`Frame no. ${frame} is already being exported.`); |
| 85 | return; |