File size: 1,424 Bytes
d305e44
 
c1f12bf
 
212820a
 
 
 
c1f12bf
 
 
212820a
c1f12bf
 
 
7fd5ad1
c1f12bf
38b0287
 
 
 
 
 
c1f12bf
38b0287
7fd5ad1
38b0287
c1f12bf
 
 
 
d305e44
 
 
 
 
38b0287
 
 
7fd5ad1
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
import { convertAudioToMp3 } from '@/lib/ffmpeg/convertAudioToMp3'

import { fetchContentToBase64 } from './fetchContentToBase64'
import { convertToJpeg } from './convertToJpeg'

export async function decodeOutput(input: any): Promise<string> {
  const urlOrBase64 = `${input || ''}`

  if (!urlOrBase64) {
    return ''
  }

  const base64Url = urlOrBase64.startsWith('data:')
    ? urlOrBase64
    : await fetchContentToBase64(urlOrBase64)

  if (base64Url.startsWith('data:image/')) {
    // this step is important since some providers store data as PNG,
    // which is a unreasonable since a few frames quickly add up to 10 Mb,
    // we can't afford to have a 20 Gb .clap file
    //
    // if you really want to have a pro, Hollywood-grade storyboard storage,
    // this isn't impossible but then you need to use either file paths or remote URL paths
    // and if you want some lossless like this, we should add a parameter to support that
    const jpegImageAsBase64 = await convertToJpeg(base64Url)

    return jpegImageAsBase64
  } else if (
    base64Url.startsWith('data:audio/wav') ||
    base64Url.startsWith('data:audio/x-wav')
  ) {
    // same logic as for images here: we convert to a more compact format
    // and if you want some lossless, we should add a parameter to support that
    const mp4AudioAsBase64 = await convertAudioToMp3(base64Url)

    return mp4AudioAsBase64
  }

  return base64Url
}