File size: 13,817 Bytes
91d209c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
371
372
373
374
375
376
import { useState } from 'react';
import { motion } from 'framer-motion';
import { useGeneration } from '@/context/GenerationContext';
import { CheckIcon, DownloadIcon, PlayIcon, RefreshIcon, VideoIcon } from './Icons';
import { mergeVideos, ClipMetadata } from '@/utils/api';

export const GenerationComplete: React.FC = () => {
  const { state, reset } = useGeneration();
  const { generatedVideos, provider } = state;
  const [playingIndex, setPlayingIndex] = useState<number | null>(null);
  const [isMerging, setIsMerging] = useState(false);
  const [mergeError, setMergeError] = useState<string | null>(null);
  const [mergedVideoUrl, setMergedVideoUrl] = useState<string | null>(null);
  const [isPlayingMerged, setIsPlayingMerged] = useState(false);

  const accentColor = provider === 'kling' ? 'coral' : 'electric';

  const handleDownload = async (video: typeof generatedVideos[0], index: number) => {
    try {
      const url = video.blobUrl || video.url;
      const response = await fetch(url);
      const blob = await response.blob();
      
      const downloadUrl = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = downloadUrl;
      a.download = `video-segment-${index + 1}.mp4`;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(downloadUrl);
    } catch (error) {
      console.error('Download failed:', error);
    }
  };

  const handleDownloadAll = async () => {
    for (let i = 0; i < generatedVideos.length; i++) {
      await handleDownload(generatedVideos[i], i);
      // Small delay between downloads
      await new Promise(r => setTimeout(r, 500));
    }
  };

  // Merge all videos into a single file
  const handleMergeAndExport = async () => {
    if (generatedVideos.length === 0) return;

    setIsMerging(true);
    setMergeError(null);

    try {
      // Collect all video blobs
      const videoBlobs: Blob[] = [];
      const clipMetadata: ClipMetadata[] = [];

      for (let i = 0; i < generatedVideos.length; i++) {
        const video = generatedVideos[i];
        const url = video.blobUrl || video.url;
        
        // Fetch blob from URL
        const response = await fetch(url);
        const blob = await response.blob();
        videoBlobs.push(blob);

        // Create clip metadata
        // Use Whisper-detected trim point if available, otherwise use full duration
        // No start trimming - keep full video from beginning
        const trimStart = 0; // Always start from beginning (no overlap removal)
        const trimEnd = video.trimPoint || video.duration; // Use Whisper trim point if available
        
        clipMetadata.push({
          index: i,
          startTime: trimStart,
          endTime: trimEnd,
          type: 'video',
          duration: trimEnd - trimStart,
        });
      }

      console.log('🎬 Merging videos...', clipMetadata);

      // Call merge API
      const mergedBlob = await mergeVideos(videoBlobs, clipMetadata);

      // Create URL for preview (don't auto-download)
      const previewUrl = URL.createObjectURL(mergedBlob);
      setMergedVideoUrl(previewUrl);

      console.log('✅ Merged video ready for preview!');

    } catch (error) {
      console.error('Merge failed:', error);
      setMergeError(error instanceof Error ? error.message : 'Failed to merge videos');
    } finally {
      setIsMerging(false);
    }
  };

  // Download the merged video
  const handleDownloadMerged = () => {
    if (!mergedVideoUrl) return;
    
    const a = document.createElement('a');
    a.href = mergedVideoUrl;
    a.download = `final-video-${Date.now()}.mp4`;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  };

  return (
    <motion.div
      initial={{ opacity: 0 }}
      animate={{ opacity: 1 }}
      className="min-h-[60vh] flex flex-col items-center justify-center p-8"
    >
      <div className="max-w-4xl w-full">
        {/* Success Header */}
        <div className="text-center mb-12">
          <motion.div
            initial={{ scale: 0 }}
            animate={{ scale: 1 }}
            transition={{ type: 'spring', duration: 0.5 }}
            className={`
              w-20 h-20 mx-auto rounded-full flex items-center justify-center mb-6
              ${accentColor === 'coral' ? 'bg-coral-500/20' : 'bg-electric-500/20'}
            `}
          >
            <CheckIcon 
              size={40} 
              className={accentColor === 'coral' ? 'text-coral-400' : 'text-electric-400'} 
            />
          </motion.div>
          
          <motion.h1
            initial={{ opacity: 0, y: 20 }}
            animate={{ opacity: 1, y: 0 }}
            transition={{ delay: 0.2 }}
            className="text-4xl font-display font-bold mb-4"
          >
            <span className={accentColor === 'coral' ? 'gradient-text' : 'gradient-text-electric'}>
              Generation Complete!
            </span>
          </motion.h1>
          
          <motion.p
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            transition={{ delay: 0.3 }}
            className="text-void-400 text-lg"
          >
            {generatedVideos.length} video{generatedVideos.length !== 1 ? 's' : ''} generated successfully
          </motion.p>
        </div>

        {/* Video Grid */}
        <motion.div
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ delay: 0.4 }}
          className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6 mb-8"
        >
          {generatedVideos.map((video, index) => (
            <div
              key={video.id}
              className="card group relative overflow-hidden"
            >
              {/* Video Preview */}
              <div className="relative aspect-[9/16] bg-void-950 rounded-lg overflow-hidden mb-4">
                <video
                  src={video.blobUrl || video.url}
                  className="w-full h-full object-cover"
                  controls={playingIndex === index}
                  poster={video.thumbnails[0]}
                  onEnded={() => setPlayingIndex(null)}
                />
                
                {playingIndex !== index && (
                  <button
                    onClick={() => setPlayingIndex(index)}
                    className="absolute inset-0 flex items-center justify-center bg-black/40 group-hover:bg-black/50 transition-colors"
                  >
                    <div className={`
                      w-14 h-14 rounded-full flex items-center justify-center
                      ${accentColor === 'coral' ? 'bg-coral-500' : 'bg-electric-500'}
                      group-hover:scale-110 transition-transform
                    `}>
                      <PlayIcon size={24} className="text-white ml-1" />
                    </div>
                  </button>
                )}
              </div>

              {/* Video Info */}
              <div className="flex items-center justify-between">
                <div>
                  <h3 className="font-semibold text-void-200">
                    Segment {index + 1}
                  </h3>
                  <p className="text-xs text-void-400">
                    ~{Math.round(video.duration)}s duration
                  </p>
                </div>
                <button
                  onClick={() => handleDownload(video, index)}
                  className={`
                    p-2 rounded-lg transition-colors
                    ${accentColor === 'coral' 
                      ? 'hover:bg-coral-500/20 text-coral-400' 
                      : 'hover:bg-electric-500/20 text-electric-400'
                    }
                  `}
                >
                  <DownloadIcon size={20} />
                </button>
              </div>
            </div>
          ))}
        </motion.div>

        {/* Merged Video Preview */}
        {mergedVideoUrl && (
          <motion.div
            initial={{ opacity: 0, y: 20 }}
            animate={{ opacity: 1, y: 0 }}
            className="mb-8"
          >
            <div className={`card border-2 ${accentColor === 'coral' ? 'border-coral-500/50 bg-coral-500/5' : 'border-electric-500/50 bg-electric-500/5'}`}>
              <div className="flex items-center gap-3 mb-4">
                <div className={`p-2 rounded-lg ${accentColor === 'coral' ? 'bg-coral-500/20' : 'bg-electric-500/20'}`}>
                  <VideoIcon size={24} className={accentColor === 'coral' ? 'text-coral-400' : 'text-electric-400'} />
                </div>
                <div>
                  <h3 className="font-bold text-lg text-void-100">Final Exported Video</h3>
                  <p className="text-sm text-void-400">All segments merged into one video</p>
                </div>
              </div>
              
              {/* Video Player */}
              <div className="relative aspect-[9/16] max-w-md mx-auto bg-void-950 rounded-xl overflow-hidden mb-4">
                <video
                  src={mergedVideoUrl}
                  className="w-full h-full object-contain"
                  controls={isPlayingMerged}
                  onEnded={() => setIsPlayingMerged(false)}
                />
                
                {!isPlayingMerged && (
                  <button
                    onClick={() => setIsPlayingMerged(true)}
                    className="absolute inset-0 flex items-center justify-center bg-black/40 hover:bg-black/50 transition-colors"
                  >
                    <div className={`
                      w-16 h-16 rounded-full flex items-center justify-center
                      ${accentColor === 'coral' ? 'bg-coral-500' : 'bg-electric-500'}
                      hover:scale-110 transition-transform shadow-lg
                    `}>
                      <PlayIcon size={28} className="text-white ml-1" />
                    </div>
                  </button>
                )}
              </div>
              
              {/* Download Button */}
              <button
                onClick={handleDownloadMerged}
                className={`
                  w-full flex items-center justify-center gap-2 py-3 font-semibold rounded-xl
                  ${accentColor === 'coral' ? 'btn-primary' : 'btn-electric'}
                `}
              >
                <DownloadIcon size={20} />
                Download Final Video
              </button>
            </div>
          </motion.div>
        )}

        {/* Merge Error */}
        {mergeError && (
          <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            className="mb-6 p-4 bg-red-500/10 border border-red-500/30 rounded-xl text-center"
          >
            <p className="text-red-300 text-sm">{mergeError}</p>
          </motion.div>
        )}

        {/* Actions */}
        <motion.div
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          transition={{ delay: 0.6 }}
          className="flex flex-col sm:flex-row items-center justify-center gap-4"
        >
          {/* Primary: Merge & Export */}
          {!mergedVideoUrl && (
            <button
              onClick={handleMergeAndExport}
              disabled={isMerging || generatedVideos.length < 2}
              className={`
                flex items-center gap-2 px-6 py-3 font-semibold rounded-xl
                ${accentColor === 'coral' ? 'btn-primary' : 'btn-electric'}
                disabled:opacity-50 disabled:cursor-not-allowed
              `}
            >
              {isMerging ? (
                <>
                  <div className="w-5 h-5 border-2 border-white/30 border-t-white rounded-full animate-spin" />
                  <span>Merging...</span>
                </>
              ) : (
                <>
                  <VideoIcon size={20} />
                  <span>Export Final Video</span>
                </>
              )}
            </button>
          )}
          
          {/* Re-merge option if already merged */}
          {mergedVideoUrl && (
            <button
              onClick={() => {
                URL.revokeObjectURL(mergedVideoUrl);
                setMergedVideoUrl(null);
                handleMergeAndExport();
              }}
              disabled={isMerging}
              className="btn-secondary flex items-center gap-2"
            >
              <RefreshIcon size={20} />
              Re-merge Video
            </button>
          )}

          {/* Secondary: Download All */}
          <button
            onClick={handleDownloadAll}
            className="btn-secondary flex items-center gap-2"
          >
            <DownloadIcon size={20} />
            Download Segments
          </button>
          
          {/* Tertiary: Generate More */}
          <button
            onClick={reset}
            className="btn-secondary flex items-center gap-2"
          >
            <RefreshIcon size={20} />
            Generate More
          </button>
        </motion.div>

        {/* Tip */}
        <motion.p
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          transition={{ delay: 0.8 }}
          className="text-center text-void-500 text-sm mt-8"
        >
          {mergedVideoUrl 
            ? 'Your final video is ready! Download it or re-merge with different settings.'
            : generatedVideos.length >= 2 
              ? '"Export Final Video" will merge all segments into a single video file with Whisper-optimized trim points.'
              : 'Videos are ready to use in your video editor or social media.'
          }
        </motion.p>
      </div>
    </motion.div>
  );
};