Sidebar.Project.Video.js 3.6 KB

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