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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import path from 'path';
import fs from 'fs';
import { Plugin } from './plugin.js';
import { FFMpegUtils } from 'common-utils';
import { CaptionPlugin } from './generate-captions.js';

/**
 * Must come before the Captions plugin in the plugin list, since it modifies the transcript and bubbles before captions are generated.
 * The single static video plugin aims to join all the audio segments of a transcript into a single audio file. Then use the static video in the first section of the transcript as a placeholder for the entire transcript. This is useful for cases where the user wants to use a single static video for the entire transcript. Basically transforming all the sections into a single section with a single static video and a single audio file. The static video is used as a placeholder for the entire transcript, and the audio is used to play the entire transcript. The static video is not modified in any way, it is simply used as a placeholder for the entire transcript. The audio is generated by concatenating all the audio segments of the transcript into a single audio file. The output of this plugin is a modified transcript with a single section containing the static video and the concatenated audio file. The bubbles across all sections are also moved to the single combined section by reading each section duration, bubble duration and addind appropriate offsets to the bubble start and end times.  The output of this plugin is a modified transcript with a single section containing the static video and the concatenated audio file, and the bubbles across all sections are also moved to the single combined section by reading each section duration, bubble duration and adding appropriate offsets to the bubble start and end times. The bubbles are also sorted by their start time. Transitions are removed during this process.
 */
export class SingleStaticVideoPlugin extends Plugin {
  constructor(name, options) {
    super(name, options);
  }


  async applyPrerender(originalManuscript, jobId) {
    const transcript = originalManuscript.transcript || [];
    const originalManuscriptMeta = originalManuscript.meta || [];
    const targetWidth = +this.options.width || +this.options.targetWidth || originalManuscriptMeta?.resolution?.width || 1080;
    const targetHeight = +this.options.height || +this.options.targetHeight || originalManuscriptMeta?.resolution?.height || 1920;
    const targetAspect = targetWidth / targetHeight;
    let firstPlaceholderMedia = undefined
    let firstPlaceholderMeta = undefined

    let combinedAudioPath = path.join('public', `combined-audio-${jobId}.mp3`)
    let combinedAudioCaptionsFilePath = path.join('public', `combined-audio-captions-${jobId}.json`)
    let combinedBubbles = []
    let combinedMediaPath = undefined
    let combinedMediaMeta = undefined
    let allAudios = []

    for (let item of transcript) {

      let sectionidx = transcript.indexOf(item)
      if (!item.mediaAbsPaths || !item.mediaAbsPaths.length) continue;
      allAudios.push(item.audioFullPath)


      for (let mediaObj of item.mediaAbsPaths) {
        if (!combinedMediaPath) {
          // We only need to process the first section with media, since we are using the first media from the first section for single static video for the entire transcript
          let mediaIdx = item.mediaAbsPaths.indexOf(mediaObj)
          try {
            let mediaPath = mediaObj.path;
            if (!mediaPath || !fs.existsSync(mediaPath)) {
              const flattenedPath = this.mediaPathFlatten(mediaPath);
              if (fs.existsSync(flattenedPath)) {
                mediaObj.path = flattenedPath;
                mediaPath = flattenedPath;
                this.log(`Using flattened media path: ${flattenedPath}`);
              } else {
                this.log(`Media path does not exist: ${mediaPath}. Trying at flattened path: ${flattenedPath} failed.`);
                continue;
              }
            }
            // The first media path is used as the placeholder for the entire transcript. This is useful for cases where the user wants to use a single static video for the entire transcript
            if (!firstPlaceholderMedia) {
              firstPlaceholderMedia = mediaPath
              firstPlaceholderMeta = await FFMpegUtils.getMediaMetadata(mediaPath);
              if (!firstPlaceholderMeta || !firstPlaceholderMeta.video || !firstPlaceholderMeta.video.width || !firstPlaceholderMeta.video.height) {
                this.log(`No video stream found for ${firstPlaceholderMedia}`);
                continue;
              }
            }

            combinedMediaMeta = firstPlaceholderMeta
            const ext = path.extname(mediaPath);
            const base = path.basename(mediaPath, ext);
            combinedMediaPath = path.join(path.dirname(mediaPath), `combined-static-${sectionidx}-${mediaIdx}${ext}`);

            mediaObj.path = combinedMediaPath;
          } catch (err) {
            this.log(`Error cropping media ${mediaObj?.path}: ${err}`);
          }
        }
      }
    }

    combinedBubbles = await this.combineAllTranscriptBubbles(transcript, {
      contagious: this.options.contagious || false,
      contagiousDelay: this.options.contagiousDelay ?? 0
    })
    await Promise.all([
      this.combineAllAudioCaptions(transcript, combinedAudioCaptionsFilePath),
      this.combineAllAudios(transcript, combinedAudioPath)
    ])

    // Calculate total duration across all sections
    let totalDuration = 0;
    for (let item of transcript) {
      totalDuration += item.durationInSeconds || 0;
    }

    await this.combineFirstPlaceholderMedia(firstPlaceholderMedia, combinedAudioPath, combinedMediaPath, totalDuration, firstPlaceholderMeta);
    // Build the single combined section from the first section as a template
    const firstSection = transcript[0] || {};
    const combinedSection = {
      ...firstSection,
      text: transcript.map(s => s.text || '').join(' '),
      index: 0,
      mediaAbsPaths: combinedMediaPath ? [{
        path: combinedMediaPath,
        type: firstSection.mediaAbsPaths?.[0]?.type || 'video',
        dimensions: combinedMediaMeta?.video ? {
          width: parseInt(combinedMediaMeta.video.width),
          height: parseInt(combinedMediaMeta.video.height)
        } : undefined,
        durationSec: totalDuration
      }] : [],
      bubbles: combinedBubbles,
      audioFullPath: combinedAudioPath,
      audioCaptionFile: combinedAudioCaptionsFilePath,
      durationInSeconds: totalDuration,
      duration: Math.ceil(totalDuration * (originalManuscriptMeta?.fps || 30)),
      offset: 0,
      // Remove transitions since everything is merged into one section
      transition_type: 'none',
      transition_file: undefined,
      transition_duration_sec: 0
    };

    // Replace the transcript with the single combined section
    originalManuscript.transcript = [combinedSection];

    this.log(`Combined ${transcript.length} sections into 1 static-video section. Total duration: ${totalDuration.toFixed(2)}s, Bubbles: ${combinedBubbles.length}`);
  }

