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