Sidebar.Project.Video.js 3.4 KB

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