Sidebar.Project.Video.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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' ) ).setClass( 'Label' ) );
  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' ) ).setClass( 'Label' ) );
  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( '120px' );
  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. const meta = document.createElement( 'meta' );
  47. meta.name = 'viewport';
  48. meta.content = 'width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0';
  49. output.document.head.appendChild( meta );
  50. output.document.body.style.background = '#000';
  51. output.document.body.style.margin = '0px';
  52. output.document.body.style.overflow = 'hidden';
  53. output.document.body.appendChild( canvas );
  54. const progress = document.createElement( 'progress' );
  55. progress.style.position = 'absolute';
  56. progress.style.top = '10px';
  57. progress.style.left = ( ( width - 170 ) / 2 ) + 'px';
  58. progress.style.width = '170px';
  59. progress.value = 0;
  60. output.document.body.appendChild( progress );
  61. //
  62. const { createFFmpeg, fetchFile } = FFmpeg; // eslint-disable-line no-undef
  63. const ffmpeg = createFFmpeg( { log: true } );
  64. await ffmpeg.load();
  65. ffmpeg.setProgress( ( { ratio } ) => {
  66. progress.value = ( ratio * 0.5 ) + 0.5;
  67. } );
  68. const fps = videoFPS.getValue();
  69. const duration = videoDuration.getValue();
  70. const frames = duration * fps;
  71. let currentTime = 0;
  72. for ( let i = 0; i < frames; i ++ ) {
  73. player.render( currentTime );
  74. const num = i.toString().padStart( 5, '0' );
  75. ffmpeg.FS( 'writeFile', `tmp.${num}.png`, await fetchFile( canvas.toDataURL() ) );
  76. currentTime += 1 / fps;
  77. progress.value = ( i / frames ) * 0.5;
  78. }
  79. await ffmpeg.run( '-framerate', String( fps ), '-pattern_type', 'glob', '-i', '*.png', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'slow', '-crf', String( 5 ), 'out.mp4' );
  80. const data = ffmpeg.FS( 'readFile', 'out.mp4' );
  81. for ( let i = 0; i < frames; i ++ ) {
  82. const num = i.toString().padStart( 5, '0' );
  83. ffmpeg.FS( 'unlink', `tmp.${num}.png` );
  84. }
  85. output.document.body.removeChild( canvas );
  86. output.document.body.removeChild( progress );
  87. const video = document.createElement( 'video' );
  88. video.width = width;
  89. video.height = height;
  90. video.controls = true;
  91. video.loop = true;
  92. video.src = URL.createObjectURL( new Blob( [ data.buffer ], { type: 'video/mp4' } ) );
  93. output.document.body.appendChild( video );
  94. player.dispose();
  95. } );
  96. container.add( renderButton );
  97. //
  98. return container;
  99. }
  100. export { SidebarProjectVideo };