seawolf2357 commited on
Commit
3c15f3d
ยท
verified ยท
1 Parent(s): 1c0dc91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +717 -1450
app.py CHANGED
@@ -1,6 +1,6 @@
1
  """
2
  Simple Video Editor - Canvas ๊ธฐ๋ฐ˜ ๋ Œ๋”๋ง
3
- ์›๋ณธ ํŒŒ์ผ ์„œ๋ฒ„ ์ €์žฅ + FFmpeg MP4 ๋‚ด๋ณด๋‚ด๊ธฐ
4
  """
5
 
6
  import gradio as gr
@@ -12,7 +12,7 @@ import tempfile
12
  import shutil
13
  import time
14
 
15
- # ์—…๋กœ๋“œ๋œ ํŒŒ์ผ ์ €์žฅ ๊ฒฝ๋กœ
16
  UPLOAD_DIR = tempfile.mkdtemp()
17
  uploaded_files = {} # {filename: filepath}
18
 
@@ -22,1329 +22,736 @@ def get_editor_html(media_data="[]"):
22
  <head>
23
  <meta charset="UTF-8">
24
  <style>
25
- * {{
26
- margin: 0;
27
- padding: 0;
28
- box-sizing: border-box;
29
- }}
30
- body {{
31
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
32
- background: #f5f5f7;
33
- font-size: 13px;
34
- }}
35
- .editor {{
36
- display: flex;
37
- flex-direction: column;
38
- height: 100vh;
39
- }}
40
- .toolbar {{
41
- height: 44px;
42
- background: #fff;
43
- border-bottom: 1px solid #ddd;
44
- display: flex;
45
- align-items: center;
46
- justify-content: space-between;
47
- padding: 0 12px;
48
- }}
49
- .toolbar-title {{
50
- font-size: 15px;
51
- font-weight: 600;
52
- }}
53
- .toolbar-actions {{
54
- display: flex;
55
- gap: 6px;
56
- }}
57
- .btn {{
58
- padding: 6px 12px;
59
- border: none;
60
- border-radius: 6px;
61
- cursor: pointer;
62
- font-size: 11px;
63
- font-weight: 500;
64
- transition: all 0.2s;
65
- }}
66
- .btn-secondary {{
67
- background: #f0f0f0;
68
- color: #333;
69
- }}
70
- .btn-secondary:hover {{
71
- background: #e0e0e0;
72
- }}
73
- .btn-primary {{
74
- background: #6366f1;
75
- color: #fff;
76
- }}
77
- .btn-primary:hover {{
78
- background: #4f46e5;
79
- }}
80
- .btn-success {{
81
- background: #10b981;
82
- color: #fff;
83
- }}
84
- .btn-success:hover {{
85
- background: #059669;
86
- }}
87
- .btn-danger {{
88
- background: #ef4444;
89
- color: #fff;
90
- }}
91
- .btn-danger:hover {{
92
- background: #dc2626;
93
- }}
94
- .main {{
95
- display: flex;
96
- flex: 1;
97
- overflow: hidden;
98
- }}
99
- .library {{
100
- width: 180px;
101
- background: #fff;
102
- border-right: 1px solid #ddd;
103
- display: flex;
104
- flex-direction: column;
105
- }}
106
- .lib-header {{
107
- padding: 10px 12px;
108
- border-bottom: 1px solid #eee;
109
- font-size: 11px;
110
- font-weight: 600;
111
- color: #666;
112
- text-transform: uppercase;
113
- letter-spacing: 0.5px;
114
- }}
115
- .lib-content {{
116
- flex: 1;
117
- overflow-y: auto;
118
- padding: 8px;
119
- }}
120
- .lib-hint {{
121
- text-align: center;
122
- padding: 20px;
123
- color: #999;
124
- font-size: 11px;
125
- }}
126
- .media-grid {{
127
- display: grid;
128
- grid-template-columns: repeat(2, 1fr);
129
- gap: 6px;
130
- }}
131
- .media-item {{
132
- aspect-ratio: 16/9;
133
- background: #f0f0f0;
134
- border-radius: 6px;
135
- overflow: hidden;
136
- cursor: grab;
137
- position: relative;
138
- border: 2px solid transparent;
139
- transition: all 0.2s;
140
- }}
141
- .media-item:hover {{
142
- border-color: #6366f1;
143
- transform: scale(1.02);
144
- }}
145
- .media-item img {{
146
- width: 100%;
147
- height: 100%;
148
- object-fit: cover;
149
- }}
150
- .media-item-icon {{
151
- width: 100%;
152
- height: 100%;
153
- display: flex;
154
- align-items: center;
155
- justify-content: center;
156
- font-size: 20px;
157
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
158
- }}
159
- .media-item-dur {{
160
- position: absolute;
161
- bottom: 3px;
162
- right: 3px;
163
- background: rgba(0,0,0,0.75);
164
- padding: 2px 5px;
165
- border-radius: 3px;
166
- font-size: 9px;
167
- color: #fff;
168
- font-weight: 500;
169
- }}
170
- .media-item-name {{
171
- position: absolute;
172
- bottom: 3px;
173
- left: 3px;
174
- right: 35px;
175
- background: rgba(0,0,0,0.75);
176
- padding: 2px 5px;
177
- border-radius: 3px;
178
- font-size: 8px;
179
- color: #fff;
180
- white-space: nowrap;
181
- overflow: hidden;
182
- text-overflow: ellipsis;
183
- }}
184
- .preview-area {{
185
- flex: 1;
186
- display: flex;
187
- flex-direction: column;
188
- background: #1a1a1a;
189
- margin: 8px;
190
- border-radius: 12px;
191
- overflow: hidden;
192
- box-shadow: 0 4px 20px rgba(0,0,0,0.3);
193
- }}
194
- .preview-box {{
195
- flex: 1;
196
- display: flex;
197
- align-items: center;
198
- justify-content: center;
199
- background: #000;
200
- position: relative;
201
- }}
202
- #previewCanvas {{
203
- max-width: 100%;
204
- max-height: 100%;
205
- background: #000;
206
- }}
207
- .controls {{
208
- height: 50px;
209
- background: linear-gradient(to top, #1a1a1a, #222);
210
- display: flex;
211
- align-items: center;
212
- justify-content: center;
213
- gap: 8px;
214
- padding: 0 15px;
215
- }}
216
- .ctrl-btn {{
217
- width: 32px;
218
- height: 32px;
219
- border: none;
220
- border-radius: 50%;
221
- background: rgba(255,255,255,0.1);
222
- color: #fff;
223
- cursor: pointer;
224
- font-size: 12px;
225
- transition: all 0.2s;
226
- }}
227
- .ctrl-btn:hover {{
228
- background: rgba(255,255,255,0.2);
229
- transform: scale(1.1);
230
- }}
231
- .ctrl-btn.play {{
232
- width: 40px;
233
- height: 40px;
234
- background: #6366f1;
235
- font-size: 14px;
236
- }}
237
- .ctrl-btn.play:hover {{
238
- background: #4f46e5;
239
- }}
240
- .time-display {{
241
- font-family: 'SF Mono', Monaco, monospace;
242
- font-size: 11px;
243
- color: #aaa;
244
- min-width: 100px;
245
- text-align: center;
246
- background: rgba(0,0,0,0.3);
247
- padding: 4px 10px;
248
- border-radius: 4px;
249
- }}
250
- .props {{
251
- width: 160px;
252
- background: #fff;
253
- border-left: 1px solid #ddd;
254
- display: flex;
255
- flex-direction: column;
256
- }}
257
- .props-header {{
258
- padding: 10px 12px;
259
- border-bottom: 1px solid #eee;
260
- font-size: 11px;
261
- font-weight: 600;
262
- color: #666;
263
- text-transform: uppercase;
264
- letter-spacing: 0.5px;
265
- }}
266
- .props-content {{
267
- flex: 1;
268
- padding: 12px;
269
- overflow-y: auto;
270
- }}
271
- .no-sel {{
272
- color: #999;
273
- text-align: center;
274
- padding: 20px;
275
- font-size: 11px;
276
- }}
277
- .prop-group {{
278
- margin-bottom: 12px;
279
- }}
280
- .prop-label {{
281
- font-size: 10px;
282
- color: #666;
283
- margin-bottom: 4px;
284
- font-weight: 500;
285
- }}
286
- .prop-input {{
287
- width: 100%;
288
- padding: 6px 8px;
289
- border: 1px solid #ddd;
290
- border-radius: 4px;
291
- font-size: 11px;
292
- }}
293
- .prop-input:focus {{
294
- outline: none;
295
- border-color: #6366f1;
296
- }}
297
- .timeline {{
298
- height: 150px;
299
- background: #fff;
300
- border-top: 1px solid #ddd;
301
- display: flex;
302
- flex-direction: column;
303
- }}
304
- .tl-toolbar {{
305
- height: 32px;
306
- background: #fafafa;
307
- border-bottom: 1px solid #eee;
308
- display: flex;
309
- align-items: center;
310
- padding: 0 8px;
311
- gap: 6px;
312
- }}
313
- .tl-toolbar .btn {{
314
- padding: 4px 8px;
315
- font-size: 10px;
316
- }}
317
- .tl-zoom {{
318
- display: flex;
319
- align-items: center;
320
- gap: 4px;
321
- margin-left: auto;
322
- font-size: 10px;
323
- color: #666;
324
- }}
325
- .tl-zoom input {{
326
- width: 60px;
327
- }}
328
- .tl-container {{
329
- flex: 1;
330
- overflow-x: auto;
331
- position: relative;
332
- }}
333
- .tl-ruler {{
334
- height: 20px;
335
- background: #fff;
336
- border-bottom: 1px solid #eee;
337
- position: sticky;
338
- top: 0;
339
- }}
340
- .tl-tracks {{
341
- position: relative;
342
- }}
343
- .tl-track {{
344
- height: 50px;
345
- border-bottom: 1px solid #eee;
346
- display: flex;
347
- }}
348
- .tl-track:nth-child(2) {{
349
- background: #fffbeb;
350
- }}
351
- .track-label {{
352
- width: 50px;
353
- padding: 0 6px;
354
- font-size: 9px;
355
- color: #666;
356
- background: #fafafa;
357
- display: flex;
358
- align-items: center;
359
- border-right: 1px solid #eee;
360
- font-weight: 500;
361
- }}
362
- .track-content {{
363
- flex: 1;
364
- position: relative;
365
- min-width: 800px;
366
- }}
367
- .clip {{
368
- position: absolute;
369
- height: 40px;
370
- top: 5px;
371
- border-radius: 6px;
372
- cursor: grab;
373
- display: flex;
374
- align-items: center;
375
- overflow: hidden;
376
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
377
- transition: box-shadow 0.2s;
378
- }}
379
- .clip:hover {{
380
- box-shadow: 0 0 0 2px #6366f1;
381
- }}
382
- .clip.selected {{
383
- box-shadow: 0 0 0 2px #6366f1;
384
- }}
385
- .clip.video {{
386
- background: linear-gradient(135deg, #818cf8, #6366f1);
387
- }}
388
- .clip.image {{
389
- background: linear-gradient(135deg, #34d399, #10b981);
390
- }}
391
- .clip.audio {{
392
- background: linear-gradient(135deg, #fbbf24, #f59e0b);
393
- }}
394
- .clip-thumb {{
395
- width: 40px;
396
- height: 100%;
397
- object-fit: cover;
398
- }}
399
- .clip-info {{
400
- padding: 0 6px;
401
- flex: 1;
402
- overflow: hidden;
403
- }}
404
- .clip-name {{
405
- font-size: 9px;
406
- color: #fff;
407
- font-weight: 500;
408
- overflow: hidden;
409
- text-overflow: ellipsis;
410
- white-space: nowrap;
411
- }}
412
- .clip-dur {{
413
- font-size: 8px;
414
- color: rgba(255,255,255,0.8);
415
- margin-top: 2px;
416
- }}
417
- .clip-handle {{
418
- position: absolute;
419
- top: 0;
420
- bottom: 0;
421
- width: 8px;
422
- background: rgba(255,255,255,0.5);
423
- cursor: ew-resize;
424
- opacity: 0;
425
- transition: opacity 0.2s;
426
- }}
427
- .clip:hover .clip-handle {{
428
- opacity: 1;
429
- }}
430
- .clip-handle-l {{
431
- left: 0;
432
- border-radius: 6px 0 0 6px;
433
- }}
434
- .clip-handle-r {{
435
- right: 0;
436
- border-radius: 0 6px 6px 0;
437
- }}
438
- .playhead {{
439
- position: absolute;
440
- top: 0;
441
- bottom: 0;
442
- width: 2px;
443
- background: #ef4444;
444
- z-index: 10;
445
- pointer-events: none;
446
- }}
447
- .playhead::before {{
448
- content: "";
449
- position: absolute;
450
- top: 0;
451
- left: -5px;
452
- border-left: 6px solid transparent;
453
- border-right: 6px solid transparent;
454
- border-top: 8px solid #ef4444;
455
- }}
456
- .drop-zone {{
457
- background: rgba(99, 102, 241, 0.1) !important;
458
- outline: 2px dashed #6366f1 !important;
459
- }}
460
- .status {{
461
- height: 24px;
462
- background: #f5f5f5;
463
- border-top: 1px solid #ddd;
464
- display: flex;
465
- align-items: center;
466
- padding: 0 12px;
467
- font-size: 10px;
468
- color: #666;
469
- }}
470
- .ctx-menu {{
471
- position: fixed;
472
- background: #fff;
473
- border: 1px solid #ddd;
474
- border-radius: 8px;
475
- padding: 4px 0;
476
- min-width: 120px;
477
- z-index: 1000;
478
- box-shadow: 0 4px 12px rgba(0,0,0,0.15);
479
- display: none;
480
- }}
481
- .ctx-item {{
482
- padding: 8px 12px;
483
- cursor: pointer;
484
- font-size: 11px;
485
- transition: background 0.2s;
486
- }}
487
- .ctx-item:hover {{
488
- background: #f5f5f5;
489
- }}
490
- .ctx-item.danger {{
491
- color: #ef4444;
492
- }}
493
- .hidden-media {{
494
- position: absolute;
495
- left: -9999px;
496
- top: -9999px;
497
- width: 1px;
498
- height: 1px;
499
- opacity: 0;
500
- pointer-events: none;
501
- }}
502
  </style>
503
  </head>
504
  <body>
505
  <div class="editor">
506
- <div class="toolbar">
507
- <div class="toolbar-title">๐ŸŽฌ Video Editor</div>
508
- <div class="toolbar-actions">
509
- <button class="btn btn-secondary" onclick="undo()">โ†ฉ ์‹คํ–‰์ทจ์†Œ</button>
510
- <button class="btn btn-success" onclick="copyExportData()">๐Ÿ“‹ ํƒ€์ž„๋ผ์ธ ๋ณต์‚ฌ</button>
511
- </div>
512
- </div>
513
- <div class="main">
514
- <div class="library">
515
- <div class="lib-header">๐Ÿ“ ๋ฏธ๋””์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ</div>
516
- <div class="lib-content">
517
- <div class="lib-hint" id="hint">ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜์„ธ์š”</div>
518
- <div class="media-grid" id="mediaGrid"></div>
519
- </div>
520
- </div>
521
- <div class="preview-area">
522
- <div class="preview-box">
523
- <canvas id="previewCanvas" width="640" height="360"></canvas>
524
- </div>
525
- <div class="controls">
526
- <button class="ctrl-btn" onclick="seek(0)">โฎ</button>
527
- <button class="ctrl-btn" onclick="seek(S.time-5)">โช</button>
528
- <button class="ctrl-btn play" onclick="togglePlay()" id="playBtn">โ–ถ</button>
529
- <button class="ctrl-btn" onclick="seek(S.time+5)">โฉ</button>
530
- <button class="ctrl-btn" onclick="seek(S.dur)">โญ</button>
531
- <div class="time-display">
532
- <span id="curT">00:00.00</span> / <span id="totT">00:00.00</span>
533
- </div>
534
- <button class="ctrl-btn" onclick="toggleMute()" id="muteBtn">๐Ÿ”Š</button>
535
- </div>
536
- </div>
537
- <div class="props">
538
- <div class="props-header">โš™๏ธ ์†์„ฑ</div>
539
- <div class="props-content" id="propsBox">
540
- <div class="no-sel">ํด๋ฆฝ์„ ์„ ํƒํ•˜์„ธ์š”</div>
541
- </div>
542
- </div>
543
- </div>
544
- <div class="timeline">
545
- <div class="tl-toolbar">
546
- <button class="btn btn-secondary" onclick="splitClip()">โœ‚ ์ž๋ฅด๊ธฐ</button>
547
- <button class="btn btn-secondary" onclick="dupeClip()">๐Ÿ“‹ ๋ณต์ œ</button>
548
- <button class="btn btn-danger" onclick="delClip()">๐Ÿ—‘ ์‚ญ์ œ</button>
549
- <div class="tl-zoom">
550
- ๐Ÿ” <input type="range" min="0.5" max="3" step="0.1" value="1" oninput="setZoom(this.value)">
551
- </div>
552
- </div>
553
- <div class="tl-container" id="tlBox" onclick="tlClick(event)">
554
- <div class="tl-ruler" id="ruler"></div>
555
- <div class="tl-tracks">
556
- <div class="tl-track">
557
- <div class="track-label">๐ŸŽฌ ์˜์ƒ</div>
558
- <div class="track-content" id="t0"></div>
559
- </div>
560
- <div class="tl-track">
561
- <div class="track-label">๐ŸŽต ์˜ค๋””์˜ค</div>
562
- <div class="track-content" id="t1"></div>
563
- </div>
564
- </div>
565
- <div class="playhead" id="playhead" style="left:50px"></div>
566
- </div>
567
- </div>
568
- <div class="status" id="status">์ค€๋น„๋จ</div>
569
  </div>
570
  <div class="ctx-menu" id="ctx">
571
- <div class="ctx-item" onclick="splitClip()">โœ‚ ์ž๋ฅด๊ธฐ</div>
572
- <div class="ctx-item" onclick="dupeClip()">๐Ÿ“‹ ๋ณต์ œ</div>
573
- <div class="ctx-item danger" onclick="delClip()">๐Ÿ—‘ ์‚ญ์ œ</div>
 
 
 
 
 
 
 
 
574
  </div>
575
  <div id="hiddenMedia" class="hidden-media"></div>
576
-
577
  <script>
578
- // ์ƒํƒœ ๊ฐ์ฒด
579
- var S = {{
580
- media: [],
581
- clips: [],
582
- sel: null,
583
- playing: false,
584
- muted: false,
585
- time: 0,
586
- dur: 0,
587
- zoom: 1,
588
- pps: 80,
589
- history: [],
590
- animId: null,
591
- els: {{}},
592
- canvas: null,
593
- ctx: null
 
594
  }};
595
 
596
- // ์ดˆ๊ธฐํ™”
597
- function init() {{
598
- S.canvas = document.getElementById('previewCanvas');
599
- S.ctx = S.canvas.getContext('2d');
600
- drawPlaceholder();
601
- }}
602
-
603
- // ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค
604
- function id() {{
605
- return Math.random().toString(36).substr(2, 9);
606
- }}
607
-
608
- function fmt(t) {{
609
- if (!t || isNaN(t)) t = 0;
610
- var m = Math.floor(t / 60);
611
- var s = Math.floor(t % 60);
612
- var ms = Math.floor((t % 1) * 100);
613
- return String(m).padStart(2, '0') + ':' + String(s).padStart(2, '0') + '.' + String(ms).padStart(2, '0');
614
- }}
615
-
616
- function r(n) {{
617
- return Math.round(n * 1000) / 1000;
618
- }}
619
-
620
- function stat(m) {{
621
- document.getElementById('status').textContent = m;
622
- }}
623
-
624
- function save() {{
625
- S.history.push(JSON.stringify(S.clips));
626
- if (S.history.length > 30) S.history.shift();
627
- }}
628
-
629
- // ํ”Œ๋ ˆ์ด์Šคํ™€๋” ๊ทธ๋ฆฌ๊ธฐ
630
- function drawPlaceholder() {{
631
- S.ctx.fillStyle = '#000';
632
- S.ctx.fillRect(0, 0, 640, 360);
633
- S.ctx.fillStyle = '#444';
634
- S.ctx.font = '14px sans-serif';
635
- S.ctx.textAlign = 'center';
636
- S.ctx.fillText('ํƒ€์ž„๋ผ์ธ์— ๋ฏธ๋””์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”', 320, 180);
637
- }}
638
-
639
- // ๋ฏธ๋””์–ด ์ถ”๊ฐ€
640
- function addMedia(name, type, url, filePath) {{
641
- var m = {{
642
- id: id(),
643
- name: name,
644
- type: type,
645
- url: url,
646
- filePath: filePath || name,
647
- dur: type === 'image' ? 5 : 0,
648
- thumb: type === 'image' ? url : null,
649
- loaded: type === 'image' ? true : false
650
- }};
651
- S.media.push(m);
652
-
653
- var container = document.getElementById('hiddenMedia');
654
-
655
- if (type === 'video') {{
656
- var v = document.createElement('video');
657
- v.src = url;
658
- v.muted = true;
659
- v.playsInline = true;
660
- v.preload = 'auto';
661
- v.crossOrigin = 'anonymous';
662
- container.appendChild(v);
663
- S.els[m.id] = v;
664
-
665
- v.onloadedmetadata = function() {{
666
- m.dur = r(v.duration);
667
- m.loaded = true;
668
- renderLib();
669
- v.currentTime = 0.5;
670
- // ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๋กœ๋“œ ํ›„ ํด๋ฆฝ ์ถ”๊ฐ€
671
- addClip(m);
672
- console.log("[VideoEditor] Video loaded:", m.name, m.dur);
673
- }};
674
-
675
- v.onseeked = function() {{
676
- if (!m.thumb) {{
677
- try {{
678
- var c = document.createElement('canvas');
679
- c.width = 160;
680
- c.height = 90;
681
- c.getContext('2d').drawImage(v, 0, 0, 160, 90);
682
- m.thumb = c.toDataURL();
683
- renderLib();
684
- }} catch(e) {{}}
685
- }}
686
- }};
687
-
688
- v.onerror = function() {{
689
- console.error("[VideoEditor] Video load error:", m.name);
690
- m.loaded = true;
691
- m.dur = 5;
692
- renderLib();
693
- addClip(m);
694
- }};
695
- }} else if (type === 'audio') {{
696
- var a = document.createElement('audio');
697
- a.src = url;
698
- a.preload = 'auto';
699
- container.appendChild(a);
700
- S.els[m.id] = a;
701
-
702
- a.onloadedmetadata = function() {{
703
- m.dur = r(a.duration);
704
- m.loaded = true;
705
- renderLib();
706
- // ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๋กœ๋“œ ํ›„ ํด๋ฆฝ ์ถ”๊ฐ€
707
- addClip(m);
708
- console.log("[VideoEditor] Audio loaded:", m.name, m.dur);
709
- }};
710
-
711
- a.onerror = function() {{
712
- console.error("[VideoEditor] Audio load error:", m.name);
713
- m.loaded = true;
714
- m.dur = 5;
715
- renderLib();
716
- addClip(m);
717
- }};
718
- }} else if (type === 'image') {{
719
- var img = new Image();
720
- img.crossOrigin = 'anonymous';
721
- img.onload = function() {{
722
- m.loaded = true;
723
- renderLib();
724
- // ์ด๋ฏธ์ง€ ๋กœ๋“œ ํ›„ ํด๋ฆฝ ์ถ”๊ฐ€
725
- addClip(m);
726
- console.log("[VideoEditor] Image loaded:", m.name);
727
- }};
728
- img.onerror = function() {{
729
- console.error("[VideoEditor] Image load error:", m.name);
730
- m.loaded = true;
731
- addClip(m);
732
- }};
733
- img.src = url;
734
- S.els[m.id] = img;
735
- }}
736
-
737
- renderLib();
738
- stat('๋ฏธ๋””์–ด ๋กœ๋”ฉ: ' + name);
739
- }}
740
-
741
- // ๋ฏธ๋””์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ Œ๋”๋ง
742
- function renderLib() {{
743
- var g = document.getElementById('mediaGrid');
744
- var h = document.getElementById('hint');
745
- h.style.display = S.media.length ? 'none' : 'block';
746
- g.innerHTML = '';
747
-
748
- S.media.forEach(function(m) {{
749
- var d = document.createElement('div');
750
- d.className = 'media-item';
751
- d.draggable = true;
752
- d.ondblclick = function() {{ addClip(m); }};
753
- d.ondragstart = function(e) {{ e.dataTransfer.setData('mid', m.id); }};
754
-
755
- var th = m.thumb ? '<img src="' + m.thumb + '">' : '<div class="media-item-icon">' + (m.type === 'video' ? '๐ŸŽฌ' : m.type === 'audio' ? '๐ŸŽต' : '๐Ÿ–ผ') + '</div>';
756
- d.innerHTML = th + (m.dur ? '<div class="media-item-dur">' + fmt(m.dur) + '</div>' : '');
757
- g.appendChild(d);
758
- }});
759
- }}
760
-
761
- // ํŠธ๋ž™ ๋ ์œ„์น˜ ๊ณ„์‚ฐ
762
- function trackEnd(tr) {{
763
- var end = 0;
764
- for (var i = 0; i < S.clips.length; i++) {{
765
- var c = S.clips[i];
766
- if (c.track === tr) {{
767
- var e = r(c.start + (c.te - c.ts));
768
- if (e > end) end = e;
769
- }}
770
- }}
771
- return end;
772
- }}
773
-
774
- // ํด๋ฆฝ ์ถ”๊ฐ€
775
- function addClip(m, at) {{
776
- save();
777
- var tr = m.type === 'audio' ? 1 : 0;
778
- var st = at !== undefined ? r(at) : trackEnd(tr);
779
-
780
- S.clips.push({{
781
- id: id(),
782
- mid: m.id,
783
- name: m.name,
784
- type: m.type,
785
- track: tr,
786
- start: st,
787
- dur: m.dur,
788
- ts: 0,
789
- te: m.dur,
790
- vol: 1,
791
- filePath: m.filePath
792
- }});
793
-
794
- renderTL();
795
- updateDur();
796
- stat('ํด๋ฆฝ ์ถ”๊ฐ€: ' + m.name);
797
- drawFrame();
798
- }}
799
-
800
- // ํƒ€์ž„๋ผ์ธ ๋ Œ๋”๋ง
801
- function renderTL() {{
802
- ['t0', 't1'].forEach(function(tid) {{
803
- document.getElementById(tid).innerHTML = '';
804
- }});
805
-
806
- S.clips.forEach(function(c) {{
807
- var tr = document.getElementById('t' + c.track);
808
- var el = document.createElement('div');
809
- el.className = 'clip ' + c.type + (S.sel === c.id ? ' selected' : '');
810
- var len = r(c.te - c.ts);
811
- el.style.left = r(c.start * S.pps * S.zoom) + 'px';
812
- el.style.width = Math.max(30, r(len * S.pps * S.zoom)) + 'px';
813
- el.draggable = true;
814
-
815
- el.onclick = function(e) {{ e.stopPropagation(); selClip(c.id); }};
816
- el.oncontextmenu = function(e) {{ e.preventDefault(); selClip(c.id); showCtx(e.clientX, e.clientY); }};
817
- el.ondragstart = function(e) {{ e.dataTransfer.setData('cid', c.id); e.dataTransfer.setData('ox', e.offsetX); }};
818
-
819
- var m = S.media.find(function(x) {{ return x.id === c.mid; }});
820
- var th = m && m.thumb ? '<img class="clip-thumb" src="' + m.thumb + '">' : '';
821
- el.innerHTML = th + '<div class="clip-info"><div class="clip-name">' + c.name + '</div><div class="clip-dur">' + fmt(len) + '</div></div><div class="clip-handle clip-handle-l"></div><div class="clip-handle clip-handle-r"></div>';
822
-
823
- el.querySelector('.clip-handle-l').onmousedown = function(e) {{ e.stopPropagation(); startTrim(c.id, 'l', e); }};
824
- el.querySelector('.clip-handle-r').onmousedown = function(e) {{ e.stopPropagation(); startTrim(c.id, 'r', e); }};
825
-
826
- tr.appendChild(el);
827
- }});
828
-
829
- renderRuler();
830
- setupDrop();
831
- }}
832
-
833
- // ๋ฃฐ๋Ÿฌ ๋ Œ๋”๋ง
834
- function renderRuler() {{
835
- var ru = document.getElementById('ruler');
836
- var w = Math.max(r(S.dur * S.pps * S.zoom) + 200, 800);
837
- ru.style.width = w + 'px';
838
-
839
- var h = '<svg width="100%" height="20" style="position:absolute;left:50px">';
840
- var step = S.zoom < 0.7 ? 5 : S.zoom < 1.5 ? 2 : 1;
841
-
842
- for (var i = 0; i <= S.dur + 10; i += step) {{
843
- var x = r(i * S.pps * S.zoom);
844
- h += '<line x1="' + x + '" y1="14" x2="' + x + '" y2="20" stroke="#ccc"/>';
845
- h += '<text x="' + x + '" y="11" fill="#999" font-size="9" text-anchor="middle">' + fmt(i) + '</text>';
846
- }}
847
-
848
- ru.innerHTML = h + '</svg>';
849
- }}
850
-
851
- // ๋“œ๋กญ์กด ์„ค์ •
852
- function setupDrop() {{
853
- ['t0', 't1'].forEach(function(tid, idx) {{
854
- var tr = document.getElementById(tid);
855
-
856
- tr.ondragover = function(e) {{
857
- e.preventDefault();
858
- tr.classList.add('drop-zone');
859
- }};
860
-
861
- tr.ondragleave = function() {{
862
- tr.classList.remove('drop-zone');
863
- }};
864
-
865
- tr.ondrop = function(e) {{
866
- e.preventDefault();
867
- tr.classList.remove('drop-zone');
868
-
869
- var rect = tr.getBoundingClientRect();
870
- var t = r(Math.max(0, (e.clientX - rect.left) / (S.pps * S.zoom)));
871
- var mid = e.dataTransfer.getData('mid');
872
- var cid = e.dataTransfer.getData('cid');
873
- var ox = parseFloat(e.dataTransfer.getData('ox') || 0);
874
-
875
- if (mid) {{
876
- var m = S.media.find(function(x) {{ return x.id === mid; }});
877
- if (m) addClip(m, t);
878
- }} else if (cid) {{
879
- save();
880
- var c = S.clips.find(function(x) {{ return x.id === cid; }});
881
- if (c) {{
882
- c.start = r(Math.max(0, t - ox / (S.pps * S.zoom)));
883
- c.track = c.type === 'audio' ? 1 : idx;
884
- renderTL();
885
- updateDur();
886
- drawFrame();
887
- }}
888
- }}
889
- }};
890
- }});
891
- }}
892
-
893
- // ํŠธ๋ฆผ ๊ด€๋ จ
894
- var trimData = null;
895
-
896
- function startTrim(cid, side, e) {{
897
- e.preventDefault();
898
- var c = S.clips.find(function(x) {{ return x.id === cid; }});
899
- if (!c) return;
900
- save();
901
- trimData = {{ cid: cid, side: side, sx: e.clientX, ots: c.ts, ote: c.te, ost: c.start }};
902
- document.addEventListener('mousemove', doTrim);
903
- document.addEventListener('mouseup', endTrim);
904
- }}
905
-
906
- function doTrim(e) {{
907
- if (!trimData) return;
908
- var c = S.clips.find(function(x) {{ return x.id === trimData.cid; }});
909
- if (!c) return;
910
-
911
- var dx = e.clientX - trimData.sx;
912
- var dt = r(dx / (S.pps * S.zoom));
913
-
914
- if (trimData.side === 'l') {{
915
- var newTs = Math.max(0, Math.min(c.te - 0.1, trimData.ots + dt));
916
- c.ts = r(newTs);
917
- c.start = r(trimData.ost + (newTs - trimData.ots));
918
- }} else {{
919
- c.te = r(Math.max(c.ts + 0.1, Math.min(c.dur, trimData.ote + dt)));
920
- }}
921
-
922
- renderTL();
923
- updateDur();
924
- }}
925
-
926
- function endTrim() {{
927
- trimData = null;
928
- document.removeEventListener('mousemove', doTrim);
929
- document.removeEventListener('mouseup', endTrim);
930
- }}
931
-
932
- // ํด๋ฆฝ ์„ ํƒ
933
- function selClip(cid) {{
934
- S.sel = cid;
935
- renderTL();
936
- renderProps();
937
- }}
938
-
939
- // ์†์„ฑ ํŒจ๋„ ๋ Œ๋”๋ง
940
- function renderProps() {{
941
- var box = document.getElementById('propsBox');
942
- var c = S.clips.find(function(x) {{ return x.id === S.sel; }});
943
-
944
- if (!c) {{
945
- box.innerHTML = '<div class="no-sel">ํด๋ฆฝ์„ ์„ ํƒํ•˜์„ธ์š”</div>';
946
- return;
947
- }}
948
-
949
- var len = r(c.te - c.ts);
950
- box.innerHTML = '<div class="prop-group"><div class="prop-label">์ด๋ฆ„</div><input class="prop-input" value="' + c.name + '" onchange="setProp(\'name\',this.value)"></div>' +
951
- '<div class="prop-group"><div class="prop-label">์‹œ์ž‘ ์‹œ๊ฐ„</div><input class="prop-input" type="number" step="0.1" value="' + c.start + '" onchange="setProp(\'start\',parseFloat(this.value))"></div>' +
952
- '<div class="prop-group"><div class="prop-label">๊ธธ์ด: ' + fmt(len) + '</div></div>' +
953
- (c.type !== 'image' ? '<div class="prop-group"><div class="prop-label">๋ณผ๋ฅจ ' + Math.round(c.vol * 100) + '%</div><input class="prop-input" type="range" min="0" max="1" step="0.05" value="' + c.vol + '" oninput="setProp(\'vol\',parseFloat(this.value))"></div>' : '');
954
- }}
955
-
956
- function setProp(p, v) {{
957
- save();
958
- var c = S.clips.find(function(x) {{ return x.id === S.sel; }});
959
- if (c) {{
960
- c[p] = p === 'start' ? r(v) : v;
961
- renderTL();
962
- updateDur();
963
- renderProps();
964
- drawFrame();
965
- }}
966
- }}
967
-
968
- // ํด๋ฆฝ ๋ถ„ํ• 
969
- function splitClip() {{
970
- if (!S.sel) {{
971
- alert('ํด๋ฆฝ์„ ์„ ํƒํ•˜์„ธ์š”');
972
- return;
973
- }}
974
-
975
- var c = S.clips.find(function(x) {{ return x.id === S.sel; }});
976
- if (!c) return;
977
-
978
- var cEnd = r(c.start + c.te - c.ts);
979
- if (S.time <= c.start || S.time >= cEnd) {{
980
- alert('ํ”Œ๋ ˆ์ดํ—ค๋“œ๊ฐ€ ํด๋ฆฝ ์•ˆ์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค');
981
- return;
982
- }}
983
-
984
- save();
985
- var splitAt = r(S.time - c.start);
986
- var c2 = JSON.parse(JSON.stringify(c));
987
- c2.id = id();
988
- c2.start = r(S.time);
989
- c2.ts = r(c.ts + splitAt);
990
- c.te = r(c.ts + splitAt);
991
- S.clips.push(c2);
992
-
993
- renderTL();
994
- updateDur();
995
- hideCtx();
996
- stat('ํด๋ฆฝ ๋ถ„ํ• ๋จ');
997
- }}
998
-
999
- // ํด๋ฆฝ ๋ณต์ œ
1000
- function dupeClip() {{
1001
- if (!S.sel) return;
1002
-
1003
- var c = S.clips.find(function(x) {{ return x.id === S.sel; }});
1004
- if (!c) return;
1005
-
1006
- save();
1007
- var len = r(c.te - c.ts);
1008
- var nc = JSON.parse(JSON.stringify(c));
1009
- nc.id = id();
1010
- nc.start = r(c.start + len);
1011
- S.clips.push(nc);
1012
-
1013
- renderTL();
1014
- updateDur();
1015
- hideCtx();
1016
- stat('ํด๋ฆฝ ๋ณต์ œ๋จ');
1017
- }}
1018
-
1019
- // ํด๋ฆฝ ์‚ญ์ œ
1020
- function delClip() {{
1021
- if (!S.sel) return;
1022
-
1023
- save();
1024
- S.clips = S.clips.filter(function(x) {{ return x.id !== S.sel; }});
1025
- S.sel = null;
1026
-
1027
- renderTL();
1028
- renderProps();
1029
- updateDur();
1030
- hideCtx();
1031
- stat('ํด๋ฆฝ ์‚ญ์ œ๋จ');
1032
- drawFrame();
1033
- }}
1034
-
1035
- // ์‹คํ–‰ ์ทจ์†Œ
1036
- function undo() {{
1037
- if (S.history.length) {{
1038
- S.clips = JSON.parse(S.history.pop());
1039
- renderTL();
1040
- updateDur();
1041
- stat('์‹คํ–‰์ทจ์†Œ');
1042
- drawFrame();
1043
- }}
1044
- }}
1045
-
1046
- // ์ด ๊ธธ์ด ์—…๋ฐ์ดํŠธ
1047
- function updateDur() {{
1048
- var mx = 0;
1049
- for (var i = 0; i < S.clips.length; i++) {{
1050
- var c = S.clips[i];
1051
- var e = r(c.start + c.te - c.ts);
1052
- if (e > mx) mx = e;
1053
- }}
1054
- S.dur = mx;
1055
- document.getElementById('totT').textContent = fmt(mx);
1056
- }}
1057
-
1058
- // ์žฌ์ƒ ํ† ๊ธ€
1059
- function togglePlay() {{
1060
- S.playing = !S.playing;
1061
- document.getElementById('playBtn').textContent = S.playing ? 'โธ' : 'โ–ถ';
1062
- if (S.playing) play();
1063
- else stop();
1064
- }}
1065
-
1066
- // ์žฌ์ƒ
1067
- function play() {{
1068
- var last = performance.now();
1069
-
1070
- function loop(now) {{
1071
- if (!S.playing) return;
1072
-
1073
- var dt = (now - last) / 1000;
1074
- last = now;
1075
- S.time = S.time + dt;
1076
-
1077
- if (S.time >= S.dur) {{
1078
- S.time = 0;
1079
- if (S.dur === 0) {{
1080
- S.playing = false;
1081
- document.getElementById('playBtn').textContent = 'โ–ถ';
1082
- return;
1083
- }}
1084
- }}
1085
-
1086
- updateHead();
1087
- drawFrame();
1088
- S.animId = requestAnimationFrame(loop);
1089
- }}
1090
-
1091
- S.animId = requestAnimationFrame(loop);
1092
- }}
1093
-
1094
- // ์ •์ง€
1095
- function stop() {{
1096
- if (S.animId) {{
1097
- cancelAnimationFrame(S.animId);
1098
- S.animId = null;
1099
- }}
1100
-
1101
- Object.keys(S.els).forEach(function(k) {{
1102
- var el = S.els[k];
1103
- if (el && el.pause) el.pause();
1104
- }});
1105
- }}
1106
-
1107
- // ์‹œ๊ฐ„ ์ด๋™
1108
- function seek(t) {{
1109
- S.time = Math.max(0, Math.min(S.dur || 0, t));
1110
- updateHead();
1111
- drawFrame();
1112
  }}
1113
-
1114
- // ํ”Œ๋ ˆ์ดํ—ค๋“œ ์—…๋ฐ์ดํŠธ
1115
- function updateHead() {{
1116
- document.getElementById('playhead').style.left = (50 + S.time * S.pps * S.zoom) + 'px';
1117
- document.getElementById('curT').textContent = fmt(S.time);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1118
  }}
1119
 
1120
- // ํŠน์ • ์‹œ๊ฐ„์˜ ํด๋ฆฝ ๊ฐ€์ ธ์˜ค๊ธฐ
1121
- function getClipAt(t, type) {{
1122
- var sorted = S.clips.filter(function(c) {{
1123
- if (type === 'visual') return c.type === 'video' || c.type === 'image';
1124
- if (type === 'audio') return c.type === 'audio';
1125
- return true;
1126
- }}).sort(function(a, b) {{ return a.start - b.start; }});
1127
-
1128
- for (var i = 0; i < sorted.length; i++) {{
1129
- var c = sorted[i];
1130
- var cEnd = c.start + (c.te - c.ts);
1131
- if (t >= c.start && t < cEnd) return c;
1132
- }}
1133
- return null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1134
  }}
1135
-
1136
- // ํ”„๋ ˆ์ž„ ๊ทธ๋ฆฌ๊ธฐ
1137
- function drawFrame() {{
1138
- var t = S.time;
1139
- var vc = getClipAt(t, 'visual');
1140
-
1141
- S.ctx.fillStyle = '#000';
1142
- S.ctx.fillRect(0, 0, 640, 360);
1143
-
1144
- if (vc) {{
1145
- var el = S.els[vc.mid];
1146
- if (el) {{
1147
- if (vc.type === 'video') {{
1148
- var clipT = t - vc.start + vc.ts;
1149
- if (Math.abs(el.currentTime - clipT) > 0.05) {{
1150
- el.currentTime = clipT;
1151
- }}
1152
- if (S.playing && el.paused) el.play().catch(function() {{}});
1153
- if (!S.playing && !el.paused) el.pause();
1154
- el.volume = S.muted ? 0 : vc.vol;
1155
- }}
1156
-
1157
- try {{
1158
- var sw = el.videoWidth || el.naturalWidth || el.width || 640;
1159
- var sh = el.videoHeight || el.naturalHeight || el.height || 360;
1160
- var scale = Math.min(640 / sw, 360 / sh);
1161
- var dw = sw * scale, dh = sh * scale;
1162
- var dx = (640 - dw) / 2, dy = (360 - dh) / 2;
1163
- S.ctx.drawImage(el, dx, dy, dw, dh);
1164
- }} catch(e) {{}}
1165
- }}
1166
- }} else if (S.clips.length === 0) {{
1167
- S.ctx.fillStyle = '#444';
1168
- S.ctx.font = '14px sans-serif';
1169
- S.ctx.textAlign = 'center';
1170
- S.ctx.fillText('ํƒ€์ž„๋ผ์ธ์— ๋ฏธ๋””์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”', 320, 180);
1171
- }}
1172
-
1173
- // ์˜ค๋””์˜ค ํด๋ฆฝ ์ฒ˜๋ฆฌ
1174
- var audioClips = S.clips.filter(function(c) {{
1175
- if (c.type !== 'audio') return false;
1176
- var cEnd = c.start + (c.te - c.ts);
1177
- return t >= c.start && t < cEnd;
1178
- }});
1179
-
1180
- audioClips.forEach(function(ac) {{
1181
- var el = S.els[ac.mid];
1182
- if (el) {{
1183
- var clipT = t - ac.start + ac.ts;
1184
- if (Math.abs(el.currentTime - clipT) > 0.1) el.currentTime = clipT;
1185
- el.volume = S.muted ? 0 : ac.vol;
1186
- if (S.playing && el.paused) el.play().catch(function() {{}});
1187
- if (!S.playing && !el.paused) el.pause();
1188
- }}
1189
- }});
1190
-
1191
- // ๋ฒ”์œ„ ๋ฐ– ์˜ค๋””์˜ค ์ •์ง€
1192
- S.clips.forEach(function(c) {{
1193
- if (c.type !== 'audio') return;
1194
- var cEnd = c.start + (c.te - c.ts);
1195
- if (t < c.start || t >= cEnd) {{
1196
- var el = S.els[c.mid];
1197
- if (el && !el.paused) el.pause();
1198
- }}
1199
- }});
1200
-
1201
- // ๋นˆ ํ”„๋ ˆ์ž„ ํ‘œ์‹œ
1202
- if (!vc && !audioClips.length && S.clips.length > 0) {{
1203
- S.ctx.fillStyle = '#333';
1204
- S.ctx.font = '12px sans-serif';
1205
- S.ctx.textAlign = 'center';
1206
- S.ctx.fillText('์žฌ์ƒ ์œ„์น˜์— ๋ฏธ๋””์–ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค', 320, 180);
1207
- }}
1208
  }}
1209
-
1210
- // ์Œ์†Œ๊ฑฐ ํ† ๊ธ€
1211
- function toggleMute() {{
1212
- S.muted = !S.muted;
1213
- document.getElementById('muteBtn').textContent = S.muted ? '๐Ÿ”‡' : '๐Ÿ”Š';
1214
  }}
1215
 
1216
- // ์คŒ ์„ค์ •
1217
- function setZoom(v) {{
1218
- S.zoom = parseFloat(v);
1219
- renderTL();
1220
- updateHead();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1221
  }}
1222
-
1223
- // ํƒ€์ž„๋ผ์ธ ํด๋ฆญ
1224
- function tlClick(e) {{
1225
- if (e.target.closest('.clip')) return;
1226
-
1227
- var rect = document.getElementById('tlBox').getBoundingClientRect();
1228
- var scrollL = document.getElementById('tlBox').scrollLeft;
1229
- S.time = Math.max(0, Math.min(S.dur || 0, (e.clientX - rect.left - 50 + scrollL) / (S.pps * S.zoom)));
1230
- updateHead();
1231
- drawFrame();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1232
  }}
1233
 
1234
- // ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด
1235
- function showCtx(x, y) {{
1236
- var m = document.getElementById('ctx');
1237
- m.style.display = 'block';
1238
- m.style.left = x + 'px';
1239
- m.style.top = y + 'px';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1240
  }}
1241
-
1242
- function hideCtx() {{
1243
- document.getElementById('ctx').style.display = 'none';
 
 
 
 
1244
  }}
1245
-
1246
- document.addEventListener('click', function(e) {{
1247
- if (!e.target.closest('.ctx-menu')) hideCtx();
1248
  }});
1249
-
1250
- // ๋‚ด๋ณด๋‚ด๊ธฐ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
1251
- function getExportData() {{
1252
- var clipsData = [];
1253
- for (var i = 0; i < S.clips.length; i++) {{
1254
- var c = S.clips[i];
1255
- var m = S.media.find(function(x) {{ return x.id === c.mid; }});
1256
- clipsData.push({{
1257
- filePath: m ? m.filePath : c.name,
1258
- type: c.type,
1259
- start: c.start,
1260
- ts: c.ts,
1261
- te: c.te,
1262
- vol: c.vol
1263
- }});
1264
- }}
1265
- return JSON.stringify({{ clips: clipsData }});
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1266
  }}
1267
 
1268
- // ๋‚ด๋ณด๋‚ด๊ธฐ ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ
1269
- function copyExportData() {{
1270
- var data = getExportData();
1271
-
1272
- navigator.clipboard.writeText(data).then(function() {{
1273
- stat('ํƒ€์ž„๋ผ์ธ ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ๋จ! ์œ„์˜ ํ…์ŠคํŠธ๋ฐ•์Šค์— ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ›„ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ ํด๋ฆญ');
1274
- alert('ํƒ€์ž„๋ผ์ธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณต์‚ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!\\n\\n1. ์œ„์˜ ํ…์ŠคํŠธ๋ฐ•์Šค์— Ctrl+V๋กœ ๋ถ™์—ฌ๋„ฃ๊ธฐ\\n2. MP4 ๋‚ด๋ณด๋‚ด๊ธฐ ๋ฒ„ํŠผ ํด๋ฆญ');
1275
- }}).catch(function() {{
1276
- // fallback
1277
- var ta = document.createElement('textarea');
1278
- ta.value = data;
1279
- document.body.appendChild(ta);
1280
- ta.select();
1281
- document.execCommand('copy');
1282
- document.body.removeChild(ta);
1283
- stat('ํƒ€์ž„๋ผ์ธ ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ๋จ!');
1284
- alert('ํƒ€์ž„๋ผ์ธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณต์‚ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!');
1285
- }});
 
1286
  }}
1287
 
1288
- // ํ‚ค๋ณด๋“œ ๋‹จ์ถ•ํ‚ค
1289
- document.addEventListener('keydown', function(e) {{
1290
- if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
1291
-
1292
- if (e.code === 'Space') {{
1293
- e.preventDefault();
1294
- togglePlay();
1295
- }} else if (e.code === 'Delete') {{
1296
- e.preventDefault();
1297
- delClip();
1298
- }} else if (e.code === 'ArrowLeft') {{
1299
- seek(S.time - 0.1);
1300
- }} else if (e.code === 'ArrowRight') {{
1301
- seek(S.time + 0.1);
1302
- }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1303
  }});
1304
 
1305
- // ์ดˆ๊ธฐํ™” ์‹คํ–‰
1306
  init();
1307
  renderTL();
1308
- stat('์ค€๋น„๋จ | ๋‹จ์ถ•ํ‚ค: Space(์žฌ์ƒ), Delete(์‚ญ์ œ), โ†โ†’(์ด๋™)');
1309
-
1310
- // postMessage๋กœ ๋ฏธ๋””์–ด ๋ฐ์ดํ„ฐ ์ˆ˜์‹ 
1311
- window.addEventListener("message", function(e) {{
1312
- if (e.data && e.data.type === "loadMedia" && e.data.data) {{
1313
- console.log("[VideoEditor] Received media via postMessage:", e.data.data.length);
1314
- e.data.data.forEach(function(m) {{
1315
- addMedia(m.name, m.type, m.dataUrl, m.filePath);
1316
- }});
1317
- }}
1318
- }});
1319
-
1320
- // ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ ๋กœ๋“œ (๋นˆ ๋ฐฐ์—ด)
1321
- var initData = {media_data};
1322
- if (initData && initData.length) {{
1323
- initData.forEach(function(m) {{
1324
- addMedia(m.name, m.type, m.dataUrl, m.filePath);
1325
- }});
1326
- }}
1327
  </script>
1328
  </body>
1329
  </html>'''
1330
 
1331
-
1332
- def process_files(files):
1333
  """ํŒŒ์ผ ์ฒ˜๋ฆฌ ๋ฐ ์„œ๋ฒ„์— ์ €์žฅ"""
1334
  global uploaded_files
1335
  if not files:
1336
  return []
1337
-
1338
  results = []
1339
  file_list = files if isinstance(files, list) else [files]
1340
-
1341
  for f in file_list:
1342
  if not f:
1343
  continue
1344
  path = f.name if hasattr(f, 'name') else f
1345
  name = os.path.basename(path)
1346
  ext = name.lower().split('.')[-1]
1347
-
1348
  if ext in ['mp4', 'webm', 'mov', 'avi', 'mkv']:
1349
  t, m = 'video', f'video/{ext}'
1350
  elif ext in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
@@ -1354,66 +761,26 @@ def process_files(files):
1354
  else:
1355
  continue
1356
 
1357
- # ์„œ๋ฒ„์— ํŒŒ์ผ ๋ณต์‚ฌ
1358
  dst_path = os.path.join(UPLOAD_DIR, f"{int(time.time()*1000)}_{name}")
1359
  shutil.copy(path, dst_path)
1360
  uploaded_files[name] = dst_path
1361
- print(f"[Upload] {name} -> {dst_path}")
1362
 
1363
- # base64 ์ธ์ฝ”๋”ฉ (๋ฏธ๋ฆฌ๋ณด๊ธฐ์šฉ)
1364
  with open(path, 'rb') as fp:
1365
  d = base64.b64encode(fp.read()).decode()
1366
-
1367
- results.append({
1368
- 'name': name,
1369
- 'type': t,
1370
- 'dataUrl': f'data:{m};base64,{d}',
1371
- 'filePath': name
1372
- })
1373
-
1374
  return results
1375
 
1376
-
1377
  def make_iframe(data):
1378
- """์—๋””ํ„ฐ iframe ์ƒ์„ฑ - ๋นˆ ์—๋””ํ„ฐ ๋กœ๋“œ ํ›„ postMessage๋กœ ๋ฏธ๋””์–ด ์ถ”๊ฐ€"""
1379
- html_content = get_editor_html("[]") # ํ•ญ์ƒ ๋นˆ ๋ฐฐ์—ด๋กœ ์‹œ์ž‘
1380
- b64 = base64.b64encode(html_content.encode('utf-8')).decode('utf-8')
1381
-
1382
- # ๋ฏธ๋””์–ด ๋ฐ์ดํ„ฐ๋ฅผ postMessage๋กœ ์ „๋‹ฌํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ
1383
- media_json = json.dumps(data, ensure_ascii=False) if data else "[]"
1384
-
1385
- return f'''
1386
- <iframe id="editorFrame" src="data:text/html;base64,{b64}" style="width:100%;height:700px;border:none;border-radius:10px"></iframe>
1387
- <script>
1388
- (function() {{
1389
- var mediaData = {media_json};
1390
- var iframe = document.getElementById("editorFrame");
1391
-
1392
- function sendMedia() {{
1393
- if (iframe.contentWindow && mediaData.length > 0) {{
1394
- iframe.contentWindow.postMessage({{type: "loadMedia", data: mediaData}}, "*");
1395
- }}
1396
- }}
1397
-
1398
- iframe.onload = function() {{
1399
- setTimeout(sendMedia, 300);
1400
- }};
1401
-
1402
- // ์ด๋ฏธ ๋กœ๋“œ๋œ ๊ฒฝ์šฐ
1403
- if (iframe.contentDocument && iframe.contentDocument.readyState === "complete") {{
1404
- setTimeout(sendMedia, 300);
1405
- }}
1406
- }})();
1407
- </script>
1408
- '''
1409
-
1410
 
1411
  def export_mp4(export_json):
1412
- """ํƒ€์ž„๋ผ์ธ ๋ฐ์ดํ„ฐ๋กœ MP4 ์ƒ์„ฑ"""
1413
  global uploaded_files
1414
 
1415
  if not export_json or len(export_json) < 10:
1416
- print("[Export] No data")
1417
  return None
1418
 
1419
  try:
@@ -1421,197 +788,97 @@ def export_mp4(export_json):
1421
  clips = data.get('clips', [])
1422
 
1423
  if not clips:
1424
- print("[Export] No clips")
1425
  return None
1426
 
1427
- # ์˜์ƒ ํด๋ฆฝ๋งŒ ํ•„ํ„ฐ๋ง
1428
  video_clips = [c for c in clips if c['type'] in ['video', 'image']]
1429
  if not video_clips:
1430
- print("[Export] No video clips")
1431
  return None
1432
 
1433
  temp_dir = tempfile.mkdtemp()
1434
  output_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4')
1435
 
1436
- # ๋‹จ์ผ ํด๋ฆฝ์ธ ๊ฒฝ์šฐ
1437
  if len(video_clips) == 1:
1438
  clip = video_clips[0]
1439
  file_path = uploaded_files.get(clip['filePath'])
1440
 
1441
  if not file_path or not os.path.exists(file_path):
1442
- print(f"[Export] File not found: {clip['filePath']}")
1443
- print(f"[Export] Available files: {list(uploaded_files.keys())}")
1444
  return None
1445
 
1446
  duration = clip['te'] - clip['ts']
1447
 
1448
  if clip['type'] == 'image':
1449
- cmd = [
1450
- 'ffmpeg', '-y',
1451
- '-loop', '1',
1452
- '-i', file_path,
1453
- '-c:v', 'libx264',
1454
- '-t', str(duration),
1455
- '-pix_fmt', 'yuv420p',
1456
- '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2',
1457
- output_path
1458
- ]
1459
  else:
1460
- cmd = [
1461
- 'ffmpeg', '-y',
1462
- '-i', file_path,
1463
- '-ss', str(clip['ts']),
1464
- '-t', str(duration),
1465
- '-c:v', 'libx264',
1466
- '-preset', 'fast',
1467
- '-crf', '23',
1468
- '-c:a', 'aac',
1469
- '-b:a', '128k',
1470
- '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2',
1471
- '-movflags', '+faststart',
1472
- output_path
1473
- ]
1474
 
1475
- print(f"[Export] Running: {' '.join(cmd)}")
1476
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
1477
 
1478
  if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
1479
- print(f"[Export] Success: {output_path}, size: {os.path.getsize(output_path)}")
1480
  return output_path
1481
- else:
1482
- print(f"[Export] FFmpeg error: {result.stderr[:500]}")
1483
- return None
1484
 
1485
- # ์—ฌ๋Ÿฌ ํด๋ฆฝ - concat ์‚ฌ์šฉ
1486
  temp_files = []
1487
  concat_file = os.path.join(temp_dir, 'concat.txt')
1488
 
1489
  for i, clip in enumerate(sorted(video_clips, key=lambda x: x['start'])):
1490
  file_path = uploaded_files.get(clip['filePath'])
1491
  if not file_path or not os.path.exists(file_path):
1492
- print(f"[Export] Skip clip, file not found: {clip['filePath']}")
1493
  continue
1494
 
1495
  temp_out = os.path.join(temp_dir, f'temp_{i}.mp4')
1496
  duration = clip['te'] - clip['ts']
1497
 
1498
  if clip['type'] == 'image':
1499
- cmd = [
1500
- 'ffmpeg', '-y',
1501
- '-loop', '1',
1502
- '-i', file_path,
1503
- '-c:v', 'libx264',
1504
- '-t', str(duration),
1505
- '-pix_fmt', 'yuv420p',
1506
- '-r', '30',
1507
- '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2',
1508
- temp_out
1509
- ]
1510
  else:
1511
- cmd = [
1512
- 'ffmpeg', '-y',
1513
- '-i', file_path,
1514
- '-ss', str(clip['ts']),
1515
- '-t', str(duration),
1516
- '-c:v', 'libx264',
1517
- '-preset', 'fast',
1518
- '-c:a', 'aac',
1519
- '-r', '30',
1520
- '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2',
1521
- temp_out
1522
- ]
1523
 
1524
- print(f"[Export] Processing clip {i}: {' '.join(cmd)}")
1525
  subprocess.run(cmd, capture_output=True, timeout=120)
1526
-
1527
- if os.path.exists(temp_out) and os.path.getsize(temp_out) > 0:
1528
  temp_files.append(temp_out)
1529
- print(f"[Export] Clip {i} done: {temp_out}")
1530
 
1531
  if not temp_files:
1532
- print("[Export] No temp files created")
1533
  return None
1534
 
1535
- # concat ํŒŒ์ผ ์ƒ์„ฑ
1536
  with open(concat_file, 'w') as f:
1537
  for tf in temp_files:
1538
  f.write(f"file '{tf}'\n")
1539
 
1540
- # ํ•ฉ์น˜๊ธฐ
1541
- cmd = [
1542
- 'ffmpeg', '-y',
1543
- '-f', 'concat',
1544
- '-safe', '0',
1545
- '-i', concat_file,
1546
- '-c:v', 'libx264',
1547
- '-preset', 'fast',
1548
- '-c:a', 'aac',
1549
- '-movflags', '+faststart',
1550
- output_path
1551
- ]
1552
-
1553
- print(f"[Export] Concat: {' '.join(cmd)}")
1554
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
1555
 
1556
- # ์ž„์‹œ ํŒŒ์ผ ์ •๋ฆฌ
1557
  for tf in temp_files:
1558
- try:
1559
- os.remove(tf)
1560
- except:
1561
- pass
1562
 
1563
  if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
1564
- print(f"[Export] Final success: {output_path}, size: {os.path.getsize(output_path)}")
1565
  return output_path
1566
- else:
1567
- print(f"[Export] Concat error: {result.stderr[:500]}")
1568
- return None
1569
 
1570
  except Exception as e:
1571
  print(f"[Export] Error: {e}")
1572
- import traceback
1573
- traceback.print_exc()
1574
  return None
1575
 
1576
-
1577
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค
1578
- with gr.Blocks(title="Video Editor") as demo:
1579
- gr.Markdown("## ๐ŸŽฌ Video Editor - MP4 ๋‚ด๋ณด๋‚ด๊ธฐ")
1580
- gr.Markdown("**์‚ฌ์šฉ๋ฒ•**: 1๏ธโƒฃ ํŒŒ์ผ ์—…๋กœ๋“œ โ†’ 2๏ธโƒฃ ์—๋””ํ„ฐ์—์„œ ํŽธ์ง‘ โ†’ 3๏ธโƒฃ 'ํƒ€์ž„๋ผ์ธ ๋ณต์‚ฌ' ํด๋ฆญ โ†’ 4๏ธโƒฃ ์•„๋ž˜ ํ…์ŠคํŠธ๋ฐ•์Šค์— ๋ถ™์—ฌ๋„ฃ๊ธฐ โ†’ 5๏ธโƒฃ 'MP4 ๋‚ด๋ณด๋‚ด๊ธฐ' ํด๋ฆญ")
1581
 
1582
- file_input = gr.File(
1583
- label="๐Ÿ“ ํŒŒ์ผ ์—…๋กœ๋“œ (๋™์˜์ƒ, ์ด๋ฏธ์ง€, ์˜ค๋””์˜ค)",
1584
- file_count="multiple",
1585
- file_types=["video", "image", "audio"]
1586
- )
 
1587
 
1588
  with gr.Row():
1589
- export_data = gr.Textbox(
1590
- label="๐Ÿ“‹ ํƒ€์ž„๋ผ์ธ ๋ฐ์ดํ„ฐ",
1591
- placeholder="์—๋””ํ„ฐ์—์„œ 'ํƒ€์ž„๋ผ์ธ ๋ณต์‚ฌ' ํด๋ฆญ ํ›„ ์—ฌ๊ธฐ์— Ctrl+V๋กœ ๋ถ™์—ฌ๋„ฃ๊ธฐ",
1592
- lines=2,
1593
- scale=4
1594
- )
1595
- export_btn = gr.Button("๐ŸŽฌ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ", variant="primary", scale=1)
1596
 
1597
  mp4_output = gr.File(label="๐Ÿ“ฅ MP4 ๋‹ค์šด๋กœ๋“œ")
1598
 
1599
- editor = gr.HTML(value=make_iframe([]))
1600
-
1601
- # ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
1602
- file_input.change(
1603
- fn=lambda x: make_iframe(process_files(x)),
1604
- inputs=[file_input],
1605
- outputs=[editor]
1606
- )
1607
-
1608
- export_btn.click(
1609
- fn=export_mp4,
1610
- inputs=[export_data],
1611
- outputs=[mp4_output]
1612
- )
1613
-
1614
 
1615
  if __name__ == "__main__":
1616
- demo.launch()
1617
-
 
1
  """
2
  Simple Video Editor - Canvas ๊ธฐ๋ฐ˜ ๋ Œ๋”๋ง
3
+ ๋นˆ ํ”„๋ ˆ์ž„ ๋ฌธ์ œ ์™„์ „ ํ•ด๊ฒฐ + ์„œ๋ฒ„ ์‚ฌ์ด๋“œ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ
4
  """
5
 
6
  import gradio as gr
 
12
  import shutil
13
  import time
14
 
15
+ # ์„œ๋ฒ„ ์‚ฌ์ด๋“œ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ์šฉ
16
  UPLOAD_DIR = tempfile.mkdtemp()
17
  uploaded_files = {} # {filename: filepath}
18
 
 
22
  <head>
23
  <meta charset="UTF-8">
24
  <style>
25
+ *{{margin:0;padding:0;box-sizing:border-box}}
26
+ body{{font-family:-apple-system,BlinkMacSystemFont,sans-serif;background:#f5f5f7;font-size:13px}}
27
+ .editor{{display:flex;flex-direction:column;height:100vh}}
28
+ .toolbar{{height:44px;background:#fff;border-bottom:1px solid #ddd;display:flex;align-items:center;justify-content:space-between;padding:0 12px}}
29
+ .toolbar-title{{font-size:15px;font-weight:600}}
30
+ .btn{{padding:5px 10px;border:none;border-radius:5px;cursor:pointer;font-size:11px;font-weight:500}}
31
+ .btn-secondary{{background:#f0f0f0;color:#333}}
32
+ .btn-secondary:hover{{background:#e0e0e0}}
33
+ .btn-success{{background:#10b981;color:#fff}}
34
+ .btn-danger{{background:#ef4444;color:#fff}}
35
+ .main{{display:flex;flex:1;overflow:hidden}}
36
+ .library{{width:160px;background:#fff;border-right:1px solid #ddd;display:flex;flex-direction:column}}
37
+ .lib-header{{padding:8px;border-bottom:1px solid #eee;font-size:10px;font-weight:600;color:#666}}
38
+ .lib-content{{flex:1;overflow-y:auto;padding:6px}}
39
+ .lib-hint{{text-align:center;padding:15px;color:#999;font-size:10px}}
40
+ .media-grid{{display:grid;grid-template-columns:repeat(2,1fr);gap:4px}}
41
+ .media-item{{aspect-ratio:16/9;background:#f0f0f0;border-radius:4px;overflow:hidden;cursor:grab;position:relative;border:1px solid #e0e0e0}}
42
+ .media-item:hover{{border-color:#6366f1}}
43
+ .media-item img{{width:100%;height:100%;object-fit:cover}}
44
+ .media-item-icon{{width:100%;height:100%;display:flex;align-items:center;justify-content:center;font-size:18px}}
45
+ .media-item-dur{{position:absolute;top:2px;right:2px;background:rgba(0,0,0,0.7);padding:1px 3px;border-radius:2px;font-size:8px;color:#fff}}
46
+ .preview-area{{flex:1;display:flex;flex-direction:column;background:#1a1a1a;margin:6px;border-radius:8px;overflow:hidden}}
47
+ .preview-box{{flex:1;display:flex;align-items:center;justify-content:center;background:#000}}
48
+ #previewCanvas{{max-width:100%;max-height:100%;background:#000}}
49
+ .controls{{height:45px;background:#222;display:flex;align-items:center;justify-content:center;gap:6px}}
50
+ .ctrl-btn{{width:28px;height:28px;border:none;border-radius:50%;background:rgba(255,255,255,0.1);color:#fff;cursor:pointer;font-size:12px}}
51
+ .ctrl-btn:hover{{background:rgba(255,255,255,0.2)}}
52
+ .ctrl-btn.play{{width:36px;height:36px;background:#6366f1}}
53
+ .time-display{{font-family:monospace;font-size:10px;color:#aaa;min-width:90px;text-align:center}}
54
+ .props{{width:140px;background:#fff;border-left:1px solid #ddd}}
55
+ .props-header{{padding:8px;border-bottom:1px solid #eee;font-size:10px;font-weight:600;color:#666}}
56
+ .props-content{{padding:8px}}
57
+ .no-sel{{color:#999;text-align:center;padding:15px;font-size:10px}}
58
+ .prop-group{{margin-bottom:10px}}
59
+ .prop-label{{font-size:9px;color:#666;margin-bottom:2px}}
60
+ .prop-input{{width:100%;padding:4px;border:1px solid #ddd;border-radius:3px;font-size:10px}}
61
+ .timeline{{height:140px;background:#fff;border-top:1px solid #ddd;display:flex;flex-direction:column}}
62
+ .tl-toolbar{{height:28px;background:#fafafa;border-bottom:1px solid #eee;display:flex;align-items:center;padding:0 6px;gap:4px}}
63
+ .tl-toolbar .btn{{padding:2px 6px;font-size:9px}}
64
+ .tl-zoom{{display:flex;align-items:center;gap:3px;margin-left:auto;font-size:9px;color:#666}}
65
+ .tl-zoom input{{width:50px}}
66
+ .tl-container{{flex:1;overflow-x:auto;position:relative}}
67
+ .tl-ruler{{height:18px;background:#fff;border-bottom:1px solid #eee}}
68
+ .tl-tracks{{position:relative}}
69
+ .tl-track{{height:45px;border-bottom:1px solid #eee;display:flex}}
70
+ .tl-track:nth-child(2){{background:#fffbeb}}
71
+ .track-label{{width:50px;padding:0 4px;font-size:8px;color:#666;background:#fafafa;display:flex;align-items:center;border-right:1px solid #eee}}
72
+ .track-content{{flex:1;position:relative;min-width:600px}}
73
+ .clip{{position:absolute;height:36px;top:4px;border-radius:4px;cursor:grab;display:flex;align-items:center;overflow:hidden}}
74
+ .clip:hover{{box-shadow:0 0 0 2px #6366f1}}
75
+ .clip.selected{{box-shadow:0 0 0 2px #6366f1}}
76
+ .clip.video{{background:linear-gradient(135deg,#818cf8,#6366f1)}}
77
+ .clip.image{{background:linear-gradient(135deg,#34d399,#10b981)}}
78
+ .clip.audio{{background:linear-gradient(135deg,#fbbf24,#f59e0b)}}
79
+ .clip-thumb{{width:36px;height:100%;object-fit:cover}}
80
+ .clip-info{{padding:0 4px;flex:1;overflow:hidden}}
81
+ .clip-name{{font-size:8px;color:#fff;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}}
82
+ .clip-dur{{font-size:7px;color:rgba(255,255,255,0.7)}}
83
+ .clip-handle{{position:absolute;top:0;bottom:0;width:8px;background:rgba(255,255,255,0.5);cursor:ew-resize;opacity:0}}
84
+ .clip:hover .clip-handle{{opacity:1}}
85
+ .clip-handle-l{{left:0;border-radius:4px 0 0 4px}}
86
+ .clip-handle-r{{right:0;border-radius:0 4px 4px 0}}
87
+ .playhead{{position:absolute;top:0;bottom:0;width:2px;background:#ef4444;z-index:10;pointer-events:none}}
88
+ .playhead::before{{content:"";position:absolute;top:0;left:-4px;border-left:5px solid transparent;border-right:5px solid transparent;border-top:7px solid #ef4444}}
89
+ .drop-zone{{background:rgba(99,102,241,0.1)!important;outline:2px dashed #6366f1!important}}
90
+ .status{{height:20px;background:#f5f5f5;border-top:1px solid #ddd;display:flex;align-items:center;padding:0 8px;font-size:9px;color:#666}}
91
+ .ctx-menu{{position:fixed;background:#fff;border:1px solid #ddd;border-radius:5px;padding:3px 0;min-width:100px;z-index:1000;box-shadow:0 4px 12px rgba(0,0,0,0.15);display:none}}
92
+ .ctx-item{{padding:5px 10px;cursor:pointer;font-size:10px}}
93
+ .ctx-item:hover{{background:#f5f5f5}}
94
+ .ctx-item.danger{{color:#ef4444}}
95
+ .modal{{position:fixed;inset:0;background:rgba(0,0,0,0.5);display:flex;align-items:center;justify-content:center;z-index:1000}}
96
+ .modal-box{{background:#fff;border-radius:8px;padding:16px;min-width:240px;text-align:center}}
97
+ .modal-box h3{{margin-bottom:8px;font-size:13px}}
98
+ .progress{{height:5px;background:#eee;border-radius:3px;overflow:hidden;margin:10px 0}}
99
+ .progress-bar{{height:100%;background:#6366f1;transition:width 0.2s}}
100
+ .hidden-media{{position:absolute;left:-9999px;top:-9999px;width:1px;height:1px;opacity:0;pointer-events:none}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  </style>
102
  </head>
103
  <body>
104
  <div class="editor">
105
+ <div class="toolbar">
106
+ <div class="toolbar-title">๐ŸŽฌ Video Editor</div>
107
+ <div style="display:flex;gap:4px">
108
+ <button class="btn btn-secondary" onclick="undo()">โ†ฉ ์‹คํ–‰์ทจ์†Œ</button>
109
+ <button class="btn btn-success" onclick="exportVideo()">๐Ÿ“ฅ ๋‚ด๋ณด๋‚ด๊ธฐ</button>
110
+ </div>
111
+ </div>
112
+ <div class="main">
113
+ <div class="library">
114
+ <div class="lib-header">๐Ÿ“ ๋ฏธ๋””์–ด</div>
115
+ <div class="lib-content">
116
+ <div class="lib-hint" id="hint">ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜์„ธ์š”</div>
117
+ <div class="media-grid" id="mediaGrid"></div>
118
+ </div>
119
+ </div>
120
+ <div class="preview-area">
121
+ <div class="preview-box">
122
+ <canvas id="previewCanvas" width="640" height="360"></canvas>
123
+ </div>
124
+ <div class="controls">
125
+ <button class="ctrl-btn" onclick="seek(0)">โฎ</button>
126
+ <button class="ctrl-btn" onclick="seek(S.time-5)">โช</button>
127
+ <button class="ctrl-btn play" onclick="togglePlay()" id="playBtn">โ–ถ</button>
128
+ <button class="ctrl-btn" onclick="seek(S.time+5)">โฉ</button>
129
+ <button class="ctrl-btn" onclick="seek(S.dur)">โญ</button>
130
+ <div class="time-display"><span id="curT">00:00.00</span> / <span id="totT">00:00.00</span></div>
131
+ <button class="ctrl-btn" onclick="toggleMute()" id="muteBtn">๐Ÿ”Š</button>
132
+ </div>
133
+ </div>
134
+ <div class="props">
135
+ <div class="props-header">โš™๏ธ ์†์„ฑ</div>
136
+ <div class="props-content" id="propsBox"><div class="no-sel">ํด๋ฆฝ ์„ ํƒ</div></div>
137
+ </div>
138
+ </div>
139
+ <div class="timeline">
140
+ <div class="tl-toolbar">
141
+ <button class="btn btn-secondary" onclick="splitClip()">โœ‚ ์ž๋ฅด๊ธฐ</button>
142
+ <button class="btn btn-secondary" onclick="dupeClip()">๐Ÿ“‹ ๋ณต์ œ</button>
143
+ <button class="btn btn-danger" onclick="delClip()">๐Ÿ—‘ ์‚ญ์ œ</button>
144
+ <div class="tl-zoom">๐Ÿ”<input type="range" min="0.5" max="3" step="0.1" value="1" oninput="setZoom(this.value)"></div>
145
+ </div>
146
+ <div class="tl-container" id="tlBox" onclick="tlClick(event)">
147
+ <div class="tl-ruler" id="ruler"></div>
148
+ <div class="tl-tracks">
149
+ <div class="tl-track"><div class="track-label">๐ŸŽฌ ์˜์ƒ</div><div class="track-content" id="t0"></div></div>
150
+ <div class="tl-track"><div class="track-label">๐ŸŽต ์˜ค๋””์˜ค</div><div class="track-content" id="t1"></div></div>
151
+ </div>
152
+ <div class="playhead" id="playhead" style="left:50px"></div>
153
+ </div>
154
+ </div>
155
+ <div class="status" id="status">์ค€๋น„๋จ</div>
 
 
 
 
 
 
 
 
 
 
 
 
156
  </div>
157
  <div class="ctx-menu" id="ctx">
158
+ <div class="ctx-item" onclick="splitClip()">โœ‚ ์ž๋ฅด๊ธฐ</div>
159
+ <div class="ctx-item" onclick="dupeClip()">๐Ÿ“‹ ๋ณต์ œ</div>
160
+ <div class="ctx-item danger" onclick="delClip()">๐Ÿ—‘ ์‚ญ์ œ</div>
161
+ </div>
162
+ <div class="modal" id="exportModal" style="display:none">
163
+ <div class="modal-box">
164
+ <h3>๐ŸŽฌ ์˜์ƒ ๋‚ด๋ณด๋‚ด๊ธฐ</h3>
165
+ <p id="exportMsg">์ค€๋น„์ค‘...</p>
166
+ <div class="progress"><div class="progress-bar" id="exportBar"></div></div>
167
+ <button class="btn btn-secondary" onclick="cancelExport()">์ทจ์†Œ</button>
168
+ </div>
169
  </div>
170
  <div id="hiddenMedia" class="hidden-media"></div>
 
171
  <script>
172
+ var S={{
173
+ media:[],
174
+ clips:[],
175
+ sel:null,
176
+ playing:false,
177
+ muted:false,
178
+ time:0,
179
+ dur:0,
180
+ zoom:1,
181
+ pps:80,
182
+ history:[],
183
+ animId:null,
184
+ cancelled:false,
185
+ els:{{}},
186
+ canvas:null,
187
+ ctx:null,
188
+ lastClipId:null
189
  }};
190
 
191
+ function init(){{
192
+ S.canvas=document.getElementById('previewCanvas');
193
+ S.ctx=S.canvas.getContext('2d');
194
+ drawPlaceholder();
195
+ }}
196
+
197
+ function id(){{return Math.random().toString(36).substr(2,9)}}
198
+ function fmt(t){{if(!t||isNaN(t))t=0;var m=Math.floor(t/60),s=Math.floor(t%60),ms=Math.floor((t%1)*100);return String(m).padStart(2,'0')+':'+String(s).padStart(2,'0')+'.'+String(ms).padStart(2,'0')}}
199
+ function r(n){{return Math.round(n*1000)/1000}}
200
+ function stat(m){{document.getElementById('status').textContent=m}}
201
+ function save(){{S.history.push(JSON.stringify(S.clips));if(S.history.length>30)S.history.shift()}}
202
+
203
+ function drawPlaceholder(){{
204
+ S.ctx.fillStyle='#000';
205
+ S.ctx.fillRect(0,0,640,360);
206
+ S.ctx.fillStyle='#444';
207
+ S.ctx.font='14px sans-serif';
208
+ S.ctx.textAlign='center';
209
+ S.ctx.fillText('ํƒ€์ž„๋ผ์ธ์— ๋ฏธ๋””์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”',320,180);
210
+ }}
211
+
212
+ function addMedia(name,type,url,filePath){{
213
+ var m={{id:id(),name:name,type:type,url:url,filePath:filePath||name,dur:type==='image'?5:0,thumb:type==='image'?url:null}};
214
+ S.media.push(m);
215
+ var container=document.getElementById('hiddenMedia');
216
+ if(type==='video'){{
217
+ var v=document.createElement('video');
218
+ v.src=url;
219
+ v.muted=true;
220
+ v.playsInline=true;
221
+ v.preload='auto';
222
+ v.crossOrigin='anonymous';
223
+ container.appendChild(v);
224
+ S.els[m.id]=v;
225
+ v.onloadedmetadata=function(){{
226
+ m.dur=r(v.duration);
227
+ renderLib();
228
+ v.currentTime=0.5;
229
+ }};
230
+ v.onseeked=function(){{
231
+ if(!m.thumb){{
232
+ try{{
233
+ var c=document.createElement('canvas');
234
+ c.width=160;c.height=90;
235
+ c.getContext('2d').drawImage(v,0,0,160,90);
236
+ m.thumb=c.toDataURL();
237
+ renderLib();
238
+ }}catch(e){{}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  }}
240
+ }};
241
+ }}else if(type==='audio'){{
242
+ var a=document.createElement('audio');
243
+ a.src=url;
244
+ a.preload='auto';
245
+ container.appendChild(a);
246
+ S.els[m.id]=a;
247
+ a.onloadedmetadata=function(){{m.dur=r(a.duration);renderLib()}};
248
+ }}else if(type==='image'){{
249
+ var img=new Image();
250
+ img.src=url;
251
+ img.crossOrigin='anonymous';
252
+ S.els[m.id]=img;
253
+ }}
254
+ renderLib();
255
+ stat('๋ฏธ๋””์–ด ์ถ”๊ฐ€: '+name);
256
+ setTimeout(function(){{addClip(m)}},400);
257
+ }}
258
+
259
+ function renderLib(){{
260
+ var g=document.getElementById('mediaGrid');
261
+ var h=document.getElementById('hint');
262
+ h.style.display=S.media.length?'none':'block';
263
+ g.innerHTML='';
264
+ S.media.forEach(function(m){{
265
+ var d=document.createElement('div');
266
+ d.className='media-item';
267
+ d.draggable=true;
268
+ d.ondblclick=function(){{addClip(m)}};
269
+ d.ondragstart=function(e){{e.dataTransfer.setData('mid',m.id)}};
270
+ var th=m.thumb?'<img src="'+m.thumb+'">':'<div class="media-item-icon">'+(m.type==='video'?'๐ŸŽฌ':m.type==='audio'?'๐ŸŽต':'๐Ÿ–ผ')+'</div>';
271
+ d.innerHTML=th+(m.dur?'<div class="media-item-dur">'+fmt(m.dur)+'</div>':'');
272
+ g.appendChild(d);
273
+ }});
274
  }}
275
 
276
+ function trackEnd(tr){{
277
+ var end=0;
278
+ for(var i=0;i<S.clips.length;i++){{
279
+ var c=S.clips[i];
280
+ if(c.track===tr){{
281
+ var e=r(c.start+(c.te-c.ts));
282
+ if(e>end)end=e;
283
+ }}
284
+ }}
285
+ return end;
286
+ }}
287
+
288
+ function addClip(m,at){{
289
+ save();
290
+ var tr=m.type==='audio'?1:0;
291
+ var st=at!==undefined?r(at):trackEnd(tr);
292
+ S.clips.push({{
293
+ id:id(),
294
+ mid:m.id,
295
+ name:m.name,
296
+ type:m.type,
297
+ track:tr,
298
+ start:st,
299
+ dur:m.dur,
300
+ ts:0,
301
+ te:m.dur,
302
+ vol:1,
303
+ filePath:m.filePath
304
+ }});
305
+ renderTL();
306
+ updateDur();
307
+ stat('ํด๋ฆฝ ์ถ”๊ฐ€: '+m.name);
308
+ drawFrame();
309
+ }}
310
+
311
+ function renderTL(){{
312
+ ['t0','t1'].forEach(function(tid){{document.getElementById(tid).innerHTML=''}});
313
+ S.clips.forEach(function(c){{
314
+ var tr=document.getElementById('t'+c.track);
315
+ var el=document.createElement('div');
316
+ el.className='clip '+c.type+(S.sel===c.id?' selected':'');
317
+ var len=r(c.te-c.ts);
318
+ el.style.left=r(c.start*S.pps*S.zoom)+'px';
319
+ el.style.width=Math.max(25,r(len*S.pps*S.zoom))+'px';
320
+ el.draggable=true;
321
+ el.onclick=function(e){{e.stopPropagation();selClip(c.id)}};
322
+ el.oncontextmenu=function(e){{e.preventDefault();selClip(c.id);showCtx(e.clientX,e.clientY)}};
323
+ el.ondragstart=function(e){{e.dataTransfer.setData('cid',c.id);e.dataTransfer.setData('ox',e.offsetX)}};
324
+ var m=S.media.find(function(x){{return x.id===c.mid}});
325
+ var th=m&&m.thumb?'<img class="clip-thumb" src="'+m.thumb+'">':'';
326
+ el.innerHTML=th+'<div class="clip-info"><div class="clip-name">'+c.name+'</div><div class="clip-dur">'+fmt(len)+'</div></div><div class="clip-handle clip-handle-l"></div><div class="clip-handle clip-handle-r"></div>';
327
+ el.querySelector('.clip-handle-l').onmousedown=function(e){{e.stopPropagation();startTrim(c.id,'l',e)}};
328
+ el.querySelector('.clip-handle-r').onmousedown=function(e){{e.stopPropagation();startTrim(c.id,'r',e)}};
329
+ tr.appendChild(el);
330
+ }});
331
+ renderRuler();
332
+ setupDrop();
333
+ }}
334
+
335
+ function renderRuler(){{
336
+ var ru=document.getElementById('ruler');
337
+ var w=Math.max(r(S.dur*S.pps*S.zoom)+150,600);
338
+ ru.style.width=w+'px';
339
+ var h='<svg width="100%" height="18" style="position:absolute;left:50px">';
340
+ var step=S.zoom<0.7?5:S.zoom<1.5?2:1;
341
+ for(var i=0;i<=S.dur+10;i+=step){{
342
+ var x=r(i*S.pps*S.zoom);
343
+ h+='<line x1="'+x+'" y1="13" x2="'+x+'" y2="18" stroke="#ccc"/>';
344
+ h+='<text x="'+x+'" y="10" fill="#999" font-size="8" text-anchor="middle">'+fmt(i)+'</text>';
345
+ }}
346
+ ru.innerHTML=h+'</svg>';
347
+ }}
348
+
349
+ function setupDrop(){{
350
+ ['t0','t1'].forEach(function(tid,idx){{
351
+ var tr=document.getElementById(tid);
352
+ tr.ondragover=function(e){{e.preventDefault();tr.classList.add('drop-zone')}};
353
+ tr.ondragleave=function(){{tr.classList.remove('drop-zone')}};
354
+ tr.ondrop=function(e){{
355
+ e.preventDefault();
356
+ tr.classList.remove('drop-zone');
357
+ var rect=tr.getBoundingClientRect();
358
+ var t=r(Math.max(0,(e.clientX-rect.left)/(S.pps*S.zoom)));
359
+ var mid=e.dataTransfer.getData('mid');
360
+ var cid=e.dataTransfer.getData('cid');
361
+ var ox=parseFloat(e.dataTransfer.getData('ox')||0);
362
+ if(mid){{
363
+ var m=S.media.find(function(x){{return x.id===mid}});
364
+ if(m)addClip(m,t);
365
+ }}else if(cid){{
366
+ save();
367
+ var c=S.clips.find(function(x){{return x.id===cid}});
368
+ if(c){{
369
+ c.start=r(Math.max(0,t-ox/(S.pps*S.zoom)));
370
+ c.track=c.type==='audio'?1:idx;
371
+ renderTL();
372
+ updateDur();
373
+ drawFrame();
374
  }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375
  }}
376
+ }};
377
+ }});
 
 
 
378
  }}
379
 
380
+ var trimData=null;
381
+ function startTrim(cid,side,e){{
382
+ e.preventDefault();
383
+ var c=S.clips.find(function(x){{return x.id===cid}});
384
+ if(!c)return;
385
+ save();
386
+ trimData={{cid:cid,side:side,sx:e.clientX,ots:c.ts,ote:c.te,ost:c.start}};
387
+ document.addEventListener('mousemove',doTrim);
388
+ document.addEventListener('mouseup',endTrim);
389
+ }}
390
+ function doTrim(e){{
391
+ if(!trimData)return;
392
+ var c=S.clips.find(function(x){{return x.id===trimData.cid}});
393
+ if(!c)return;
394
+ var dx=e.clientX-trimData.sx;
395
+ var dt=r(dx/(S.pps*S.zoom));
396
+ if(trimData.side==='l'){{
397
+ var newTs=Math.max(0,Math.min(c.te-0.1,trimData.ots+dt));
398
+ c.ts=r(newTs);
399
+ c.start=r(trimData.ost+(newTs-trimData.ots));
400
+ }}else{{
401
+ c.te=r(Math.max(c.ts+0.1,Math.min(c.dur,trimData.ote+dt)));
402
  }}
403
+ renderTL();
404
+ updateDur();
405
+ }}
406
+ function endTrim(){{
407
+ trimData=null;
408
+ document.removeEventListener('mousemove',doTrim);
409
+ document.removeEventListener('mouseup',endTrim);
410
+ }}
411
+
412
+ function selClip(cid){{S.sel=cid;renderTL();renderProps()}}
413
+
414
+ function renderProps(){{
415
+ var box=document.getElementById('propsBox');
416
+ var c=S.clips.find(function(x){{return x.id===S.sel}});
417
+ if(!c){{box.innerHTML='<div class="no-sel">ํด๋ฆฝ ์„ ํƒ</div>';return}}
418
+ var len=r(c.te-c.ts);
419
+ box.innerHTML='<div class="prop-group"><div class="prop-label">์ด๋ฆ„</div><input class="prop-input" value="'+c.name+'" onchange="setProp(\\'name\\',this.value)"></div>'+
420
+ '<div class="prop-group"><div class="prop-label">์‹œ์ž‘</div><input class="prop-input" type="number" step="0.1" value="'+c.start+'" onchange="setProp(\\'start\\',parseFloat(this.value))"></div>'+
421
+ '<div class="prop-group"><div class="prop-label">๊ธธ์ด: '+fmt(len)+'</div></div>'+
422
+ (c.type!=='image'?'<div class="prop-group"><div class="prop-label">๋ณผ๋ฅจ '+Math.round(c.vol*100)+'%</div><input class="prop-input" type="range" min="0" max="1" step="0.05" value="'+c.vol+'" oninput="setProp(\\'vol\\',parseFloat(this.value))"></div>':'');
423
+ }}
424
+
425
+ function setProp(p,v){{
426
+ save();
427
+ var c=S.clips.find(function(x){{return x.id===S.sel}});
428
+ if(c){{c[p]=p==='start'?r(v):v;renderTL();updateDur();renderProps();drawFrame()}}
429
+ }}
430
+
431
+ function splitClip(){{
432
+ if(!S.sel){{alert('ํด๋ฆฝ์„ ์„ ํƒํ•˜์„ธ์š”');return}}
433
+ var c=S.clips.find(function(x){{return x.id===S.sel}});
434
+ if(!c)return;
435
+ var cEnd=r(c.start+c.te-c.ts);
436
+ if(S.time<=c.start||S.time>=cEnd){{alert('ํ”Œ๋ ˆ์ดํ—ค๋“œ๊ฐ€ ํด๋ฆฝ ์•ˆ์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค');return}}
437
+ save();
438
+ var splitAt=r(S.time-c.start);
439
+ var c2=JSON.parse(JSON.stringify(c));
440
+ c2.id=id();
441
+ c2.start=r(S.time);
442
+ c2.ts=r(c.ts+splitAt);
443
+ c.te=r(c.ts+splitAt);
444
+ S.clips.push(c2);
445
+ renderTL();updateDur();hideCtx();stat('ํด๋ฆฝ ๋ถ„ํ• ๋จ');
446
+ }}
447
+
448
+ function dupeClip(){{
449
+ if(!S.sel)return;
450
+ var c=S.clips.find(function(x){{return x.id===S.sel}});
451
+ if(!c)return;
452
+ save();
453
+ var len=r(c.te-c.ts);
454
+ var nc=JSON.parse(JSON.stringify(c));
455
+ nc.id=id();
456
+ nc.start=r(c.start+len);
457
+ S.clips.push(nc);
458
+ renderTL();updateDur();hideCtx();stat('ํด๋ฆฝ ๋ณต์ œ๋จ');
459
+ }}
460
+
461
+ function delClip(){{
462
+ if(!S.sel)return;
463
+ save();
464
+ S.clips=S.clips.filter(function(x){{return x.id!==S.sel}});
465
+ S.sel=null;
466
+ renderTL();renderProps();updateDur();hideCtx();stat('ํด๋ฆฝ ์‚ญ์ œ๋จ');
467
+ drawFrame();
468
+ }}
469
+
470
+ function undo(){{if(S.history.length){{S.clips=JSON.parse(S.history.pop());renderTL();updateDur();stat('์‹คํ–‰์ทจ์†Œ');drawFrame()}}}}
471
+
472
+ function updateDur(){{
473
+ var mx=0;
474
+ for(var i=0;i<S.clips.length;i++){{
475
+ var c=S.clips[i];
476
+ var e=r(c.start+c.te-c.ts);
477
+ if(e>mx)mx=e;
478
+ }}
479
+ S.dur=mx;
480
+ document.getElementById('totT').textContent=fmt(mx);
481
+ }}
482
+
483
+ function togglePlay(){{
484
+ S.playing=!S.playing;
485
+ document.getElementById('playBtn').textContent=S.playing?'โธ':'โ–ถ';
486
+ if(S.playing)play();else stop();
487
+ }}
488
+
489
+ function play(){{
490
+ var last=performance.now();
491
+ function loop(now){{
492
+ if(!S.playing)return;
493
+ var dt=(now-last)/1000;
494
+ last=now;
495
+ S.time=S.time+dt;
496
+ if(S.time>=S.dur){{
497
+ S.time=0;
498
+ if(S.dur===0){{S.playing=false;document.getElementById('playBtn').textContent='โ–ถ';return}}
499
+ }}
500
+ updateHead();
501
+ drawFrame();
502
+ S.animId=requestAnimationFrame(loop);
503
+ }}
504
+ S.animId=requestAnimationFrame(loop);
505
+ }}
506
+
507
+ function stop(){{
508
+ if(S.animId){{cancelAnimationFrame(S.animId);S.animId=null}}
509
+ Object.keys(S.els).forEach(function(k){{
510
+ var el=S.els[k];
511
+ if(el&&el.pause)el.pause();
512
+ }});
513
  }}
514
 
515
+ function seek(t){{
516
+ S.time=Math.max(0,Math.min(S.dur||0,t));
517
+ updateHead();
518
+ drawFrame();
519
+ }}
520
+
521
+ function updateHead(){{
522
+ document.getElementById('playhead').style.left=(50+S.time*S.pps*S.zoom)+'px';
523
+ document.getElementById('curT').textContent=fmt(S.time);
524
+ }}
525
+
526
+ function getClipAt(t,type){{
527
+ var sorted=S.clips.filter(function(c){{
528
+ if(type==='visual')return c.type==='video'||c.type==='image';
529
+ if(type==='audio')return c.type==='audio';
530
+ return true;
531
+ }}).sort(function(a,b){{return a.start-b.start}});
532
+ for(var i=0;i<sorted.length;i++){{
533
+ var c=sorted[i];
534
+ var cEnd=c.start+(c.te-c.ts);
535
+ if(t>=c.start&&t<cEnd)return c;
536
+ }}
537
+ return null;
538
+ }}
539
+
540
+ function drawFrame(){{
541
+ var t=S.time;
542
+ var vc=getClipAt(t,'visual');
543
+ S.ctx.fillStyle='#000';
544
+ S.ctx.fillRect(0,0,640,360);
545
+ if(vc){{
546
+ var el=S.els[vc.mid];
547
+ if(el){{
548
+ if(vc.type==='video'){{
549
+ var clipT=t-vc.start+vc.ts;
550
+ if(Math.abs(el.currentTime-clipT)>0.05){{
551
+ el.currentTime=clipT;
552
+ }}
553
+ if(S.playing&&el.paused)el.play().catch(function(){{}});
554
+ if(!S.playing&&!el.paused)el.pause();
555
+ el.volume=S.muted?0:vc.vol;
556
+ }}
557
+ try{{
558
+ var sw=el.videoWidth||el.naturalWidth||el.width||640;
559
+ var sh=el.videoHeight||el.naturalHeight||el.height||360;
560
+ var scale=Math.min(640/sw,360/sh);
561
+ var dw=sw*scale,dh=sh*scale;
562
+ var dx=(640-dw)/2,dy=(360-dh)/2;
563
+ S.ctx.drawImage(el,dx,dy,dw,dh);
564
+ }}catch(e){{}}
565
+ }}
566
+ }}else if(S.clips.length===0){{
567
+ S.ctx.fillStyle='#444';
568
+ S.ctx.font='14px sans-serif';
569
+ S.ctx.textAlign='center';
570
+ S.ctx.fillText('ํƒ€์ž„๋ผ์ธ์— ๋ฏธ๋””์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”',320,180);
571
+ }}
572
+ var audioClips=S.clips.filter(function(c){{
573
+ if(c.type!=='audio')return false;
574
+ var cEnd=c.start+(c.te-c.ts);
575
+ return t>=c.start&&t<cEnd;
576
+ }});
577
+ audioClips.forEach(function(ac){{
578
+ var el=S.els[ac.mid];
579
+ if(el){{
580
+ var clipT=t-ac.start+ac.ts;
581
+ if(Math.abs(el.currentTime-clipT)>0.1)el.currentTime=clipT;
582
+ el.volume=S.muted?0:ac.vol;
583
+ if(S.playing&&el.paused)el.play().catch(function(){{}});
584
+ if(!S.playing&&!el.paused)el.pause();
585
  }}
586
+ }});
587
+ S.clips.forEach(function(c){{
588
+ if(c.type!=='audio')return;
589
+ var cEnd=c.start+(c.te-c.ts);
590
+ if(t<c.start||t>=cEnd){{
591
+ var el=S.els[c.mid];
592
+ if(el&&!el.paused)el.pause();
593
  }}
 
 
 
594
  }});
595
+ if(!vc&&!audioClips.length&&S.clips.length>0){{
596
+ S.ctx.fillStyle='#333';
597
+ S.ctx.font='12px sans-serif';
598
+ S.ctx.textAlign='center';
599
+ S.ctx.fillText('์žฌ์ƒ ์œ„์น˜์— ๋ฏธ๋””์–ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค',320,180);
600
+ }}
601
+ }}
602
+
603
+ function toggleMute(){{S.muted=!S.muted;document.getElementById('muteBtn').textContent=S.muted?'๐Ÿ”‡':'๐Ÿ”Š'}}
604
+ function setZoom(v){{S.zoom=parseFloat(v);renderTL();updateHead()}}
605
+ function tlClick(e){{
606
+ if(e.target.closest('.clip'))return;
607
+ var rect=document.getElementById('tlBox').getBoundingClientRect();
608
+ var scrollL=document.getElementById('tlBox').scrollLeft;
609
+ S.time=Math.max(0,Math.min(S.dur||0,(e.clientX-rect.left-50+scrollL)/(S.pps*S.zoom)));
610
+ updateHead();
611
+ drawFrame();
612
+ }}
613
+
614
+ function showCtx(x,y){{var m=document.getElementById('ctx');m.style.display='block';m.style.left=x+'px';m.style.top=y+'px'}}
615
+ function hideCtx(){{document.getElementById('ctx').style.display='none'}}
616
+ document.addEventListener('click',function(e){{if(!e.target.closest('.ctx-menu'))hideCtx()}});
617
+
618
+ function exportVideo(){{
619
+ if(!S.clips.length){{alert('ํด๋ฆฝ์„ ์ถ”๊ฐ€ํ•˜์„ธ์š”');return}}
620
+ S.cancelled=false;
621
+ document.getElementById('exportModal').style.display='flex';
622
+ document.getElementById('exportBar').style.width='0%';
623
+ document.getElementById('exportMsg').textContent='FFmpeg ๋กœ๋”ฉ ์ค‘...';
624
+ loadFFmpeg().then(doExport).catch(function(e){{
625
+ document.getElementById('exportMsg').textContent='FFmpeg ๋กœ๋“œ ์‹คํŒจ: '+e.message;
626
+ }});
627
  }}
628
 
629
+ var ffmpeg=null;
630
+ async function loadFFmpeg(){{
631
+ if(ffmpeg)return;
632
+ var script=document.createElement('script');
633
+ script.src='https://cdn.jsdelivr.net/npm/@ffmpeg/ffmpeg@0.12.6/dist/umd/ffmpeg.min.js';
634
+ document.head.appendChild(script);
635
+ await new Promise(function(resolve){{script.onload=resolve}});
636
+ var script2=document.createElement('script');
637
+ script2.src='https://cdn.jsdelivr.net/npm/@ffmpeg/util@0.12.1/dist/umd/index.min.js';
638
+ document.head.appendChild(script2);
639
+ await new Promise(function(resolve){{script2.onload=resolve}});
640
+ ffmpeg=new FFmpegWASM.FFmpeg();
641
+ ffmpeg.on('progress',function(p){{
642
+ if(p.progress)document.getElementById('exportMsg').textContent='๋ณ€ํ™˜ ์ค‘... '+Math.round(p.progress*100)+'%';
643
+ }});
644
+ await ffmpeg.load({{
645
+ coreURL:'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.4/dist/umd/ffmpeg-core.js',
646
+ wasmURL:'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.4/dist/umd/ffmpeg-core.wasm'
647
+ }});
648
  }}
649
 
650
+ async function doExport(){{
651
+ var canvas=document.createElement('canvas');
652
+ canvas.width=1280;canvas.height=720;
653
+ var ctx=canvas.getContext('2d');
654
+ var stream=canvas.captureStream(30);
655
+ var opts={{mimeType:'video/webm;codecs=vp8'}};
656
+ if(!MediaRecorder.isTypeSupported(opts.mimeType))opts={{mimeType:'video/webm'}};
657
+ var rec=new MediaRecorder(stream,opts);
658
+ var chunks=[];
659
+ rec.ondataavailable=function(e){{if(e.data.size>0)chunks.push(e.data)}};
660
+ document.getElementById('exportMsg').textContent='๋…นํ™” ์ค‘...';
661
+ rec.start(100);
662
+ var dur=S.dur;
663
+ var start=performance.now();
664
+ await new Promise(function(resolve){{
665
+ function render(){{
666
+ if(S.cancelled){{rec.stop();resolve();return}}
667
+ var t=(performance.now()-start)/1000;
668
+ if(t>=dur){{
669
+ setTimeout(function(){{rec.stop();setTimeout(resolve,300)}},200);
670
+ return;
671
+ }}
672
+ document.getElementById('exportBar').style.width=(t/dur*50)+'%';
673
+ document.getElementById('exportMsg').textContent='๋…นํ™” ์ค‘... '+Math.round(t/dur*100)+'%';
674
+ ctx.fillStyle='#000';
675
+ ctx.fillRect(0,0,1280,720);
676
+ var vc=getClipAt(t,'visual');
677
+ if(vc){{
678
+ var el=S.els[vc.mid];
679
+ if(el){{
680
+ if(vc.type==='video')el.currentTime=t-vc.start+vc.ts;
681
+ try{{
682
+ var sw=el.videoWidth||el.naturalWidth||el.width||1280;
683
+ var sh=el.videoHeight||el.naturalHeight||el.height||720;
684
+ var scale=Math.min(1280/sw,720/sh);
685
+ var dw=sw*scale,dh=sh*scale;
686
+ ctx.drawImage(el,(1280-dw)/2,(720-dh)/2,dw,dh);
687
+ }}catch(e){{}}
688
+ }}
689
+ }}
690
+ requestAnimationFrame(render);
691
+ }}
692
+ requestAnimationFrame(render);
693
+ }});
694
+ if(S.cancelled)return;
695
+ var webmBlob=new Blob(chunks,{{type:'video/webm'}});
696
+ if(webmBlob.size<1000){{document.getElementById('exportMsg').textContent='๋…นํ™” ์‹คํŒจ';return}}
697
+ document.getElementById('exportBar').style.width='50%';
698
+ document.getElementById('exportMsg').textContent='MP4 ๋ณ€ํ™˜ ์ค‘...';
699
+ try{{
700
+ var webmData=new Uint8Array(await webmBlob.arrayBuffer());
701
+ await ffmpeg.writeFile('input.webm',webmData);
702
+ await ffmpeg.exec(['-i','input.webm','-c:v','libx264','-preset','fast','-crf','23','-c:a','aac','-movflags','+faststart','output.mp4']);
703
+ var mp4Data=await ffmpeg.readFile('output.mp4');
704
+ var mp4Blob=new Blob([mp4Data],{{type:'video/mp4'}});
705
+ document.getElementById('exportBar').style.width='100%';
706
+ document.getElementById('exportMsg').textContent='์™„๋ฃŒ! ('+Math.round(mp4Blob.size/1024)+'KB)';
707
+ var a=document.createElement('a');
708
+ a.href=URL.createObjectURL(mp4Blob);
709
+ a.download='video_'+Date.now()+'.mp4';
710
+ a.click();
711
+ await ffmpeg.deleteFile('input.webm');
712
+ await ffmpeg.deleteFile('output.mp4');
713
+ }}catch(e){{
714
+ console.error(e);
715
+ document.getElementById('exportMsg').textContent='MP4 ๋ณ€ํ™˜ ์‹คํŒจ, WebM์œผ๋กœ ๋‹ค์šด๋กœ๋“œ';
716
+ var a=document.createElement('a');
717
+ a.href=URL.createObjectURL(webmBlob);
718
+ a.download='video_'+Date.now()+'.webm';
719
+ a.click();
720
+ }}
721
+ }}
722
+
723
+ function cancelExport(){{S.cancelled=true;document.getElementById('exportModal').style.display='none'}}
724
+
725
+ document.addEventListener('keydown',function(e){{
726
+ if(e.target.tagName==='INPUT')return;
727
+ if(e.code==='Space'){{e.preventDefault();togglePlay()}}
728
+ else if(e.code==='Delete'){{e.preventDefault();delClip()}}
729
+ else if(e.code==='ArrowLeft'){{seek(S.time-0.1)}}
730
+ else if(e.code==='ArrowRight'){{seek(S.time+0.1)}}
731
  }});
732
 
 
733
  init();
734
  renderTL();
735
+ stat('์ค€๋น„๋จ');
736
+ var initData={media_data};
737
+ if(initData&&initData.length)initData.forEach(function(m){{addMedia(m.name,m.type,m.dataUrl,m.filePath)}});
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
738
  </script>
739
  </body>
740
  </html>'''
741
 
742
+ def process_file(files):
 
743
  """ํŒŒ์ผ ์ฒ˜๋ฆฌ ๋ฐ ์„œ๋ฒ„์— ์ €์žฅ"""
744
  global uploaded_files
745
  if not files:
746
  return []
 
747
  results = []
748
  file_list = files if isinstance(files, list) else [files]
 
749
  for f in file_list:
750
  if not f:
751
  continue
752
  path = f.name if hasattr(f, 'name') else f
753
  name = os.path.basename(path)
754
  ext = name.lower().split('.')[-1]
 
755
  if ext in ['mp4', 'webm', 'mov', 'avi', 'mkv']:
756
  t, m = 'video', f'video/{ext}'
757
  elif ext in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
 
761
  else:
762
  continue
763
 
764
+ # ์„œ๋ฒ„์— ํŒŒ์ผ ๋ณต์‚ฌ (MP4 ๋‚ด๋ณด๋‚ด๊ธฐ์šฉ)
765
  dst_path = os.path.join(UPLOAD_DIR, f"{int(time.time()*1000)}_{name}")
766
  shutil.copy(path, dst_path)
767
  uploaded_files[name] = dst_path
 
768
 
 
769
  with open(path, 'rb') as fp:
770
  d = base64.b64encode(fp.read()).decode()
771
+ results.append({'name': name, 'type': t, 'dataUrl': f'data:{m};base64,{d}', 'filePath': name})
 
 
 
 
 
 
 
772
  return results
773
 
 
774
  def make_iframe(data):
775
+ j = json.dumps(data, ensure_ascii=False)
776
+ h = get_editor_html(j).replace("'", "&#39;")
777
+ return f"<iframe srcdoc='{h}' style='width:100%;height:750px;border:none;border-radius:10px'></iframe>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
778
 
779
  def export_mp4(export_json):
780
+ """์„œ๋ฒ„ ์‚ฌ์ด๋“œ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ"""
781
  global uploaded_files
782
 
783
  if not export_json or len(export_json) < 10:
 
784
  return None
785
 
786
  try:
 
788
  clips = data.get('clips', [])
789
 
790
  if not clips:
 
791
  return None
792
 
 
793
  video_clips = [c for c in clips if c['type'] in ['video', 'image']]
794
  if not video_clips:
 
795
  return None
796
 
797
  temp_dir = tempfile.mkdtemp()
798
  output_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4')
799
 
800
+ # ๋‹จ์ผ ํด๋ฆฝ
801
  if len(video_clips) == 1:
802
  clip = video_clips[0]
803
  file_path = uploaded_files.get(clip['filePath'])
804
 
805
  if not file_path or not os.path.exists(file_path):
 
 
806
  return None
807
 
808
  duration = clip['te'] - clip['ts']
809
 
810
  if clip['type'] == 'image':
811
+ cmd = ['ffmpeg', '-y', '-loop', '1', '-i', file_path, '-c:v', 'libx264', '-t', str(duration), '-pix_fmt', 'yuv420p', '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2', output_path]
 
 
 
 
 
 
 
 
 
812
  else:
813
+ cmd = ['ffmpeg', '-y', '-i', file_path, '-ss', str(clip['ts']), '-t', str(duration), '-c:v', 'libx264', '-preset', 'fast', '-crf', '23', '-c:a', 'aac', '-b:a', '128k', '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2', '-movflags', '+faststart', output_path]
 
 
 
 
 
 
 
 
 
 
 
 
 
814
 
815
+ subprocess.run(cmd, capture_output=True, timeout=300)
 
816
 
817
  if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
 
818
  return output_path
819
+ return None
 
 
820
 
821
+ # ์—ฌ๋Ÿฌ ํด๋ฆฝ
822
  temp_files = []
823
  concat_file = os.path.join(temp_dir, 'concat.txt')
824
 
825
  for i, clip in enumerate(sorted(video_clips, key=lambda x: x['start'])):
826
  file_path = uploaded_files.get(clip['filePath'])
827
  if not file_path or not os.path.exists(file_path):
 
828
  continue
829
 
830
  temp_out = os.path.join(temp_dir, f'temp_{i}.mp4')
831
  duration = clip['te'] - clip['ts']
832
 
833
  if clip['type'] == 'image':
834
+ cmd = ['ffmpeg', '-y', '-loop', '1', '-i', file_path, '-c:v', 'libx264', '-t', str(duration), '-pix_fmt', 'yuv420p', '-r', '30', '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2', temp_out]
 
 
 
 
 
 
 
 
 
 
835
  else:
836
+ cmd = ['ffmpeg', '-y', '-i', file_path, '-ss', str(clip['ts']), '-t', str(duration), '-c:v', 'libx264', '-preset', 'fast', '-c:a', 'aac', '-r', '30', '-vf', 'scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2', temp_out]
 
 
 
 
 
 
 
 
 
 
 
837
 
 
838
  subprocess.run(cmd, capture_output=True, timeout=120)
839
+ if os.path.exists(temp_out):
 
840
  temp_files.append(temp_out)
 
841
 
842
  if not temp_files:
 
843
  return None
844
 
 
845
  with open(concat_file, 'w') as f:
846
  for tf in temp_files:
847
  f.write(f"file '{tf}'\n")
848
 
849
+ cmd = ['ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', concat_file, '-c:v', 'libx264', '-preset', 'fast', '-c:a', 'aac', '-movflags', '+faststart', output_path]
850
+ subprocess.run(cmd, capture_output=True, timeout=300)
 
 
 
 
 
 
 
 
 
 
 
 
 
851
 
 
852
  for tf in temp_files:
853
+ try: os.remove(tf)
854
+ except: pass
 
 
855
 
856
  if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
 
857
  return output_path
858
+ return None
 
 
859
 
860
  except Exception as e:
861
  print(f"[Export] Error: {e}")
 
 
862
  return None
863
 
864
+ with gr.Blocks() as demo:
865
+ gr.Markdown("## ๐ŸŽฌ Video Editor")
 
 
 
866
 
867
+ f = gr.File(label="๐Ÿ“ ํŒŒ์ผ ์—…๋กœ๋“œ", file_count="multiple", file_types=["video", "image", "audio"])
868
+ e = gr.HTML(value=make_iframe([]))
869
+
870
+ gr.Markdown("---")
871
+ gr.Markdown("### ๐Ÿ“ฅ ์„œ๋ฒ„ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ (์„ ํƒ์‚ฌํ•ญ)")
872
+ gr.Markdown("์—๋””ํ„ฐ ๋‚ด '๋‚ด๋ณด๋‚ด๊ธฐ' ๋ฒ„ํŠผ์ด ์•ˆ๋  ๋•Œ ์‚ฌ์šฉ. ํƒ€์ž„๋ผ์ธ JSON์„ ๋ถ™์—ฌ๋„ฃ์œผ์„ธ์š”.")
873
 
874
  with gr.Row():
875
+ export_data = gr.Textbox(label="ํƒ€์ž„๋ผ์ธ JSON", placeholder='{"clips":[...]}', lines=2, scale=4)
876
+ export_btn = gr.Button("๐ŸŽฌ MP4 ์ƒ์„ฑ", variant="primary", scale=1)
 
 
 
 
 
877
 
878
  mp4_output = gr.File(label="๐Ÿ“ฅ MP4 ๋‹ค์šด๋กœ๋“œ")
879
 
880
+ f.change(fn=lambda x: make_iframe(process_file(x)), inputs=[f], outputs=[e])
881
+ export_btn.click(fn=export_mp4, inputs=[export_data], outputs=[mp4_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
882
 
883
  if __name__ == "__main__":
884
+ demo.launch()