|
| 1 | +import { UIBreak, UIButton, UIInteger, UIPanel, UIProgress, UIRow, UIText } from './libs/ui.js'; |
| 2 | + |
| 3 | +import { APP } from './libs/app.js'; |
| 4 | + |
| 5 | +function SidebarProjectVideo( editor ) { |
| 6 | + |
| 7 | + var container = new UIPanel(); |
| 8 | + container.setId( 'render' ); |
| 9 | + |
| 10 | + // Video |
| 11 | + |
| 12 | + container.add( new UIText( 'Video' ).setTextTransform( 'uppercase' ) ); |
| 13 | + container.add( new UIBreak(), new UIBreak() ); |
| 14 | + |
| 15 | + // Resolution |
| 16 | + |
| 17 | + var resolutionRow = new UIRow(); |
| 18 | + container.add( resolutionRow ); |
| 19 | + |
| 20 | + resolutionRow.add( new UIText( 'Resolution' ).setWidth( '90px' ) ); |
| 21 | + |
| 22 | + var videoWidth = new UIInteger( 600 ).setWidth( '28px' ); |
| 23 | + resolutionRow.add( videoWidth ); |
| 24 | + |
| 25 | + resolutionRow.add( new UIText( '×' ).setFontSize( '12px' ).setWidth( '14px' ) ); |
| 26 | + |
| 27 | + var videoHeight = new UIInteger( 600 ).setWidth( '28px' ); |
| 28 | + resolutionRow.add( videoHeight ); |
| 29 | + |
| 30 | + var videoFPS = new UIInteger( 30 ).setTextAlign( 'center' ).setWidth( '20px' ); |
| 31 | + resolutionRow.add( videoFPS ); |
| 32 | + |
| 33 | + resolutionRow.add( new UIText( 'fps' ).setFontSize( '12px' ) ); |
| 34 | + |
| 35 | + // Duration |
| 36 | + |
| 37 | + var videoDurationRow = new UIRow(); |
| 38 | + videoDurationRow.add( new UIText( 'Duration' ).setWidth( '90px' ) ); |
| 39 | + |
| 40 | + var videoDuration = new UIInteger( 10 ); |
| 41 | + videoDurationRow.add( videoDuration ); |
| 42 | + |
| 43 | + container.add( videoDurationRow ); |
| 44 | + |
| 45 | + // Render |
| 46 | + |
| 47 | + container.add( new UIText( '' ).setWidth( '90px' ) ); |
| 48 | + |
| 49 | + const progress = new UIProgress( 0 ); |
| 50 | + progress.setDisplay( 'none' ); |
| 51 | + progress.setWidth( '170px' ); |
| 52 | + container.add( progress ); |
| 53 | + |
| 54 | + const renderButton = new UIButton( 'RENDER' ); |
| 55 | + renderButton.setWidth( '170px' ); |
| 56 | + renderButton.onClick( async () => { |
| 57 | + |
| 58 | + renderButton.setDisplay( 'none' ); |
| 59 | + progress.setDisplay( '' ); |
| 60 | + progress.setValue( 0 ); |
| 61 | + |
| 62 | + const player = new APP.Player(); |
| 63 | + player.load( editor.toJSON() ); |
| 64 | + player.setPixelRatio( 1 ); |
| 65 | + player.setSize( videoWidth.getValue(), videoHeight.getValue() ); |
| 66 | + |
| 67 | + const canvas = player.dom.firstElementChild; |
| 68 | + |
| 69 | + // |
| 70 | + |
| 71 | + const { createFFmpeg, fetchFile } = FFmpeg; // eslint-disable-line no-undef |
| 72 | + const ffmpeg = createFFmpeg( { log: true } ); |
| 73 | + |
| 74 | + await ffmpeg.load(); |
| 75 | + |
| 76 | + ffmpeg.setProgress( ( { ratio } ) => { |
| 77 | + |
| 78 | + progress.setValue( ratio ); |
| 79 | + |
| 80 | + } ); |
| 81 | + |
| 82 | + const fps = videoFPS.getValue(); |
| 83 | + const duration = videoDuration.getValue(); |
| 84 | + const frames = duration * fps; |
| 85 | + |
| 86 | + let currentTime = 0; |
| 87 | + |
| 88 | + for ( let i = 0; i < frames; i ++ ) { |
| 89 | + |
| 90 | + player.render( currentTime ); |
| 91 | + |
| 92 | + const num = i.toString().padStart( 5, '0' ); |
| 93 | + ffmpeg.FS( 'writeFile', `tmp.${num}.png`, await fetchFile( canvas.toDataURL() ) ); |
| 94 | + currentTime += 1 / fps; |
| 95 | + |
| 96 | + progress.setValue( i / frames ); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + await ffmpeg.run( '-framerate', String( fps ), '-pattern_type', 'glob', '-i', '*.png', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'slow', '-crf', String( 6 ), 'out.mp4' ); |
| 101 | + |
| 102 | + const data = ffmpeg.FS( 'readFile', 'out.mp4' ); |
| 103 | + |
| 104 | + for ( let i = 0; i < frames; i ++ ) { |
| 105 | + |
| 106 | + const num = i.toString().padStart( 5, '0' ); |
| 107 | + ffmpeg.FS( 'unlink', `tmp.${num}.png` ); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + save( new Blob( [ data.buffer ], { type: 'video/mp4' } ), 'out.mp4' ); |
| 112 | + |
| 113 | + player.dispose(); |
| 114 | + |
| 115 | + renderButton.setDisplay( '' ); |
| 116 | + progress.setDisplay( 'none' ); |
| 117 | + |
| 118 | + } ); |
| 119 | + container.add( renderButton ); |
| 120 | + |
| 121 | + // SAVE |
| 122 | + |
| 123 | + const link = document.createElement( 'a' ); |
| 124 | + |
| 125 | + function save( blob, filename ) { |
| 126 | + |
| 127 | + link.href = URL.createObjectURL( blob ); |
| 128 | + link.download = filename; |
| 129 | + link.dispatchEvent( new MouseEvent( 'click' ) ); |
| 130 | + |
| 131 | + // URL.revokeObjectURL( url ); breaks Firefox... |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + // |
| 136 | + |
| 137 | + return container; |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +export { SidebarProjectVideo }; |
0 commit comments