File size: 3,569 Bytes
52ae39e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var workerPath =
  'https://archive.org/download/ffmpeg_asm/ffmpeg_asm.js';

function processInWebWorker() {
  var blob = URL.createObjectURL(
    new Blob(
      [
        'importScripts("' +
          workerPath +
          '");var now = Date.now;function print(text) {postMessage({"type" : "stdout","data" : text});};onmessage = function(event) {var message = event.data;if (message.type === "command") {var Module = {print: print,printErr: print,files: message.files || [],arguments: message.arguments || [],TOTAL_MEMORY: message.TOTAL_MEMORY||536870912  || false};postMessage({"type" : "start","data" : Module.arguments.join(" ")});postMessage({"type" : "stdout","data" : "Received command: " +Module.arguments.join(" ") +((Module.TOTAL_MEMORY ) ? ".  Processing with " + Module.TOTAL_MEMORY + " bits." : "")});var time = now();var result = ffmpeg_run(Module);var totalTime = now() - time;postMessage({"type" : "stdout","data" : "Finished processing (took " + totalTime + "ms)"});postMessage({"type" : "done","data" : result,"time" : totalTime});}};postMessage({"type" : "ready"});',
      ],
      {
        type: 'application/javascript',
      }
    )
  );

  var worker = new Worker(blob);
  URL.revokeObjectURL(blob);
  return worker;
}

var worker;

function convertStreams(videoBlob, setting) {
  var aab;
  var buffersReady;
  var workerReady;
  var posted;

  var fileReader = new FileReader();
  fileReader.onload = function () {
    aab = this.result;
    postMessage();
  };
  fileReader.readAsArrayBuffer(videoBlob);

  if (!worker) {
    worker = processInWebWorker();
  }
  worker.onmessage = function (event) {
    var message = event.data;
    if (message.type == 'ready') {
      workerReady = true;
      if (buffersReady) postMessage();
    } else if (message.type == 'done') {
      var result = message.data[0];
      if (setting == 'gif') {
        var blob = new File([result.data], 'test.gif', {
          type: 'image/gif',
        });
        PostBlob(blob);
      } else if (setting == 'mp4') {
        var blob = new File([result.data], 'test.mp4', {
          type: 'video/mp4',
        });
        PostBlob(blob);
      }
    }
  };
  var postMessage = function () {
    posted = true;
    if (setting == 'gif') {
      worker.postMessage({
        type: 'command',
        arguments: '-i video.webm -r 24 output-10.gif'.split(' '),
        files: [
          {
            data: new Uint8Array(aab),
            name: 'video.webm',
          },
        ],
      });
    } else if (setting == 'mp4') {
      worker.postMessage({
        type: 'command',
        arguments:
          '-i video.webm -c:v mpeg4 -b:v 6400k -strict experimental output.mp4'.split(
            ' '
          ),
        files: [
          {
            data: new Uint8Array(aab),
            name: 'video.webm',
          },
        ],
      });
    }
  };
}

function PostBlob(blob) {
  var url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.style.display = 'none';
  a.href = url;
  a.download = name;
  document.body.appendChild(a);
  a.click();
  recording = false;
  currenttime = 0;
  animate(false, 0);
  $('#seekbar').offset({
    left:
      offset_left +
      $('#inner-timeline').offset().left +
      currenttime / timelinetime,
  });
  canvas.renderAll();
  resizeCanvas();
  if (background_audio != false) {
    background_audio.pause();
    background_audio = new Audio(background_audio.src);
  }
  $('#download-real').html('Download');
  $('#download-real').removeClass('downloading');
  updateRecordCanvas();
}