File size: 5,565 Bytes
520fa9c
 
f75bd44
520fa9c
ec03fd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520fa9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f75bd44
 
 
 
 
 
 
 
 
5a2ba2c
 
 
 
 
 
 
 
e1e42ea
403f72c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2d9ad7
 
 
403f72c
 
 
 
 
e1e42ea
403f72c
 
 
e1e42ea
403f72c
 
 
 
 
 
 
e1e42ea
403f72c
 
 
 
 
 
 
 
 
 
 
 
1ccdfb1
 
 
e1e42ea
 
99ead46
 
 
 
 
e1e42ea
ec03fd1
 
 
 
 
 
 
e1e42ea
ec03fd1
 
 
 
 
 
 
 
 
 
f75bd44
ec03fd1
f75bd44
ec03fd1
f75bd44
ec03fd1
f75bd44
ec03fd1
f75bd44
 
 
ec03fd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { Transcript } from "common-utils";
import _ from "lodash";
import { random, staticFile, useCurrentFrame } from "remotion";
import { DisplayMedia } from "./ig-reel/IGSimpleScene";


function levenshteinDistance(str1: string, str2: string) {
  const dp = Array.from({ length: str1.length + 1 }, (_, i) => [i]);

  for (let j = 1; j <= str2.length; j++) {
    dp[0][j] = j;
  }

  for (let i = 1; i <= str1.length; i++) {
    for (let j = 1; j <= str2.length; j++) {
      const cost = str1[i - 1] === str2[j - 1] ? 0 : 1;
      dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost);
    }
  }

  return dp[str1.length][str2.length];
}
export class RenderUtils {

  public static calculateDisplayMedia(
    item: Transcript,
    durationInSeconds: number,
    frame: number,
    fps: number) {
    let currentDisplayMedia: DisplayMedia = (item?.mediaAbsPaths as any || [])[0]
    if (!_.isEmpty(item?.mediaAbsPaths)) {
      let frameOffest = 0
      for (let i = 0; i < item?.mediaAbsPaths.length; i++) {
        const media: DisplayMedia = item.mediaAbsPaths[i] as any;
        if (!media) {
          return undefined
        }
        media.idx = i
        media.startFrame = frameOffest
        if (media.durationSec == undefined) {
          media.durationSec = durationInSeconds / (item?.mediaAbsPaths.length)
          if (i == item.mediaAbsPaths.length - 1) {
            media.durationSec = durationInSeconds
          }
        }
        media.durationFrames = media.durationSec * fps
        media.endFrame = frameOffest + media.durationFrames
        frameOffest = frameOffest + media.durationFrames
        if (frame > media.startFrame && frame < media.endFrame) {
          currentDisplayMedia = media
        }
      }

    }
    return currentDisplayMedia
  }
  public static tryStaticFile(file: string) {
    try {
      return staticFile(file);
    } catch (e) {
      let fileName = RenderUtils.getFileName(file)
      return staticFile(fileName!!);
    }
  }

  public static convertVHToPixels(vh: string) {
    return Math.round(window.innerHeight * (parseInt(vh.replace("vh", "")) / 100));
  }

  public static convertVWToPixels(vw: string) {
    return Math.round(window.innerWidth * (parseInt(vw.replace("vw", "")) / 100));
  }

  public static splitWordsIntoGroups(words: any) {
    const groups = [];
    let currentGroup = [];

    for (const word of words) {
      if (currentGroup.length === 0) {
        currentGroup.push(word);
      } else if (word.sentence_end) {
        currentGroup.push(word);
        groups.push(currentGroup);
        currentGroup = [];
      } else if (currentGroup.length < 5) {
        currentGroup.push(word);
      } else {
        groups.push(currentGroup);
        currentGroup = [word];
      }
    }

    if (currentGroup.length > 0) {
      groups.push(currentGroup);
    }

    // Ensure each group has a maximum of 5 words
    const finalGroups = [];
    for (const group of groups) {
      while (group.length > 5) {
        finalGroups.push(group.slice(0, 5));
        group.splice(0, 5);
      }
      finalGroups.push(group);
    }

    return finalGroups?.map(group => {
      return {
        words: group,
        start: undefined,
        end: undefined
      }
    });
  }


  public static findCurrentGroupByTime(timeSec: Number, groups: any[]) {
    var t = timeSec;
    let wds = groups
    for (let i = 0; i < wds.length; i++) {
      const x: any = wds[i];
      if (x.start != undefined && x.end != undefined && (t >= x.start && t < x.end)) {
        return x
      }
    }
  }


  public static findCurrentWord(timeSec: Number, frames: any[]) {
    var t = timeSec;
    let wds = frames
    for (let i = 0; i < wds.length; i++) {
      const x = wds[i];
      if (x.start && x.end && (t >= x.start && t < x.end)) {
        return x
      }
    }
  }


  public static randomElement<T>(arr: T[], seed: string): T {
    return arr[Math.floor(random(seed || 1) * arr.length)];
  }

  public static hashCode(s: string) {
    return s.split("").reduce(function (a: number, b: string) {
      a = ((a << 5) - a) + b.charCodeAt(0);
      return a & a;
    }, 0);
  }

  public static splitArrayIntoChunks(arr: string | any[], chunkSize: number) {
    const chunkedArr = [];
    for (let i = 0; i < arr.length; i += chunkSize) {
      chunkedArr.push(arr.slice(i, i + chunkSize));
    }
    return chunkedArr;
  }

  public static splitArray(arr: string | any[], pieces: number) {
    let result = [];
    let chunkSize = Math.ceil(arr.length / pieces);
    for (let i = 0, j = arr.length; i < j; i += chunkSize) {
      result.push(arr.slice(i, i + chunkSize));
    }
    return result;
  }

  public static getFileName(filePath: string, subdirpath?: string): string | undefined {
    if (!filePath)
      return undefined;
    if (filePath.startsWith("http"))
      return filePath;

    // Always extract just the filename from any path
    const match = filePath.match(/([^\/\\]+)$/);
    let filename = match ? match[1] : '';
    if (subdirpath) {
      return subdirpath + "/" + filename;
    } else {
      return filename;
    }
  }

  public static findMostSimilarString(inputString: string, stringArray: string[]) {
    let minDistance = Number.MAX_SAFE_INTEGER;
    let mostSimilarString = '';

    stringArray.forEach((str) => {
      const distance = levenshteinDistance(inputString, str);
      if (distance < minDistance) {
        minDistance = distance;
        mostSimilarString = str;
      }
    });

    return mostSimilarString;
  }

}