File size: 2,475 Bytes
c1f12bf
 
9c63d01
 
cc43ec0
 
083ce88
1425c47
 
 
 
9c63d01
1425c47
 
 
 
 
 
9c63d01
1425c47
9c63d01
1425c47
 
9c63d01
1425c47
 
9c63d01
1425c47
9c63d01
1425c47
9c63d01
1425c47
9c63d01
1425c47
9c63d01
1425c47
9c63d01
 
 
c1f12bf
 
 
 
9c63d01
 
cc43ec0
 
 
 
 
 
 
 
 
 
 
 
c1f12bf
 
9c63d01
c1f12bf
9c63d01
 
c1f12bf
9c63d01
 
c1f12bf
 
9c63d01
 
 
c1f12bf
 
 
9c63d01
1425c47
ecec80b
9c63d01
cc43ec0
9c63d01
1425c47
 
9c63d01
 
 
 
 
 
 
 
 
 
 
c1f12bf
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
import { ClapAssetSource, ClapSegmentCategory } from '@aitube/clap'
import { TimelineSegment } from '@aitube/timeline'

export type ExportableSegment = {
  id: string

  segment: TimelineSegment

  // lowercase category name
  category: string

  directory: string

  // a short id that is neither a hash or a UUID, but instead something like
  // video0, video1, sound100, storyboard435 etc..
  shortId: string

  // used to create short and unique IDs in the project files for external audio editors
  index: number

  prefix: string

  // eg image/jpeg, audio/mpeg
  mimetype: string

  // eg mp3, mp4
  format: string

  filePath: string

  fileName: string

  assetUrl: string

  assetSourceType: ClapAssetSource

  isExportableToFile: boolean
}

export function formatSegmentForExport(
  segment: TimelineSegment,
  index: number
): ExportableSegment {
  const directory = `${segment.category}`.toLowerCase()
  const prefix = `shot_${String(index).padStart(4, '0')}_`

  const notFoundFileFormat = 'unknown/unknown'
  let fallbackFileFormat = notFoundFileFormat

  if (segment.assetUrl.startsWith('data:')) {
    const reg = new RegExp(/data:(.*);base64/gi)
    fallbackFileFormat = `${reg.exec(segment.assetUrl)?.[1] || notFoundFileFormat}`
  }

  // old .clap files might not have the `assetFileFormat`
  // which is why we perform a fallback check
  let mimetype = `${segment.assetFileFormat || fallbackFileFormat}`
  if (mimetype === 'audio/mpeg') {
    mimetype = 'audio/mp3'
  }
  const format = `${mimetype.split('/').pop() || 'unknown'}`.toLowerCase()
  const fileName = `${prefix}${segment.id}.${format}`
  const filePath = `${directory}/${fileName}`
  let assetUrl = segment.assetUrl || ''
  let assetSourceType = segment.assetSourceType || ClapAssetSource.EMPTY

  const isExportableToFile =
    (segment.category === ClapSegmentCategory.VIDEO ||
      segment.category === ClapSegmentCategory.STORYBOARD ||
      segment.category === ClapSegmentCategory.DIALOGUE ||
      segment.category === ClapSegmentCategory.SOUND ||
      segment.category === ClapSegmentCategory.MUSIC) &&
    format !== 'unknown' &&
    segment.assetUrl.startsWith('data:')

  const category = segment.category.toLocaleLowerCase()

  return {
    id: segment.id,
    segment,
    category,
    shortId: `${category}${index}`,
    directory,
    index,
    prefix,
    mimetype,
    format,
    filePath,
    fileName,
    assetUrl,
    assetSourceType,
    isExportableToFile,
  }
}