  async combineFirstPlaceholderMedia(inputPath, audioPath, outputPath, totalDuration, inputMeta = null) {
    if (!inputPath || !fs.existsSync(inputPath)) {
      this.log(`Placeholder media not found: ${inputPath}`);
      return;
    }

    this.log(`Clipping/Looping placeholder media ${inputPath} to ${totalDuration}s -> ${outputPath}`);
    try {
      const ext = path.extname(inputPath).toLowerCase();
      const isImage = ['.png', '.jpg', '.jpeg', '.webp'].includes(ext);

      let cmd = '';
      if (isImage) {
        cmd = `ffmpeg -loop 1 -i "${inputPath}" -i "${audioPath}" -t ${totalDuration} -c:v libx264 -preset veryfast -crf 23 -pix_fmt yuv420p -c:a aac -map 0:v -map 1:a "${outputPath}" -y`;
      } else {
        let inputDuration = 0;
        if (inputMeta && inputMeta.format && inputMeta.format.duration) {
          inputDuration = parseFloat(inputMeta.format.duration);
        } else {
          try {
            const meta = await FFMpegUtils.getMediaMetadata(inputPath);
            inputDuration = parseFloat(meta?.format?.duration || 0);
          } catch (e) {
            this.log(`Could not fetch metadata for duration: ${e}`);
          }
        }

        if (inputDuration > totalDuration) {
          const startTime = Math.max(0, inputDuration - totalDuration);
          cmd = `ffmpeg -ss ${startTime} -i "${inputPath}" -i "${audioPath}" -t ${totalDuration} -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 "${outputPath}" -y`;
        } else {
          cmd = `ffmpeg -stream_loop -1 -i "${inputPath}" -i "${audioPath}" -t ${totalDuration} -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 "${outputPath}" -y`;
        }
      }

      await FFMpegUtils.execute(cmd);
    } catch (err) {
      this.log(`Error in combineFirstPlaceholderMedia: ${err}`);
      // fallback to copy if ffmpeg fails
      fs.copyFileSync(inputPath, outputPath);
    }
  }

