Sidebar.Project.Video.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { UIBreak, UIButton, UIInteger, UIPanel, UIProgress, UIRow, UIText } from './libs/ui.js';
  2. import { APP } from './libs/app.js';
  3. function SidebarProjectVideo( editor ) {
  4. var container = new UIPanel();
  5. container.setId( 'render' );
  6. // Video
  7. container.add( new UIText( 'Video' ).setTextTransform( 'uppercase' ) );
  8. container.add( new UIBreak(), new UIBreak() );
  9. // Resolution
  10. var resolutionRow = new UIRow();
  11. container.add( resolutionRow );
  12. resolutionRow.add( new UIText( 'Resolution' ).setWidth( '90px' ) );
  13. var videoWidth = new UIInteger( 1024 ).setTextAlign( 'center' ).setWidth( '28px' );
  14. resolutionRow.add( videoWidth );
  15. resolutionRow.add( new UIText( '×' ).setTextAlign( 'center' ).setFontSize( '12px' ).setWidth( '12px' ) );
  16. var videoHeight = new UIInteger( 1024 ).setTextAlign( 'center' ).setWidth( '28px' );
  17. resolutionRow.add( videoHeight );
  18. var videoFPS = new UIInteger( 30 ).setTextAlign( 'center' ).setWidth( '20px' );
  19. resolutionRow.add( videoFPS );
  20. resolutionRow.add( new UIText( 'fps' ).setFontSize( '12px' ) );
  21. // Duration
  22. var videoDurationRow = new UIRow();
  23. videoDurationRow.add( new UIText( 'Duration' ).setWidth( '90px' ) );
  24. var videoDuration = new UIInteger( 10 );
  25. videoDurationRow.add( videoDuration );
  26. container.add( videoDurationRow );
  27. // Render
  28. container.add( new UIText( '' ).setWidth( '90px' ) );
  29. const progress = new UIProgress( 0 );
  30. progress.setDisplay( 'none' );
  31. progress.setWidth( '170px' );
  32. container.add( progress );
  33. const renderButton = new UIButton( 'RENDER' );
  34. renderButton.setWidth( '170px' );
  35. renderButton.onClick( async () => {
  36. renderButton.setDisplay( 'none' );
  37. progress.setDisplay( '' );
  38. progress.setValue( 0 );
  39. const player = new APP.Player();
  40. player.load( editor.toJSON() );
  41. player.setPixelRatio( 1 );
  42. player.setSize( videoWidth.getValue(), videoHeight.getValue() );
  43. const canvas = player.dom.firstElementChild;
  44. //
  45. const { createFFmpeg, fetchFile } = FFmpeg; // eslint-disable-line no-undef
  46. const ffmpeg = createFFmpeg( { log: true } );
  47. await ffmpeg.load();
  48. ffmpeg.setProgress( ( { ratio } ) => {
  49. progress.setValue( ratio );
  50. } );
  51. const fps = videoFPS.getValue();
  52. const duration = videoDuration.getValue();
  53. const frames = duration * fps;
  54. let currentTime = 0;
  55. for ( let i = 0; i < frames; i ++ ) {
  56. player.render( currentTime );
  57. const num = i.toString().padStart( 5, '0' );
  58. ffmpeg.FS( 'writeFile', `tmp.${num}.png`, await fetchFile( canvas.toDataURL() ) );
  59. currentTime += 1 / fps;
  60. progress.setValue( i / frames );
  61. }
  62. await ffmpeg.run( '-framerate', String( fps ), '-pattern_type', 'glob', '-i', '*.png', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'slow', '-crf', String( 6 ), 'out.mp4' );
  63. const data = ffmpeg.FS( 'readFile', 'out.mp4' );
  64. for ( let i = 0; i < frames; i ++ ) {
  65. const num = i.toString().padStart( 5, '0' );
  66. ffmpeg.FS( 'unlink', `tmp.${num}.png` );
  67. }
  68. save( new Blob( [ data.buffer ], { type: 'video/mp4' } ), 'out.mp4' );
  69. player.dispose();
  70. renderButton.setDisplay( '' );
  71. progress.setDisplay( 'none' );
  72. } );
  73. container.add( renderButton );
  74. // SAVE
  75. const link = document.createElement( 'a' );
  76. function save( blob, filename ) {
  77. if ( link.href ) {
  78. URL.revokeObjectURL( link.href );
  79. }
  80. link.href = URL.createObjectURL( blob );
  81. link.download = filename;
  82. link.dispatchEvent( new MouseEvent( 'click' ) );
  83. }
  84. //
  85. return container;
  86. }
  87. export { SidebarProjectVideo };