Sidebar.Project.Video.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. const resolutionRow = new UIRow();
  12. container.add( resolutionRow );
  13. resolutionRow.add( new UIText( strings.getKey( 'sidebar/project/resolution' ) ).setWidth( '90px' ) );
  14. const videoWidth = new UIInteger( 1024 ).setTextAlign( 'center' ).setWidth( '28px' );
  15. resolutionRow.add( videoWidth );
  16. resolutionRow.add( new UIText( '×' ).setTextAlign( 'center' ).setFontSize( '12px' ).setWidth( '12px' ) );
  17. const videoHeight = new UIInteger( 1024 ).setTextAlign( 'center' ).setWidth( '28px' );
  18. resolutionRow.add( videoHeight );
  19. const videoFPS = new UIInteger( 30 ).setTextAlign( 'center' ).setWidth( '20px' );
  20. resolutionRow.add( videoFPS );
  21. resolutionRow.add( new UIText( 'fps' ).setFontSize( '12px' ) );
  22. // Duration
  23. const videoDurationRow = new UIRow();
  24. videoDurationRow.add( new UIText( strings.getKey( 'sidebar/project/duration' ) ).setWidth( '90px' ) );
  25. const videoDuration = new UIInteger( 10 );
  26. videoDurationRow.add( videoDuration );
  27. container.add( videoDurationRow );
  28. // Render
  29. const renderButton = new UIButton( strings.getKey( 'sidebar/project/render' ) );
  30. renderButton.setWidth( '170px' );
  31. renderButton.setMarginLeft( '90px' );
  32. renderButton.onClick( async () => {
  33. const player = new APP.Player();
  34. player.load( editor.toJSON() );
  35. player.setPixelRatio( 1 );
  36. player.setSize( videoWidth.getValue(), videoHeight.getValue() );
  37. //
  38. const width = videoWidth.getValue() / window.devicePixelRatio;
  39. const height = videoHeight.getValue() / window.devicePixelRatio;
  40. const canvas = player.canvas;
  41. canvas.style.width = width + 'px';
  42. canvas.style.height = height + 'px';
  43. const left = ( screen.width - width ) / 2;
  44. const top = ( screen.height - height ) / 2;
  45. const output = window.open( '', '_blank', `location=no,left=${left},top=${top},width=${width},height=${height}` );
  46. output.document.body.style.background = '#000';
  47. output.document.body.style.margin = '0px';
  48. output.document.body.style.overflow = 'hidden';
  49. output.document.body.appendChild( canvas );
  50. const progress = document.createElement( 'progress' );
  51. progress.style.position = 'absolute';
  52. progress.style.top = '10px';
  53. progress.style.left = ( ( width - 170 ) / 2 ) + 'px';
  54. progress.style.width = '170px';
  55. progress.value = 0;
  56. output.document.body.appendChild( progress );
  57. //
  58. const { createFFmpeg, fetchFile } = FFmpeg; // eslint-disable-line no-undef
  59. const ffmpeg = createFFmpeg( { log: true } );
  60. await ffmpeg.load();
  61. ffmpeg.setProgress( ( { ratio } ) => {
  62. progress.value = ( ratio * 0.5 ) + 0.5;
  63. } );
  64. const fps = videoFPS.getValue();
  65. const duration = videoDuration.getValue();
  66. const frames = duration * fps;
  67. let currentTime = 0;
  68. for ( let i = 0; i < frames; i ++ ) {
  69. player.render( currentTime );
  70. const num = i.toString().padStart( 5, '0' );
  71. ffmpeg.FS( 'writeFile', `tmp.${num}.png`, await fetchFile( canvas.toDataURL() ) );
  72. currentTime += 1 / fps;
  73. progress.value = ( i / frames ) * 0.5;
  74. }
  75. await ffmpeg.run( '-framerate', String( fps ), '-pattern_type', 'glob', '-i', '*.png', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'slow', '-crf', String( 5 ), 'out.mp4' );
  76. const data = ffmpeg.FS( 'readFile', 'out.mp4' );
  77. for ( let i = 0; i < frames; i ++ ) {
  78. const num = i.toString().padStart( 5, '0' );
  79. ffmpeg.FS( 'unlink', `tmp.${num}.png` );
  80. }
  81. output.document.body.removeChild( canvas );
  82. output.document.body.removeChild( progress );
  83. const video = document.createElement( 'video' );
  84. video.width = width;
  85. video.height = height;
  86. video.controls = true;
  87. video.loop = true;
  88. video.src = URL.createObjectURL( new Blob( [ data.buffer ], { type: 'video/mp4' } ) );
  89. output.document.body.appendChild( video );
  90. player.dispose();
  91. } );
  92. container.add( renderButton );
  93. //
  94. return container;
  95. }
  96. export { SidebarProjectVideo };