File size: 12,133 Bytes
d41a187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3655da
d41a187
 
 
 
 
 
 
 
 
 
 
 
 
 
758e72e
d41a187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3655da
d41a187
 
 
 
 
a3655da
 
d41a187
 
a3655da
 
d41a187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Magic Cut - Video Face Splitter</title>
    <style>
        :root {
            --primary: #a855f7;
            --bg: #0f0f1a;
            --surface: #1a1a2e;
            --text: #f3f4f6;
        }

        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: var(--bg);
            color: var(--text);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            padding: 1rem;
        }

        .container {
            background: var(--surface);
            padding: 2rem;
            border-radius: 16px;
            width: 100%;
            max-width: 600px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
            border: 1px solid #2a2a4a;
        }

        h2 {
            margin-top: 0;
            text-align: center;
            background: linear-gradient(135deg, #a855f7, #ec4899);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-size: 1.8rem;
        }

        h4 {
            margin: 0;
            color: #9ca3af;
            text-align: center;
            font-weight: 400;
            margin-bottom: 1.5rem;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            font-size: 0.9rem;
            color: #d1d5db;
        }

        input,
        textarea {
            width: 100%;
            padding: 0.75rem;
            background: #0f0f1a;
            border: 1px solid #374151;
            border-radius: 8px;
            color: white;
            box-sizing: border-box;
            font-family: inherit;
        }

        input:focus,
        textarea:focus {
            outline: 2px solid var(--primary);
            border-color: transparent;
        }

        button {
            width: 100%;
            padding: 0.875rem;
            background: linear-gradient(135deg, #a855f7, #ec4899);
            color: white;
            border: none;
            border-radius: 8px;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s;
            font-size: 1rem;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(168, 85, 247, 0.3);
        }

        button:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        #statusBox {
            margin-top: 2rem;
            display: none;
            background: #0f0f1a;
            padding: 1.5rem;
            border-radius: 12px;
            border: 1px solid #374151;
        }

        .status-badge {
            display: inline-block;
            padding: 6px 14px;
            border-radius: 99px;
            font-size: 0.8rem;
            font-weight: 600;
            margin-bottom: 1rem;
        }

        .status-badge.queued {
            background: #f59e0b;
            color: black;
        }

        .status-badge.processing {
            background: #3b82f6;
            color: white;
        }

        .status-badge.completed {
            background: #10b981;
            color: black;
        }

        .status-badge.failed {
            background: #ef4444;
            color: white;
        }

        #progressText {
            color: #d1d5db;
            margin-bottom: 1rem;
            font-size: 0.95rem;
        }

        .result-box {
            background: #1a1a2e;
            padding: 1rem;
            border-radius: 8px;
            margin-top: 1rem;
        }

        .result-url {
            word-break: break-all;
            font-size: 0.85rem;
            color: var(--primary);
            margin-bottom: 0.5rem;
        }

        .copy-btn {
            background: #374151;
            border: none;
            color: white;
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 0.85rem;
            width: auto;
            margin-top: 0.5rem;
        }

        .copy-btn:hover {
            background: #4b5563;
            transform: none;
            box-shadow: none;
        }

        .spinner {
            border: 4px solid #374151;
            border-top: 4px solid var(--primary);
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto 1rem auto;
            display: none;
        }

        @keyframes spin {
            0% {
                transform: rotate(0deg);
            }

            100% {
                transform: rotate(360deg);
            }
        }

        .info-box {
            background: rgba(168, 85, 247, 0.1);
            border: 1px solid rgba(168, 85, 247, 0.3);
            border-radius: 8px;
            padding: 1rem;
            margin-bottom: 1.5rem;
            font-size: 0.85rem;
            color: #d1d5db;
        }

        .segments-info {
            margin-top: 1rem;
            font-size: 0.85rem;
            color: #9ca3af;
        }

        video {
            width: 100%;
            max-height: 400px;
            border-radius: 8px;
            margin-top: 1rem;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2>✂️ Magic Cut</h2>
        <h4>Transform 16:9 videos into vertical shorts with face tracking</h4>

        <div class="info-box">
            <strong>How it works:</strong><br>
            1. Paste your Cloudinary video URL with <code>so_X,du_Y</code> (start time, duration)<br>
            2. We analyze each frame for faces (every 500ms)<br>
            3. When 2+ faces detected → split-screen layout<br>
            4. Get your final 9:16 video URL!
        </div>

        <div class="form-group">
            <label>1. Your HF Space URL (Direct link)</label>
            <input type="text" id="serverUrl" value="https://adxabhi-magic-cut.hf.space"
                placeholder="https://username-space-name.hf.space">
            <small style="color: #6b7280; display: block; margin-top: 4px;">Found in Space > Embed this Space > Direct
                URL</small>
        </div>

        <div class="form-group">
            <label>2. Cloudinary Video URL</label>
            <textarea id="videoUrl" rows="3"
                placeholder="https://res.cloudinary.com/doxoms9hd/video/upload/so_55,du_30/fl_getinfo/video_id.jpg"></textarea>
            <small style="color: #6b7280; display: block; margin-top: 4px;">
                Format: so_X,du_Y (start at X seconds, duration Y seconds)
            </small>
        </div>

        <button id="processBtn" onclick="submitJob()">🎬 Process Video</button>

        <div id="statusBox">
            <div id="spinner" class="spinner"></div>
            <span id="statusBadge" class="status-badge">Waiting</span>
            <div id="progressText">Initializing...</div>
            <div id="resultBox"></div>
        </div>
    </div>

    <script>
        let pollInterval = null;

        async function submitJob() {
            let serverUrl = document.getElementById('serverUrl').value.trim();
            serverUrl = serverUrl.replace(/\/$/, "");

            const videoUrl = document.getElementById('videoUrl').value.trim();
            const btn = document.getElementById('processBtn');
            const statusBox = document.getElementById('statusBox');

            if (!serverUrl || !videoUrl) {
                alert("Please fill in both fields");
                return;
            }

            btn.disabled = true;
            statusBox.style.display = 'block';
            document.getElementById('resultBox').innerHTML = '';
            updateStatus("queued", "Submitting job...");

            try {
                const response = await fetch(`${serverUrl}/jobs`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ video_url: videoUrl })
                });

                const data = await response.json();

                if (data.job_id) {
                    console.log("Job Submitted:", data.job_id);
                    startPolling(serverUrl, data.job_id);
                } else {
                    updateStatus("failed", "Failed to get Job ID");
                    btn.disabled = false;
                }

            } catch (error) {
                console.error(error);
                updateStatus("failed", "Connection Error. Check URL.");
                btn.disabled = false;
            }
        }

        function startPolling(serverUrl, jobId) {
            if (pollInterval) clearInterval(pollInterval);

            pollInterval = setInterval(async () => {
                try {
                    const res = await fetch(`${serverUrl}/jobs/${jobId}`);
                    const job = await res.json();

                    updateStatus(job.status, job.progress);

                    if (job.status === 'completed') {
                        clearInterval(pollInterval);
                        showResults(job.result);
                        document.getElementById('processBtn').disabled = false;
                    }

                    if (job.status === 'failed') {
                        clearInterval(pollInterval);
                        document.getElementById('progressText').innerText = "Error: " + job.error;
                        document.getElementById('processBtn').disabled = false;
                    }

                } catch (e) {
                    console.error("Polling error", e);
                }
            }, 2000);
        }

        function updateStatus(status, message) {
            const badge = document.getElementById('statusBadge');
            const spinner = document.getElementById('spinner');
            const text = document.getElementById('progressText');

            badge.className = `status-badge ${status}`;
            badge.innerText = status.toUpperCase();
            text.innerText = message || "Processing...";

            if (status === 'processing' || status === 'queued') {
                spinner.style.display = 'block';
            } else {
                spinner.style.display = 'none';
            }
        }

        function showResults(result) {
            const box = document.getElementById('resultBox');
            const segments = result.multi_face_segments || [];

            let segmentsHtml = '';
            if (segments.length > 0) {
                segmentsHtml = `
                    <div class="segments-info">
                        <strong>🎭 Multi-face segments found:</strong><br>
                        ${segments.map((s, i) => `Segment ${i + 1}: ${s.start}s - ${s.end}s`).join('<br>')}
                    </div>
                `;
            } else {
                segmentsHtml = `<div class="segments-info">No multi-face segments detected (single speaker throughout)</div>`;
            }

            box.innerHTML = `
                <div class="result-box">
                    <div style="margin-bottom: 0.5rem; color: #10b981; font-weight: 600;">✅ Video Ready!</div>
                    <div class="result-url">${result.video_url}</div>
                    <button class="copy-btn" onclick="navigator.clipboard.writeText('${result.video_url}').then(() => this.innerText = 'Copied!')">
                        📋 Copy URL
                    </button>
                    ${segmentsHtml}
                    <div class="segments-info">
                        <strong>📊 Stats:</strong> ${result.total_frames_analyzed} frames analyzed
                    </div>
                    <video controls src="${result.video_url}"></video>
                </div>
            `;
        }
    </script>
</body>

</html>