File size: 1,197 Bytes
a8c39f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Setup if needed and start recording.
async () => {
  // Set up recording functions if not already initialized
  if (!window.startRecording) {
      let recorder_js = null;
      let main_js = null;
  }

  // Function to fetch and convert video blob to base64 using async/await without explicit Promise
  async function getVideoBlobAsBase64(objectURL) {
      const response = await fetch(objectURL);
      if (!response.ok) {
        throw new Error('Failed to fetch video blob.');
      }

      const blob = await response.blob();

      const reader = new FileReader();
      reader.readAsDataURL(blob);

      return new Promise((resolve, reject) => {
        reader.onloadend = () => {
          if (reader.result) {
            resolve(reader.result.split(',')[1]); // Return the base64 string (without data URI prefix)
          } else {
            reject('Failed to convert blob to base64.');
          }
        };
      });
  }

  if (window.currentState === "RECORDING") {
      await window.stopRecording();
      const base64String = await getVideoBlobAsBase64(window.videoSource);
      return base64String;
  } else {
      window.startRecording();
      return "Record";
  }
}