Sidebar.Project.Video.js 6.7 KB

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