Sidebar.Project.Video.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import { UIBreak, UIButton, UIInteger, UIPanel, UIRow, UIText } from './libs/ui.js';
  2. import { APP } from './libs/app.js';
  3. function SidebarProjectVideo( editor ) {
  4. const strings = editor.strings;
  5. const container = new UIPanel();
  6. container.setId( 'render' );
  7. // Video
  8. container.add( new UIText( strings.getKey( 'sidebar/project/video' ) ).setTextTransform( 'uppercase' ) );
  9. container.add( new UIBreak(), new UIBreak() );
  10. // Resolution
  11. function toDiv2() {
  12. // Make sure dimensions are divisible by 2 (requirement of libx264)
  13. this.setValue( 2 * Math.floor( this.getValue() / 2 ) );
  14. }
  15. const resolutionRow = new UIRow();
  16. container.add( resolutionRow );
  17. resolutionRow.add( new UIText( strings.getKey( 'sidebar/project/resolution' ) ).setClass( 'Label' ) );
  18. const videoWidth = new UIInteger( 1024 ).setTextAlign( 'center' ).setWidth( '28px' ).setStep( 2 ).onChange( toDiv2 );
  19. resolutionRow.add( videoWidth );
  20. resolutionRow.add( new UIText( '×' ).setTextAlign( 'center' ).setFontSize( '12px' ).setWidth( '12px' ) );
  21. const videoHeight = new UIInteger( 1024 ).setTextAlign( 'center' ).setWidth( '28px' ).setStep( 2 ).onChange( toDiv2 );
  22. resolutionRow.add( videoHeight );
  23. const videoFPS = new UIInteger( 30 ).setTextAlign( 'center' ).setWidth( '20px' );
  24. resolutionRow.add( videoFPS );
  25. resolutionRow.add( new UIText( 'fps' ).setFontSize( '12px' ) );
  26. // Duration
  27. const videoDurationRow = new UIRow();
  28. videoDurationRow.add( new UIText( strings.getKey( 'sidebar/project/duration' ) ).setClass( 'Label' ) );
  29. const videoDuration = new UIInteger( 10 );
  30. videoDurationRow.add( videoDuration );
  31. container.add( videoDurationRow );
  32. // Render
  33. const renderButton = new UIButton( strings.getKey( 'sidebar/project/render' ) );
  34. renderButton.setWidth( '170px' );
  35. renderButton.setMarginLeft( '120px' );
  36. renderButton.onClick( async () => {
  37. const player = new APP.Player();
  38. player.load( editor.toJSON() );
  39. player.setPixelRatio( 1 );
  40. player.setSize( videoWidth.getValue(), videoHeight.getValue() );
  41. //
  42. const width = videoWidth.getValue() / window.devicePixelRatio;
  43. const height = videoHeight.getValue() / window.devicePixelRatio;
  44. const canvas = player.canvas;
  45. canvas.style.width = width + 'px';
  46. canvas.style.height = height + 'px';
  47. const left = ( screen.width - width ) / 2;
  48. const top = ( screen.height - height ) / 2;
  49. const output = window.open( '', '_blank', `location=no,left=${left},top=${top},width=${width},height=${height}` );
  50. const meta = document.createElement( 'meta' );
  51. meta.name = 'viewport';
  52. meta.content = 'width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0';
  53. output.document.head.appendChild( meta );
  54. output.document.body.style.background = '#000';
  55. output.document.body.style.margin = '0px';
  56. output.document.body.style.overflow = 'hidden';
  57. output.document.body.appendChild( canvas );
  58. const status = document.createElement( 'div' );
  59. status.style.position = 'absolute';
  60. status.style.top = '10px';
  61. status.style.left = '10px';
  62. status.style.color = 'white';
  63. status.style.fontFamily = 'system-ui';
  64. status.style.fontSize = '12px';
  65. status.style.textShadow = '0 0 2px black';
  66. output.document.body.appendChild( status );
  67. const writeFileStatus = document.createElement( 'span' );
  68. status.appendChild( writeFileStatus );
  69. const encodingText = document.createElement( 'span' );
  70. encodingText.textContent = ' encoding'; // TODO: l10n
  71. encodingText.hidden = true;
  72. status.appendChild( encodingText );
  73. const encodingStatus = document.createElement( 'span' );
  74. encodingStatus.hidden = true;
  75. status.appendChild( encodingStatus );
  76. const videoSizeText = document.createElement( 'span' );
  77. videoSizeText.textContent = ' size'; // TODO: l10n
  78. videoSizeText.hidden = true;
  79. status.appendChild( videoSizeText );
  80. const videoSizeStatus = document.createElement( 'span' );
  81. videoSizeStatus.hidden = true;
  82. status.appendChild( videoSizeStatus );
  83. const completedStatus = document.createElement( 'span' );
  84. completedStatus.textContent = ' ✓';
  85. completedStatus.hidden = true;
  86. status.appendChild( completedStatus );
  87. const video = document.createElement( 'video' );
  88. video.width = width;
  89. video.height = height;
  90. video.controls = true;
  91. video.loop = true;
  92. video.hidden = true;
  93. output.document.body.appendChild( video );
  94. //
  95. const { createFFmpeg, fetchFile } = FFmpeg; // eslint-disable-line no-undef
  96. const ffmpeg = createFFmpeg( { log: true } );
  97. await ffmpeg.load();
  98. ffmpeg.setProgress( ( { ratio } ) => {
  99. encodingStatus.textContent = `( ${ Math.floor( ratio * 100 ) }% )`;
  100. } );
  101. output.addEventListener( 'unload', function () {
  102. if ( video.src.startsWith( 'blob:' ) ) {
  103. URL.revokeObjectURL( video.src );
  104. } else {
  105. ffmpeg.exit();
  106. }
  107. } );
  108. const fps = videoFPS.getValue();
  109. const duration = videoDuration.getValue();
  110. const frames = duration * fps;
  111. //
  112. await ( async function () {
  113. let currentTime = 0;
  114. for ( let i = 0; i < frames; i ++ ) {
  115. player.render( currentTime );
  116. const num = i.toString().padStart( 5, '0' );
  117. if ( output.closed ) return;
  118. ffmpeg.FS( 'writeFile', `tmp.${num}.png`, await fetchFile( canvas.toDataURL() ) );
  119. currentTime += 1 / fps;
  120. const frame = i + 1;
  121. const progress = Math.floor( frame / frames * 100 );
  122. writeFileStatus.textContent = `${ frame } / ${ frames } ( ${ progress }% )`;
  123. }
  124. encodingText.hidden = false;
  125. encodingStatus.hidden = false;
  126. await ffmpeg.run( '-framerate', String( fps ), '-pattern_type', 'glob', '-i', '*.png', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'slow', '-crf', String( 5 ), 'out.mp4' );
  127. const videoData = ffmpeg.FS( 'readFile', 'out.mp4' );
  128. for ( let i = 0; i < frames; i ++ ) {
  129. const num = i.toString().padStart( 5, '0' );
  130. ffmpeg.FS( 'unlink', `tmp.${num}.png` );
  131. }
  132. ffmpeg.FS( 'unlink', 'out.mp4' );
  133. output.document.body.removeChild( canvas );
  134. videoSizeText.hidden = false;
  135. videoSizeStatus.textContent = `( ${ formatFileSize( videoData.buffer.byteLength ) } )`;
  136. videoSizeStatus.hidden = false;
  137. completedStatus.hidden = false;
  138. video.src = URL.createObjectURL( new Blob( [ videoData.buffer ], { type: 'video/mp4' } ) );
  139. video.hidden = false;
  140. } )();
  141. player.dispose();
  142. } );
  143. container.add( renderButton );
  144. //
  145. return container;
  146. }
  147. function formatFileSize( sizeB, K = 1024 ) {
  148. if ( sizeB === 0 ) return '0B';
  149. const sizes = [ sizeB, sizeB / K, sizeB / K / K ].reverse();
  150. const units = [ 'B', 'KB', 'MB' ].reverse();
  151. const index = sizes.findIndex( size => size >= 1 );
  152. return new Intl.NumberFormat( 'en-us', { useGrouping: true, maximumFractionDigits: 1 } )
  153. .format( sizes[ index ] ) + units[ index ];
  154. }
  155. export { SidebarProjectVideo };