  // Takes all transcripts and returns a single array with bubbles from different sections put into a single array and sorted by start time. The bubbles are also adjusted to have the correct start and end times based on the section duration and bubble duration.  Return array of bubbles.
  // Options:
  //   contagious {boolean} - if true, each bubble[N+1].fromSec is forced to bubble[N].toSec + contagiousDelay after sorting
  //   contagiousDelay {number} - gap in seconds between consecutive bubbles when contagious=true (default: 0)
  async combineAllTranscriptBubbles(transcripts, { contagious = false, contagiousDelay = 0 } = {}) {
    let combinedBubbles = [];
    let cumulativeOffset = 0;

    for (let section of transcripts) {
      const sectionDuration = section.durationInSeconds || 0;
      const bubbles = section.bubbles || [];

      for (let bubble of bubbles) {
        // Deep clone the bubble so we don't mutate the original
        const adjustedBubble = JSON.parse(JSON.stringify(bubble));

        // Adjust timing with cumulative offset from previous sections
        if (adjustedBubble.fromSec !== undefined && adjustedBubble.fromSec !== null) {
          adjustedBubble.fromSec = adjustedBubble.fromSec + cumulativeOffset;
        }
        if (adjustedBubble.toSec !== undefined && adjustedBubble.toSec !== null) {
          adjustedBubble.toSec = adjustedBubble.toSec + cumulativeOffset;
        }

        combinedBubbles.push(adjustedBubble);
      }

      cumulativeOffset += sectionDuration;
    }

    // Sort bubbles by their start time
    combinedBubbles.sort((a, b) => (a.fromSec || 0) - (b.fromSec || 0));

    // Apply contagious chaining: bubble[N+1].fromSec = bubble[N].toSec + contagiousDelay
    if (contagious && combinedBubbles.length > 1) {
      this.log(`Applying contagious chaining with delay=${contagiousDelay}s across ${combinedBubbles.length} bubbles.`);
      for (let i = 1; i < combinedBubbles.length; i++) {
        const prev = combinedBubbles[i - 1];
        if (prev.toSec !== undefined && prev.toSec !== null) {
          const newFromSec = prev.toSec + contagiousDelay;
          if (combinedBubbles[i].fromSec !== undefined && combinedBubbles[i].toSec !== undefined) {
            // Preserve original duration, shift the window forward
            const originalDur = combinedBubbles[i].toSec - combinedBubbles[i].fromSec;
            combinedBubbles[i].fromSec = newFromSec;
            combinedBubbles[i].toSec = Math.round((newFromSec + originalDur) * 1000) / 1000;
          } else {
            combinedBubbles[i].fromSec = newFromSec;
          }
          // Mirror into mediaTextPrompts if present
          if (combinedBubbles[i].mediaTextPrompts?.length) {
            combinedBubbles[i].mediaTextPrompts[0].fromSec = combinedBubbles[i].fromSec;
            combinedBubbles[i].mediaTextPrompts[0].toSec = combinedBubbles[i].toSec;
            if (combinedBubbles[i].durationSec !== undefined) {
              combinedBubbles[i].durationSec = Math.round((combinedBubbles[i].toSec - combinedBubbles[i].fromSec) * 1000) / 1000;
              combinedBubbles[i].mediaTextPrompts[0].durationSec = combinedBubbles[i].durationSec;
            }
          }
        }
      }
    }

    this.log(`Combined ${combinedBubbles.length} bubbles from ${transcripts.length} sections`);
    return combinedBubbles;
  }

  // Combines all audio files from all transcripts into a single audio file.  The combined audio file is saved to the specified output path.
  async combineAllAudios(transcripts, outAudioPath) {
    // flatten path first for each audio before processing this.mediaPathFlatten(..);
    const audioPaths = [];

    for (let section of transcripts) {
      let audioPath = section.audioFullPath;
      if (!audioPath) continue;

      // Try the original path first, then fall back to flattened
      if (!fs.existsSync(audioPath)) {
        const flattenedPath = this.mediaPathFlatten(audioPath);
        if (fs.existsSync(flattenedPath)) {
          audioPath = flattenedPath;
          this.log(`Using flattened audio path: ${flattenedPath}`);
        } else {
          this.log(`Audio path does not exist: ${audioPath}. Flattened path ${flattenedPath} also not found. Skipping.`);
          continue;
        }
      }

      audioPaths.push(audioPath);
    }

    if (audioPaths.length === 0) {
      this.log('No audio files found to combine.');
      return;
    }

    if (audioPaths.length === 1) {
      // Only one audio file, just copy it
      fs.copyFileSync(audioPaths[0], outAudioPath);
      this.log(`Single audio file copied to ${outAudioPath}`);
      return;
    }

    this.log(`Joining ${audioPaths.length} audio files into ${outAudioPath}`);
    await FFMpegUtils.joinAudios(audioPaths, outAudioPath);
    this.log(`Combined audio saved to ${outAudioPath}`);
  }

