Sidebar.Project.Video.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 progress = document.createElement( 'progress' );
  59. progress.style.position = 'absolute';
  60. progress.style.top = '10px';
  61. progress.style.left = ( ( width - 170 ) / 2 ) + 'px';
  62. progress.style.width = '170px';
  63. progress.value = 0;
  64. output.document.body.appendChild( progress );
  65. const video = document.createElement( 'video' );
  66. video.width = width;
  67. video.height = height;
  68. video.controls = true;
  69. video.loop = true;
  70. video.hidden = true;
  71. output.document.body.appendChild( video );
  72. //
  73. const { createFFmpeg, fetchFile } = FFmpeg; // eslint-disable-line no-undef
  74. const ffmpeg = createFFmpeg( { log: true } );
  75. await ffmpeg.load();
  76. ffmpeg.setProgress( ( { ratio } ) => {
  77. // A ffmpeg bug:
  78. // `ratio` sometimes be NaN (ffmpegwasm/ffmpeg.wasm/issues/178)
  79. // `ratio` sometimes be Infinity when rendering multiple videos simultaneously
  80. if ( Number.isFinite( ratio ) ) { // guards both Infinity and NaN
  81. progress.value = ( ratio * 0.5 ) + 0.5;
  82. }
  83. } );
  84. output.addEventListener( 'unload', function () {
  85. if ( video.src.startsWith( 'blob:' ) === false ) {
  86. ffmpeg.exit();
  87. }
  88. } );
  89. const fps = videoFPS.getValue();
  90. const duration = videoDuration.getValue();
  91. const frames = duration * fps;
  92. //
  93. await ( async function () {
  94. let currentTime = 0;
  95. for ( let i = 0; i < frames; i ++ ) {
  96. player.render( currentTime );
  97. const num = i.toString().padStart( 5, '0' );
  98. if ( output.closed ) return;
  99. ffmpeg.FS( 'writeFile', `tmp.${num}.png`, await fetchFile( canvas.toDataURL() ) );
  100. currentTime += 1 / fps;
  101. progress.value = ( i / frames ) * 0.5;
  102. }
  103. await ffmpeg.run( '-framerate', String( fps ), '-pattern_type', 'glob', '-i', '*.png', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'slow', '-crf', String( 5 ), 'out.mp4' );
  104. const videoData = ffmpeg.FS( 'readFile', 'out.mp4' );
  105. for ( let i = 0; i < frames; i ++ ) {
  106. const num = i.toString().padStart( 5, '0' );
  107. ffmpeg.FS( 'unlink', `tmp.${num}.png` );
  108. }
  109. ffmpeg.FS( 'unlink', 'out.mp4' );
  110. output.document.body.removeChild( canvas );
  111. output.document.body.removeChild( progress );
  112. video.src = URL.createObjectURL( new Blob( [ videoData.buffer ], { type: 'video/mp4' } ) );
  113. video.hidden = false;
  114. } )();
  115. player.dispose();
  116. } );
  117. container.add( renderButton );
  118. //
  119. return container;
  120. }
  121. export { SidebarProjectVideo };