  // Combines all audio captions for all transcripts into a single audio caption file taking care of offset. The combined audio caption file is saved to the specified output path.
  async combineAllAudioCaptions(transcripts, outAudioCaptionPath) {
    // flatten path first for each audio caption file before processing this.mediaPathFlatten(..);
    let combinedTranscriptText = '';
    let combinedWords = [];
    let cumulativeOffset = 0;

    for (let section of transcripts) {
      let captionFilePath = section.audioCaptionFile;
      if (!captionFilePath) {
        // No caption file for this section, just advance the offset
        cumulativeOffset += section.durationInSeconds || 0;
        continue;
      }

      // If the caption file is an .ass file, check for the original JSON source
      if (path.extname(captionFilePath) === '.ass') {
        // Try the _audioCaptionFile (original JSON before ASS conversion) if available
        if (section._audioCaptionFile) {
          captionFilePath = section._audioCaptionFile;
        } else {
          // Try converting .ass path back to .json
          captionFilePath = captionFilePath.replace('.ass', '.json');
        }
      }

      // Resolve the caption file path
      let resolvedPath = captionFilePath;
      if (!fs.existsSync(resolvedPath)) {
        // Try with absolute path from cwd
        resolvedPath = path.join(process.cwd(), captionFilePath);
      }
      if (!fs.existsSync(resolvedPath)) {
        // Try flattened path
        const flattenedPath = this.mediaPathFlatten(captionFilePath);
        if (fs.existsSync(flattenedPath)) {
          resolvedPath = flattenedPath;
          this.log(`Using flattened caption path: ${flattenedPath}`);
        } else {
          const flattenedAbsPath = path.join(process.cwd(), flattenedPath);
          if (fs.existsSync(flattenedAbsPath)) {
            resolvedPath = flattenedAbsPath;
            this.log(`Using flattened absolute caption path: ${flattenedAbsPath}`);
          } else {
            this.log(`Caption file not found: ${captionFilePath}. Skipping.`);
            cumulativeOffset += section.durationInSeconds || 0;
            continue;
          }
        }
      }

      try {
        const captionData = JSON.parse(fs.readFileSync(resolvedPath, 'utf-8'));
        const sectionTranscript = captionData.transcript || '';
        const sectionWords = captionData.words || [];

        // Append transcript text with a space separator
        if (combinedTranscriptText.length > 0 && sectionTranscript.length > 0) {
          combinedTranscriptText += ' ';
        }
        combinedTranscriptText += sectionTranscript;

        // Adjust word timings with cumulative offset and add to combined list
        for (let word of sectionWords) {
          const adjustedWord = { ...word };
          if (adjustedWord.start !== undefined && adjustedWord.start !== null) {
            adjustedWord.start = adjustedWord.start + cumulativeOffset;
          }
          if (adjustedWord.end !== undefined && adjustedWord.end !== null) {
            adjustedWord.end = adjustedWord.end + cumulativeOffset;
          }
          combinedWords.push(adjustedWord);
        }
      } catch (err) {
        this.log(`Error reading caption file ${resolvedPath}: ${err}`);
      }

      cumulativeOffset += section.durationInSeconds || 0;
    }

    // Write the combined caption file
    const combinedCaptions = {
      transcript: combinedTranscriptText,
      words: combinedWords
    };

    fs.writeFileSync(outAudioCaptionPath, JSON.stringify(combinedCaptions, null, 2));
    this.log(`Combined ${combinedWords.length} caption words from ${transcripts.length} sections into ${outAudioCaptionPath}`);
  }


}