seawolf2357 commited on
Commit
5fefdc5
ยท
verified ยท
1 Parent(s): 3156662

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +1109 -0
app.py ADDED
@@ -0,0 +1,1109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Simple Video Editor - Canvas ๊ธฐ๋ฐ˜ ๋ Œ๋”๋ง
3
+ ๋นˆ ํ”„๋ ˆ์ž„ ๋ฌธ์ œ ์™„์ „ ํ•ด๊ฒฐ + ์„œ๋ฒ„ ์‚ฌ์ด๋“œ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ
4
+ """
5
+
6
+ import gradio as gr
7
+ import base64
8
+ import os
9
+ import json
10
+ import subprocess
11
+ import tempfile
12
+ import shutil
13
+ import time
14
+
15
+ # ์„œ๋ฒ„ ์‚ฌ์ด๋“œ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ์šฉ
16
+ UPLOAD_DIR = tempfile.mkdtemp()
17
+ uploaded_files = {} # {filename: filepath}
18
+
19
+ def get_editor_html(media_data="[]"):
20
+ return f'''<!DOCTYPE html>
21
+ <html lang="ko">
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;overflow:hidden}}
48
+ #previewCanvas{{background:#000;box-shadow:0 0 20px rgba(0,0,0,0.5)}}
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:8px;align-items:center">
108
+ <select id="ratioSelect" onchange="setRatio(this.value)" style="padding:4px 8px;border-radius:4px;border:1px solid #ddd;font-size:11px">
109
+ <option value="16:9">16:9 (๊ฐ€๋กœ)</option>
110
+ <option value="9:16">9:16 (์„ธ๋กœ)</option>
111
+ <option value="1:1">1:1 (์ •์‚ฌ๊ฐ)</option>
112
+ <option value="4:3">4:3</option>
113
+ <option value="4:5">4:5 (์ธ์Šคํƒ€)</option>
114
+ </select>
115
+ <select id="fillMode" onchange="setFillMode(this.value)" style="padding:4px 8px;border-radius:4px;border:1px solid #ddd;font-size:11px">
116
+ <option value="fit">๋งž์ถค (์—ฌ๋ฐฑ)</option>
117
+ <option value="fill">์ฑ„์šฐ๊ธฐ (ํ™•๋Œ€)</option>
118
+ <option value="blur">๋ธ”๋Ÿฌ ๋ฐฐ๊ฒฝ</option>
119
+ </select>
120
+ <button class="btn btn-secondary" onclick="undo()">โ†ฉ ์‹คํ–‰์ทจ์†Œ</button>
121
+ <button class="btn btn-success" onclick="exportVideo()">๐Ÿ“ฅ ๋‚ด๋ณด๋‚ด๊ธฐ</button>
122
+ </div>
123
+ </div>
124
+ <div class="main">
125
+ <div class="library">
126
+ <div class="lib-header">๐Ÿ“ ๋ฏธ๋””์–ด</div>
127
+ <div class="lib-content">
128
+ <div class="lib-hint" id="hint">ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜์„ธ์š”</div>
129
+ <div class="media-grid" id="mediaGrid"></div>
130
+ </div>
131
+ </div>
132
+ <div class="preview-area">
133
+ <div class="preview-box">
134
+ <canvas id="previewCanvas" width="640" height="360"></canvas>
135
+ </div>
136
+ <div class="controls">
137
+ <button class="ctrl-btn" onclick="seek(0)">โฎ</button>
138
+ <button class="ctrl-btn" onclick="seek(S.time-5)">โช</button>
139
+ <button class="ctrl-btn play" onclick="togglePlay()" id="playBtn">โ–ถ</button>
140
+ <button class="ctrl-btn" onclick="seek(S.time+5)">โฉ</button>
141
+ <button class="ctrl-btn" onclick="seek(S.dur)">โญ</button>
142
+ <div class="time-display"><span id="curT">00:00.00</span> / <span id="totT">00:00.00</span></div>
143
+ <button class="ctrl-btn" onclick="toggleMute()" id="muteBtn">๐Ÿ”Š</button>
144
+ </div>
145
+ </div>
146
+ <div class="props">
147
+ <div class="props-header">โš™๏ธ ์†์„ฑ</div>
148
+ <div class="props-content" id="propsBox"><div class="no-sel">ํด๋ฆฝ ์„ ํƒ</div></div>
149
+ </div>
150
+ </div>
151
+ <div class="timeline">
152
+ <div class="tl-toolbar">
153
+ <button class="btn btn-secondary" onclick="splitClip()">โœ‚ ์ž๋ฅด๊ธฐ</button>
154
+ <button class="btn btn-secondary" onclick="dupeClip()">๐Ÿ“‹ ๋ณต์ œ</button>
155
+ <button class="btn btn-danger" onclick="delClip()">๐Ÿ—‘ ์‚ญ์ œ</button>
156
+ <div class="tl-zoom">๐Ÿ”<input type="range" min="0.5" max="3" step="0.1" value="1" oninput="setZoom(this.value)"></div>
157
+ </div>
158
+ <div class="tl-container" id="tlBox" onclick="tlClick(event)">
159
+ <div class="tl-ruler" id="ruler"></div>
160
+ <div class="tl-tracks">
161
+ <div class="tl-track"><div class="track-label">๐ŸŽฌ ์˜์ƒ</div><div class="track-content" id="t0"></div></div>
162
+ <div class="tl-track"><div class="track-label">๐ŸŽต ์˜ค๋””์˜ค</div><div class="track-content" id="t1"></div></div>
163
+ </div>
164
+ <div class="playhead" id="playhead" style="left:50px"></div>
165
+ </div>
166
+ </div>
167
+ <div class="status" id="status">์ค€๋น„๋จ</div>
168
+ </div>
169
+ <div class="ctx-menu" id="ctx">
170
+ <div class="ctx-item" onclick="splitClip()">โœ‚ ์ž๋ฅด๊ธฐ</div>
171
+ <div class="ctx-item" onclick="dupeClip()">๐Ÿ“‹ ๋ณต์ œ</div>
172
+ <div class="ctx-item danger" onclick="delClip()">๐Ÿ—‘ ์‚ญ์ œ</div>
173
+ </div>
174
+ <div class="modal" id="exportModal" style="display:none">
175
+ <div class="modal-box">
176
+ <h3>๐ŸŽฌ ์˜์ƒ ๋‚ด๋ณด๋‚ด๊ธฐ</h3>
177
+ <p id="exportMsg">์ค€๋น„์ค‘...</p>
178
+ <div class="progress"><div class="progress-bar" id="exportBar"></div></div>
179
+ <button class="btn btn-secondary" onclick="cancelExport()">์ทจ์†Œ</button>
180
+ </div>
181
+ </div>
182
+ <div id="hiddenMedia" class="hidden-media"></div>
183
+ <script>
184
+ var S={{
185
+ media:[],
186
+ clips:[],
187
+ sel:null,
188
+ playing:false,
189
+ muted:false,
190
+ time:0,
191
+ dur:0,
192
+ zoom:1,
193
+ pps:80,
194
+ history:[],
195
+ animId:null,
196
+ cancelled:false,
197
+ els:{{}},
198
+ canvas:null,
199
+ ctx:null,
200
+ lastClipId:null,
201
+ ratio:'16:9',
202
+ fillMode:'fit',
203
+ ratioSizes:{{
204
+ '16:9':{{w:1280,h:720,pw:640,ph:360}},
205
+ '9:16':{{w:720,h:1280,pw:203,ph:360}},
206
+ '1:1':{{w:1080,h:1080,pw:360,ph:360}},
207
+ '4:3':{{w:1280,h:960,pw:480,ph:360}},
208
+ '4:5':{{w:1080,h:1350,pw:288,ph:360}}
209
+ }}
210
+ }};
211
+
212
+ function init(){{
213
+ S.canvas=document.getElementById('previewCanvas');
214
+ S.ctx=S.canvas.getContext('2d');
215
+ updateCanvasSize();
216
+ drawPlaceholder();
217
+ }}
218
+
219
+ function updateCanvasSize(){{
220
+ var size=S.ratioSizes[S.ratio];
221
+ S.canvas.width=size.pw;
222
+ S.canvas.height=size.ph;
223
+ S.canvas.style.width=size.pw+'px';
224
+ S.canvas.style.height=size.ph+'px';
225
+ }}
226
+
227
+ function setRatio(r){{
228
+ S.ratio=r;
229
+ updateCanvasSize();
230
+ drawFrame();
231
+ stat('ํ™”๋ฉด ๋น„์œจ: '+r);
232
+ }}
233
+
234
+ function setFillMode(m){{
235
+ S.fillMode=m;
236
+ drawFrame();
237
+ stat('์ฑ„์šฐ๊ธฐ ๋ชจ๋“œ: '+(m==='fit'?'๋งž์ถค (์—ฌ๋ฐฑ)':m==='fill'?'์ฑ„์šฐ๊ธฐ (ํ™•๋Œ€)':'๋ธ”๋Ÿฌ ๋ฐฐ๊ฒฝ'));
238
+ }}
239
+
240
+ function id(){{return Math.random().toString(36).substr(2,9)}}
241
+ 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')}}
242
+ function r(n){{return Math.round(n*1000)/1000}}
243
+ function stat(m){{document.getElementById('status').textContent=m}}
244
+ function save(){{S.history.push(JSON.stringify(S.clips));if(S.history.length>30)S.history.shift()}}
245
+
246
+ function drawPlaceholder(){{
247
+ var size=S.ratioSizes[S.ratio];
248
+ var pw=size.pw,ph=size.ph;
249
+ S.ctx.fillStyle='#000';
250
+ S.ctx.fillRect(0,0,pw,ph);
251
+ S.ctx.fillStyle='#444';
252
+ S.ctx.font='14px sans-serif';
253
+ S.ctx.textAlign='center';
254
+ S.ctx.fillText('ํƒ€์ž„๋ผ์ธ์— ๋ฏธ๋””์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”',pw/2,ph/2);
255
+ }}
256
+
257
+ function addMedia(name,type,url,filePath){{
258
+ var m={{id:id(),name:name,type:type,url:url,filePath:filePath||name,dur:type==='image'?5:0,thumb:type==='image'?url:null}};
259
+ S.media.push(m);
260
+ var container=document.getElementById('hiddenMedia');
261
+ if(type==='video'){{
262
+ var v=document.createElement('video');
263
+ v.src=url;
264
+ v.muted=true;
265
+ v.playsInline=true;
266
+ v.preload='auto';
267
+ v.crossOrigin='anonymous';
268
+ container.appendChild(v);
269
+ S.els[m.id]=v;
270
+ v.onloadedmetadata=function(){{
271
+ m.dur=r(v.duration);
272
+ renderLib();
273
+ v.currentTime=0.5;
274
+ }};
275
+ v.onseeked=function(){{
276
+ if(!m.thumb){{
277
+ try{{
278
+ var c=document.createElement('canvas');
279
+ c.width=160;c.height=90;
280
+ c.getContext('2d').drawImage(v,0,0,160,90);
281
+ m.thumb=c.toDataURL();
282
+ renderLib();
283
+ }}catch(e){{}}
284
+ }}
285
+ }};
286
+ }}else if(type==='audio'){{
287
+ var a=document.createElement('audio');
288
+ a.src=url;
289
+ a.preload='auto';
290
+ container.appendChild(a);
291
+ S.els[m.id]=a;
292
+ a.onloadedmetadata=function(){{m.dur=r(a.duration);renderLib()}};
293
+ }}else if(type==='image'){{
294
+ var img=new Image();
295
+ img.src=url;
296
+ img.crossOrigin='anonymous';
297
+ S.els[m.id]=img;
298
+ }}
299
+ renderLib();
300
+ stat('๋ฏธ๋””์–ด ์ถ”๊ฐ€: '+name);
301
+ setTimeout(function(){{addClip(m)}},400);
302
+ }}
303
+
304
+ function renderLib(){{
305
+ var g=document.getElementById('mediaGrid');
306
+ var h=document.getElementById('hint');
307
+ h.style.display=S.media.length?'none':'block';
308
+ g.innerHTML='';
309
+ S.media.forEach(function(m){{
310
+ var d=document.createElement('div');
311
+ d.className='media-item';
312
+ d.draggable=true;
313
+ d.ondblclick=function(){{addClip(m)}};
314
+ d.ondragstart=function(e){{e.dataTransfer.setData('mid',m.id)}};
315
+ var th=m.thumb?'<img src="'+m.thumb+'">':'<div class="media-item-icon">'+(m.type==='video'?'๐ŸŽฌ':m.type==='audio'?'๐ŸŽต':'๐Ÿ–ผ')+'</div>';
316
+ d.innerHTML=th+(m.dur?'<div class="media-item-dur">'+fmt(m.dur)+'</div>':'');
317
+ g.appendChild(d);
318
+ }});
319
+ }}
320
+
321
+ function trackEnd(tr){{
322
+ var end=0;
323
+ for(var i=0;i<S.clips.length;i++){{
324
+ var c=S.clips[i];
325
+ if(c.track===tr){{
326
+ var e=r(c.start+(c.te-c.ts));
327
+ if(e>end)end=e;
328
+ }}
329
+ }}
330
+ return end;
331
+ }}
332
+
333
+ function addClip(m,at){{
334
+ save();
335
+ var tr=m.type==='audio'?1:0;
336
+ var st=at!==undefined?r(at):trackEnd(tr);
337
+ S.clips.push({{
338
+ id:id(),
339
+ mid:m.id,
340
+ name:m.name,
341
+ type:m.type,
342
+ track:tr,
343
+ start:st,
344
+ dur:m.dur,
345
+ ts:0,
346
+ te:m.dur,
347
+ vol:1,
348
+ filePath:m.filePath
349
+ }});
350
+ renderTL();
351
+ updateDur();
352
+ stat('ํด๋ฆฝ ์ถ”๊ฐ€: '+m.name);
353
+ drawFrame();
354
+ }}
355
+
356
+ function renderTL(){{
357
+ ['t0','t1'].forEach(function(tid){{document.getElementById(tid).innerHTML=''}});
358
+ S.clips.forEach(function(c){{
359
+ var tr=document.getElementById('t'+c.track);
360
+ var el=document.createElement('div');
361
+ el.className='clip '+c.type+(S.sel===c.id?' selected':'');
362
+ var len=r(c.te-c.ts);
363
+ el.style.left=r(c.start*S.pps*S.zoom)+'px';
364
+ el.style.width=Math.max(25,r(len*S.pps*S.zoom))+'px';
365
+ el.draggable=true;
366
+ el.onclick=function(e){{e.stopPropagation();selClip(c.id)}};
367
+ el.oncontextmenu=function(e){{e.preventDefault();selClip(c.id);showCtx(e.clientX,e.clientY)}};
368
+ el.ondragstart=function(e){{e.dataTransfer.setData('cid',c.id);e.dataTransfer.setData('ox',e.offsetX)}};
369
+ var m=S.media.find(function(x){{return x.id===c.mid}});
370
+ var th=m&&m.thumb?'<img class="clip-thumb" src="'+m.thumb+'">':'';
371
+ 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>';
372
+ el.querySelector('.clip-handle-l').onmousedown=function(e){{e.stopPropagation();startTrim(c.id,'l',e)}};
373
+ el.querySelector('.clip-handle-r').onmousedown=function(e){{e.stopPropagation();startTrim(c.id,'r',e)}};
374
+ tr.appendChild(el);
375
+ }});
376
+ renderRuler();
377
+ setupDrop();
378
+ }}
379
+
380
+ function renderRuler(){{
381
+ var ru=document.getElementById('ruler');
382
+ var w=Math.max(r(S.dur*S.pps*S.zoom)+150,600);
383
+ ru.style.width=w+'px';
384
+ var h='<svg width="100%" height="18" style="position:absolute;left:50px">';
385
+ var step=S.zoom<0.7?5:S.zoom<1.5?2:1;
386
+ for(var i=0;i<=S.dur+10;i+=step){{
387
+ var x=r(i*S.pps*S.zoom);
388
+ h+='<line x1="'+x+'" y1="13" x2="'+x+'" y2="18" stroke="#ccc"/>';
389
+ h+='<text x="'+x+'" y="10" fill="#999" font-size="8" text-anchor="middle">'+fmt(i)+'</text>';
390
+ }}
391
+ ru.innerHTML=h+'</svg>';
392
+ }}
393
+
394
+ function setupDrop(){{
395
+ ['t0','t1'].forEach(function(tid,idx){{
396
+ var tr=document.getElementById(tid);
397
+ tr.ondragover=function(e){{e.preventDefault();tr.classList.add('drop-zone')}};
398
+ tr.ondragleave=function(){{tr.classList.remove('drop-zone')}};
399
+ tr.ondrop=function(e){{
400
+ e.preventDefault();
401
+ tr.classList.remove('drop-zone');
402
+ var rect=tr.getBoundingClientRect();
403
+ var t=r(Math.max(0,(e.clientX-rect.left)/(S.pps*S.zoom)));
404
+ var mid=e.dataTransfer.getData('mid');
405
+ var cid=e.dataTransfer.getData('cid');
406
+ var ox=parseFloat(e.dataTransfer.getData('ox')||0);
407
+ if(mid){{
408
+ var m=S.media.find(function(x){{return x.id===mid}});
409
+ if(m)addClip(m,t);
410
+ }}else if(cid){{
411
+ save();
412
+ var c=S.clips.find(function(x){{return x.id===cid}});
413
+ if(c){{
414
+ c.start=r(Math.max(0,t-ox/(S.pps*S.zoom)));
415
+ c.track=c.type==='audio'?1:idx;
416
+ renderTL();
417
+ updateDur();
418
+ drawFrame();
419
+ }}
420
+ }}
421
+ }};
422
+ }});
423
+ }}
424
+
425
+ var trimData=null;
426
+ function startTrim(cid,side,e){{
427
+ e.preventDefault();
428
+ var c=S.clips.find(function(x){{return x.id===cid}});
429
+ if(!c)return;
430
+ save();
431
+ trimData={{cid:cid,side:side,sx:e.clientX,ots:c.ts,ote:c.te,ost:c.start}};
432
+ document.addEventListener('mousemove',doTrim);
433
+ document.addEventListener('mouseup',endTrim);
434
+ }}
435
+ function doTrim(e){{
436
+ if(!trimData)return;
437
+ var c=S.clips.find(function(x){{return x.id===trimData.cid}});
438
+ if(!c)return;
439
+ var dx=e.clientX-trimData.sx;
440
+ var dt=r(dx/(S.pps*S.zoom));
441
+ if(trimData.side==='l'){{
442
+ var newTs=Math.max(0,Math.min(c.te-0.1,trimData.ots+dt));
443
+ c.ts=r(newTs);
444
+ c.start=r(trimData.ost+(newTs-trimData.ots));
445
+ }}else{{
446
+ c.te=r(Math.max(c.ts+0.1,Math.min(c.dur,trimData.ote+dt)));
447
+ }}
448
+ renderTL();
449
+ updateDur();
450
+ }}
451
+ function endTrim(){{
452
+ trimData=null;
453
+ document.removeEventListener('mousemove',doTrim);
454
+ document.removeEventListener('mouseup',endTrim);
455
+ }}
456
+
457
+ function selClip(cid){{S.sel=cid;renderTL();renderProps()}}
458
+
459
+ function renderProps(){{
460
+ var box=document.getElementById('propsBox');
461
+ var c=S.clips.find(function(x){{return x.id===S.sel}});
462
+ if(!c){{box.innerHTML='<div class="no-sel">ํด๋ฆฝ ์„ ํƒ</div>';return}}
463
+ var len=r(c.te-c.ts);
464
+ box.innerHTML='<div class="prop-group"><div class="prop-label">์ด๋ฆ„</div><input class="prop-input" value="'+c.name+'" onchange="setProp(\\'name\\',this.value)"></div>'+
465
+ '<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>'+
466
+ '<div class="prop-group"><div class="prop-label">๊ธธ์ด: '+fmt(len)+'</div></div>'+
467
+ (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>':'');
468
+ }}
469
+
470
+ function setProp(p,v){{
471
+ save();
472
+ var c=S.clips.find(function(x){{return x.id===S.sel}});
473
+ if(c){{c[p]=p==='start'?r(v):v;renderTL();updateDur();renderProps();drawFrame()}}
474
+ }}
475
+
476
+ function splitClip(){{
477
+ if(!S.sel){{alert('ํด๋ฆฝ์„ ์„ ํƒํ•˜์„ธ์š”');return}}
478
+ var c=S.clips.find(function(x){{return x.id===S.sel}});
479
+ if(!c)return;
480
+ var cEnd=r(c.start+c.te-c.ts);
481
+ if(S.time<=c.start||S.time>=cEnd){{alert('ํ”Œ๋ ˆ์ดํ—ค๋“œ๊ฐ€ ํด๋ฆฝ ์•ˆ์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค');return}}
482
+ save();
483
+ var splitAt=r(S.time-c.start);
484
+ var c2=JSON.parse(JSON.stringify(c));
485
+ c2.id=id();
486
+ c2.start=r(S.time);
487
+ c2.ts=r(c.ts+splitAt);
488
+ c.te=r(c.ts+splitAt);
489
+ S.clips.push(c2);
490
+ renderTL();updateDur();hideCtx();stat('ํด๋ฆฝ ๋ถ„ํ• ๋จ');
491
+ }}
492
+
493
+ function dupeClip(){{
494
+ if(!S.sel)return;
495
+ var c=S.clips.find(function(x){{return x.id===S.sel}});
496
+ if(!c)return;
497
+ save();
498
+ var len=r(c.te-c.ts);
499
+ var nc=JSON.parse(JSON.stringify(c));
500
+ nc.id=id();
501
+ nc.start=r(c.start+len);
502
+ S.clips.push(nc);
503
+ renderTL();updateDur();hideCtx();stat('ํด๋ฆฝ ๋ณต์ œ๋จ');
504
+ }}
505
+
506
+ function delClip(){{
507
+ if(!S.sel)return;
508
+ save();
509
+ S.clips=S.clips.filter(function(x){{return x.id!==S.sel}});
510
+ S.sel=null;
511
+ renderTL();renderProps();updateDur();hideCtx();stat('ํด๋ฆฝ ์‚ญ์ œ๋จ');
512
+ drawFrame();
513
+ }}
514
+
515
+ function undo(){{if(S.history.length){{S.clips=JSON.parse(S.history.pop());renderTL();updateDur();stat('์‹คํ–‰์ทจ์†Œ');drawFrame()}}}}
516
+
517
+ function updateDur(){{
518
+ var mx=0;
519
+ for(var i=0;i<S.clips.length;i++){{
520
+ var c=S.clips[i];
521
+ var e=r(c.start+c.te-c.ts);
522
+ if(e>mx)mx=e;
523
+ }}
524
+ S.dur=mx;
525
+ document.getElementById('totT').textContent=fmt(mx);
526
+ }}
527
+
528
+ function togglePlay(){{
529
+ S.playing=!S.playing;
530
+ document.getElementById('playBtn').textContent=S.playing?'โธ':'โ–ถ';
531
+ if(S.playing)play();else stop();
532
+ }}
533
+
534
+ function play(){{
535
+ var last=performance.now();
536
+ function loop(now){{
537
+ if(!S.playing)return;
538
+ var dt=(now-last)/1000;
539
+ last=now;
540
+ S.time=S.time+dt;
541
+ if(S.time>=S.dur){{
542
+ S.time=0;
543
+ if(S.dur===0){{S.playing=false;document.getElementById('playBtn').textContent='โ–ถ';return}}
544
+ }}
545
+ updateHead();
546
+ drawFrame();
547
+ S.animId=requestAnimationFrame(loop);
548
+ }}
549
+ S.animId=requestAnimationFrame(loop);
550
+ }}
551
+
552
+ function stop(){{
553
+ if(S.animId){{cancelAnimationFrame(S.animId);S.animId=null}}
554
+ Object.keys(S.els).forEach(function(k){{
555
+ var el=S.els[k];
556
+ if(el&&el.pause)el.pause();
557
+ }});
558
+ }}
559
+
560
+ function seek(t){{
561
+ S.time=Math.max(0,Math.min(S.dur||0,t));
562
+ updateHead();
563
+ drawFrame();
564
+ }}
565
+
566
+ function updateHead(){{
567
+ document.getElementById('playhead').style.left=(50+S.time*S.pps*S.zoom)+'px';
568
+ document.getElementById('curT').textContent=fmt(S.time);
569
+ }}
570
+
571
+ function getClipAt(t,type){{
572
+ var sorted=S.clips.filter(function(c){{
573
+ if(type==='visual')return c.type==='video'||c.type==='image';
574
+ if(type==='audio')return c.type==='audio';
575
+ return true;
576
+ }}).sort(function(a,b){{return a.start-b.start}});
577
+ for(var i=0;i<sorted.length;i++){{
578
+ var c=sorted[i];
579
+ var cEnd=c.start+(c.te-c.ts);
580
+ if(t>=c.start&&t<cEnd)return c;
581
+ }}
582
+ return null;
583
+ }}
584
+
585
+ function drawFrame(){{
586
+ var t=S.time;
587
+ var vc=getClipAt(t,'visual');
588
+ var size=S.ratioSizes[S.ratio];
589
+ var pw=size.pw,ph=size.ph;
590
+
591
+ S.ctx.fillStyle='#000';
592
+ S.ctx.fillRect(0,0,pw,ph);
593
+
594
+ if(vc){{
595
+ var el=S.els[vc.mid];
596
+ if(el){{
597
+ if(vc.type==='video'){{
598
+ var clipT=t-vc.start+vc.ts;
599
+ if(Math.abs(el.currentTime-clipT)>0.05){{
600
+ el.currentTime=clipT;
601
+ }}
602
+ if(S.playing&&el.paused)el.play().catch(function(){{}});
603
+ if(!S.playing&&!el.paused)el.pause();
604
+ el.volume=S.muted?0:vc.vol;
605
+ }}
606
+ try{{
607
+ var sw=el.videoWidth||el.naturalWidth||el.width||pw;
608
+ var sh=el.videoHeight||el.naturalHeight||el.height||ph;
609
+ drawWithMode(S.ctx,el,sw,sh,pw,ph,S.fillMode);
610
+ }}catch(e){{}}
611
+ }}
612
+ }}else if(S.clips.length===0){{
613
+ S.ctx.fillStyle='#444';
614
+ S.ctx.font='14px sans-serif';
615
+ S.ctx.textAlign='center';
616
+ S.ctx.fillText('ํƒ€์ž„๋ผ์ธ์— ๋ฏธ๋””์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”',pw/2,ph/2);
617
+ }}
618
+ var audioClips=S.clips.filter(function(c){{
619
+ if(c.type!=='audio')return false;
620
+ var cEnd=c.start+(c.te-c.ts);
621
+ return t>=c.start&&t<cEnd;
622
+ }});
623
+ audioClips.forEach(function(ac){{
624
+ var el=S.els[ac.mid];
625
+ if(el){{
626
+ var clipT=t-ac.start+ac.ts;
627
+ if(Math.abs(el.currentTime-clipT)>0.1)el.currentTime=clipT;
628
+ el.volume=S.muted?0:ac.vol;
629
+ if(S.playing&&el.paused)el.play().catch(function(){{}});
630
+ if(!S.playing&&!el.paused)el.pause();
631
+ }}
632
+ }});
633
+ S.clips.forEach(function(c){{
634
+ if(c.type!=='audio')return;
635
+ var cEnd=c.start+(c.te-c.ts);
636
+ if(t<c.start||t>=cEnd){{
637
+ var el=S.els[c.mid];
638
+ if(el&&!el.paused)el.pause();
639
+ }}
640
+ }});
641
+ if(!vc&&!audioClips.length&&S.clips.length>0){{
642
+ S.ctx.fillStyle='#333';
643
+ S.ctx.font='12px sans-serif';
644
+ S.ctx.textAlign='center';
645
+ S.ctx.fillText('์žฌ์ƒ ์œ„์น˜์— ๋ฏธ๋””์–ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค',pw/2,ph/2);
646
+ }}
647
+ }}
648
+
649
+ function drawWithMode(ctx,el,sw,sh,dw,dh,mode){{
650
+ if(mode==='fill'){{
651
+ // ์ฑ„์šฐ๊ธฐ: ์บ”๋ฒ„์Šค๋ฅผ ๊ฝ‰ ์ฑ„์šฐ๋„๋ก ํ™•๋Œ€, ๋„˜์น˜๋Š” ๋ถ€๋ถ„ ์ž˜๋ฆผ
652
+ var scale=Math.max(dw/sw,dh/sh);
653
+ var nw=sw*scale,nh=sh*scale;
654
+ var ox=(dw-nw)/2,oy=(dh-nh)/2;
655
+ ctx.drawImage(el,ox,oy,nw,nh);
656
+ }}else if(mode==='blur'){{
657
+ // ๋จผ์ € ํ˜„์žฌ ํ”„๋ ˆ์ž„์„ ์ž„์‹œ ์บ”๋ฒ„์Šค์— ์บก์ฒ˜
658
+ var tempCanvas=document.createElement('canvas');
659
+ tempCanvas.width=sw;tempCanvas.height=sh;
660
+ var tempCtx=tempCanvas.getContext('2d');
661
+ tempCtx.drawImage(el,0,0,sw,sh);
662
+
663
+ // ๋ธ”๋Ÿฌ ๋ฐฐ๊ฒฝ: ๋ฐฐ๊ฒฝ์— ํ™•๋Œ€+๋ธ”๋Ÿฌ, ๊ทธ ์œ„์— ์›๋ณธ
664
+ ctx.filter='blur(20px)';
665
+ var scale=Math.max(dw/sw,dh/sh)*1.1;
666
+ var nw=sw*scale,nh=sh*scale;
667
+ var ox=(dw-nw)/2,oy=(dh-nh)/2;
668
+ ctx.drawImage(tempCanvas,ox,oy,nw,nh);
669
+ ctx.filter='none';
670
+
671
+ // ์–ด๋‘ก๊ฒŒ
672
+ ctx.fillStyle='rgba(0,0,0,0.3)';
673
+ ctx.fillRect(0,0,dw,dh);
674
+
675
+ // ์›๋ณธ (fit) - ์บก์ฒ˜๋œ ํ”„๋ ˆ์ž„ ์žฌ์‚ฌ์šฉ
676
+ var fitScale=Math.min(dw/sw,dh/sh);
677
+ var fw=sw*fitScale,fh=sh*fitScale;
678
+ var fx=(dw-fw)/2,fy=(dh-fh)/2;
679
+ ctx.drawImage(tempCanvas,fx,fy,fw,fh);
680
+ }}else{{
681
+ // ๋งž์ถค (fit): ์›๋ณธ ๋น„์œจ ์œ ์ง€, ์—ฌ๋ฐฑ
682
+ var scale=Math.min(dw/sw,dh/sh);
683
+ var nw=sw*scale,nh=sh*scale;
684
+ var ox=(dw-nw)/2,oy=(dh-nh)/2;
685
+ ctx.drawImage(el,ox,oy,nw,nh);
686
+ }}
687
+ }}
688
+
689
+ function toggleMute(){{S.muted=!S.muted;document.getElementById('muteBtn').textContent=S.muted?'๐Ÿ”‡':'๐Ÿ”Š'}}
690
+ function setZoom(v){{S.zoom=parseFloat(v);renderTL();updateHead()}}
691
+ function tlClick(e){{
692
+ if(e.target.closest('.clip'))return;
693
+ var rect=document.getElementById('tlBox').getBoundingClientRect();
694
+ var scrollL=document.getElementById('tlBox').scrollLeft;
695
+ S.time=Math.max(0,Math.min(S.dur||0,(e.clientX-rect.left-50+scrollL)/(S.pps*S.zoom)));
696
+ updateHead();
697
+ drawFrame();
698
+ }}
699
+
700
+ function showCtx(x,y){{var m=document.getElementById('ctx');m.style.display='block';m.style.left=x+'px';m.style.top=y+'px'}}
701
+ function hideCtx(){{document.getElementById('ctx').style.display='none'}}
702
+ document.addEventListener('click',function(e){{if(!e.target.closest('.ctx-menu'))hideCtx()}});
703
+
704
+ function exportVideo(){{
705
+ if(!S.clips.length){{alert('ํด๋ฆฝ์„ ์ถ”๊ฐ€ํ•˜์„ธ์š”');return}}
706
+ S.cancelled=false;
707
+ document.getElementById('exportModal').style.display='flex';
708
+ document.getElementById('exportBar').style.width='0%';
709
+ document.getElementById('exportMsg').textContent='๋…นํ™” ์ค€๋น„ ์ค‘...';
710
+ doExport();
711
+ }}
712
+
713
+ async function doExport(){{
714
+ var size=S.ratioSizes[S.ratio];
715
+ var exportW=size.w,exportH=size.h;
716
+
717
+ // ๋ฉ”์ธ ์บ”๋ฒ„์Šค (MediaRecorder ์—ฐ๊ฒฐ์šฉ)
718
+ var canvas=document.createElement('canvas');
719
+ canvas.width=exportW;canvas.height=exportH;
720
+ var ctx=canvas.getContext('2d');
721
+
722
+ // ์˜คํ”„์Šคํฌ๋ฆฐ ์บ”๋ฒ„์Šค (๋ Œ๋”๋ง์šฉ - ๋”๋ธ” ๋ฒ„ํผ๋ง)
723
+ var offCanvas=document.createElement('canvas');
724
+ offCanvas.width=exportW;offCanvas.height=exportH;
725
+ var offCtx=offCanvas.getContext('2d');
726
+
727
+ // ์ดˆ๊ธฐ ๊ฒ€์€ ํ™”๋ฉด
728
+ ctx.fillStyle='#000';
729
+ ctx.fillRect(0,0,exportW,exportH);
730
+
731
+ var stream=canvas.captureStream(30);
732
+
733
+ // ๊ณ ํ™”์งˆ ์„ค์ •
734
+ var opts={{mimeType:'video/webm;codecs=vp9',videoBitsPerSecond:8000000}};
735
+ if(!MediaRecorder.isTypeSupported(opts.mimeType)){{
736
+ opts={{mimeType:'video/webm;codecs=vp8',videoBitsPerSecond:8000000}};
737
+ }}
738
+ if(!MediaRecorder.isTypeSupported(opts.mimeType)){{
739
+ opts={{mimeType:'video/webm',videoBitsPerSecond:5000000}};
740
+ }}
741
+
742
+ var rec=new MediaRecorder(stream,opts);
743
+ var chunks=[];
744
+ rec.ondataavailable=function(e){{if(e.data.size>0)chunks.push(e.data)}};
745
+
746
+ document.getElementById('exportMsg').textContent='๋…นํ™” ์ค‘... ('+S.ratio+')';
747
+ rec.start(100);
748
+
749
+ var dur=S.dur;
750
+ var fps=30;
751
+ var frameInterval=1000/fps;
752
+ var totalFrames=Math.ceil(dur*fps);
753
+ var startTime=performance.now();
754
+ var fillMode=S.fillMode;
755
+
756
+ // ์‹ค์‹œ๊ฐ„ ๊ธฐ๋ฐ˜ ๋ Œ๋”๋ง
757
+ await new Promise(function(resolve){{
758
+ var frameCount=0;
759
+ var lastVideoEl=null;
760
+ var lastClipId=null;
761
+
762
+ function render(){{
763
+ if(S.cancelled){{rec.stop();resolve();return}}
764
+
765
+ var elapsed=performance.now()-startTime;
766
+ var t=elapsed/1000;
767
+
768
+ if(t>=dur){{
769
+ rec.stop();
770
+ setTimeout(resolve,100);
771
+ return;
772
+ }}
773
+
774
+ document.getElementById('exportBar').style.width=(t/dur*100)+'%';
775
+ document.getElementById('exportMsg').textContent='๋…นํ™” ์ค‘... '+Math.round(t/dur*100)+'% ('+t.toFixed(1)+'/'+dur.toFixed(1)+'์ดˆ)';
776
+
777
+ offCtx.fillStyle='#000';
778
+ offCtx.fillRect(0,0,exportW,exportH);
779
+
780
+ var vc=getClipAt(t,'visual');
781
+ if(vc){{
782
+ var el=S.els[vc.mid];
783
+ if(el){{
784
+ if(vc.type==='video'){{
785
+ var clipT=t-vc.start+vc.ts;
786
+ if(lastClipId!==vc.id){{
787
+ el.currentTime=clipT;
788
+ el.play().catch(function(){{}});
789
+ lastClipId=vc.id;
790
+ lastVideoEl=el;
791
+ }}
792
+ if(lastVideoEl&&lastVideoEl!==el&&!lastVideoEl.paused){{
793
+ lastVideoEl.pause();
794
+ }}
795
+ lastVideoEl=el;
796
+ }}
797
+
798
+ try{{
799
+ var sw=el.videoWidth||el.naturalWidth||el.width||exportW;
800
+ var sh=el.videoHeight||el.naturalHeight||el.height||exportH;
801
+ drawWithModeExport(offCtx,el,sw,sh,exportW,exportH,fillMode);
802
+ }}catch(e){{}}
803
+ }}
804
+ }}else{{
805
+ if(lastVideoEl&&!lastVideoEl.paused){{
806
+ lastVideoEl.pause();
807
+ lastClipId=null;
808
+ }}
809
+ }}
810
+
811
+ ctx.drawImage(offCanvas,0,0);
812
+ requestAnimationFrame(render);
813
+ }}
814
+
815
+ requestAnimationFrame(render);
816
+ }});
817
+
818
+ Object.keys(S.els).forEach(function(k){{
819
+ var el=S.els[k];
820
+ if(el&&el.pause)el.pause();
821
+ }});
822
+
823
+ if(S.cancelled)return;
824
+
825
+ var webmBlob=new Blob(chunks,{{type:'video/webm'}});
826
+ if(webmBlob.size<1000){{document.getElementById('exportMsg').textContent='๋…นํ™” ์‹คํŒจ';return}}
827
+
828
+ document.getElementById('exportBar').style.width='100%';
829
+ document.getElementById('exportMsg').textContent='์™„๋ฃŒ! ('+Math.round(webmBlob.size/1024/1024*10)/10+'MB, '+S.ratio+') - ์•„๋ž˜์—์„œ ๋‹ค์šด๋กœ๋“œ';
830
+
831
+ var downloadDiv=document.createElement('div');
832
+ downloadDiv.style.marginTop='10px';
833
+ var webmLink=document.createElement('a');
834
+ webmLink.href=URL.createObjectURL(webmBlob);
835
+ webmLink.download='video_'+S.ratio.replace(':','x')+'_'+Date.now()+'.webm';
836
+ webmLink.className='btn btn-success';
837
+ webmLink.textContent='๐Ÿ“ฅ WebM ๋‹ค์šด๋กœ๋“œ';
838
+ webmLink.style.marginRight='5px';
839
+ downloadDiv.appendChild(webmLink);
840
+
841
+ var copyBtn=document.createElement('button');
842
+ copyBtn.className='btn btn-secondary';
843
+ copyBtn.textContent='๐Ÿ“‹ MP4๋ณ€ํ™˜์šฉ ๋ณต์‚ฌ';
844
+ copyBtn.onclick=async function(){{
845
+ document.getElementById('exportMsg').textContent='๋ฐ์ดํ„ฐ ์ค€๋น„ ์ค‘...';
846
+ var reader=new FileReader();
847
+ reader.onload=function(){{
848
+ var base64=reader.result.split(',')[1];
849
+ navigator.clipboard.writeText(base64).then(function(){{
850
+ document.getElementById('exportMsg').textContent='๋ณต์‚ฌ ์™„๋ฃŒ! ์œ„ ์ž…๋ ฅ๋ž€์— ๋ถ™์—ฌ๋„ฃ๊ธฐ';
851
+ alert('๋ณต์‚ฌ ์™„๋ฃŒ!\\n\\nGradio์˜ "WebM ๋ฐ์ดํ„ฐ" ์ž…๋ ฅ๋ž€์— ๋ถ™์—ฌ๋„ฃ๊ธฐ(Ctrl+V) ํ›„\\n"MP4 ๋ณ€ํ™˜" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์„ธ์š”.');
852
+ }}).catch(function(){{
853
+ prompt('์•„๋ž˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜์„ธ์š”:', base64.substring(0,100)+'...');
854
+ }});
855
+ }};
856
+ reader.readAsDataURL(webmBlob);
857
+ }};
858
+ downloadDiv.appendChild(copyBtn);
859
+ document.querySelector('.modal-box').appendChild(downloadDiv);
860
+ }}
861
+
862
+ // ๋‚ด๋ณด๋‚ด๊ธฐ์šฉ ๊ทธ๋ฆฌ๊ธฐ ํ•จ์ˆ˜ (๋ธ”๋Ÿฌ ํšจ๊ณผ ํฌํ•จ)
863
+ function drawWithModeExport(ctx,el,sw,sh,dw,dh,mode){{
864
+ if(mode==='fill'){{
865
+ var scale=Math.max(dw/sw,dh/sh);
866
+ var nw=sw*scale,nh=sh*scale;
867
+ var ox=(dw-nw)/2,oy=(dh-nh)/2;
868
+ ctx.drawImage(el,ox,oy,nw,nh);
869
+ }}else if(mode==='blur'){{
870
+ // ๋จผ์ € ํ˜„์žฌ ํ”„๋ ˆ์ž„์„ ์ž„์‹œ ์บ”๋ฒ„์Šค์— ์บก์ฒ˜
871
+ var tempCanvas=document.createElement('canvas');
872
+ tempCanvas.width=sw;tempCanvas.height=sh;
873
+ var tempCtx=tempCanvas.getContext('2d');
874
+ tempCtx.drawImage(el,0,0,sw,sh);
875
+
876
+ // ๋ธ”๋Ÿฌ ๋ฐฐ๊ฒฝ ๊ทธ๋ฆฌ๊ธฐ
877
+ ctx.filter='blur(20px)';
878
+ var scale=Math.max(dw/sw,dh/sh)*1.1;
879
+ var nw=sw*scale,nh=sh*scale;
880
+ var ox=(dw-nw)/2,oy=(dh-nh)/2;
881
+ ctx.drawImage(tempCanvas,ox,oy,nw,nh);
882
+ ctx.filter='none';
883
+
884
+ // ์–ด๋‘ก๊ฒŒ
885
+ ctx.fillStyle='rgba(0,0,0,0.3)';
886
+ ctx.fillRect(0,0,dw,dh);
887
+
888
+ // ์›๋ณธ (fit) - ์บก์ฒ˜๋œ ํ”„๋ ˆ์ž„ ์žฌ์‚ฌ์šฉ
889
+ var fitScale=Math.min(dw/sw,dh/sh);
890
+ var fw=sw*fitScale,fh=sh*fitScale;
891
+ var fx=(dw-fw)/2,fy=(dh-fh)/2;
892
+ ctx.drawImage(tempCanvas,fx,fy,fw,fh);
893
+ }}else{{
894
+ var scale=Math.min(dw/sw,dh/sh);
895
+ var nw=sw*scale,nh=sh*scale;
896
+ var ox=(dw-nw)/2,oy=(dh-nh)/2;
897
+ ctx.drawImage(el,ox,oy,nw,nh);
898
+ }}
899
+ }}
900
+
901
+ function cancelExport(){{S.cancelled=true;document.getElementById('exportModal').style.display='none'}}
902
+
903
+ document.addEventListener('keydown',function(e){{
904
+ if(e.target.tagName==='INPUT')return;
905
+ if(e.code==='Space'){{e.preventDefault();togglePlay()}}
906
+ else if(e.code==='Delete'){{e.preventDefault();delClip()}}
907
+ else if(e.code==='ArrowLeft'){{seek(S.time-0.1)}}
908
+ else if(e.code==='ArrowRight'){{seek(S.time+0.1)}}
909
+ }});
910
+
911
+ init();
912
+ renderTL();
913
+ stat('์ค€๋น„๋จ');
914
+ var initData={media_data};
915
+ if(initData&&initData.length)initData.forEach(function(m){{addMedia(m.name,m.type,m.dataUrl,m.filePath)}});
916
+ </script>
917
+ </body>
918
+ </html>'''
919
+
920
+ def process_file(files):
921
+ """ํŒŒ์ผ ์ฒ˜๋ฆฌ ๋ฐ ์„œ๋ฒ„์— ์ €์žฅ"""
922
+ global uploaded_files
923
+ if not files:
924
+ return []
925
+ results = []
926
+ file_list = files if isinstance(files, list) else [files]
927
+ for f in file_list:
928
+ if not f:
929
+ continue
930
+ path = f.name if hasattr(f, 'name') else f
931
+ name = os.path.basename(path)
932
+ ext = name.lower().split('.')[-1]
933
+ if ext in ['mp4', 'webm', 'mov', 'avi', 'mkv']:
934
+ t, m = 'video', f'video/{ext}'
935
+ elif ext in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
936
+ t, m = 'image', f'image/{ext}'
937
+ elif ext in ['mp3', 'wav', 'ogg', 'm4a', 'aac']:
938
+ t, m = 'audio', f'audio/{ext}'
939
+ else:
940
+ continue
941
+
942
+ # ์„œ๋ฒ„์— ํŒŒ์ผ ๋ณต์‚ฌ (MP4 ๋‚ด๋ณด๋‚ด๊ธฐ์šฉ)
943
+ dst_path = os.path.join(UPLOAD_DIR, f"{int(time.time()*1000)}_{name}")
944
+ shutil.copy(path, dst_path)
945
+ uploaded_files[name] = dst_path
946
+
947
+ with open(path, 'rb') as fp:
948
+ d = base64.b64encode(fp.read()).decode()
949
+ results.append({'name': name, 'type': t, 'dataUrl': f'data:{m};base64,{d}', 'filePath': name})
950
+ return results
951
+
952
+ def make_iframe(data):
953
+ j = json.dumps(data, ensure_ascii=False)
954
+ h = get_editor_html(j).replace("'", "&#39;")
955
+ return f"<iframe srcdoc='{h}' style='width:100%;height:750px;border:none;border-radius:10px'></iframe>"
956
+
957
+ def export_mp4(export_json):
958
+ """์„œ๋ฒ„ ์‚ฌ์ด๋“œ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ"""
959
+ global uploaded_files
960
+
961
+ if not export_json or len(export_json) < 10:
962
+ return None
963
+
964
+ try:
965
+ data = json.loads(export_json)
966
+ clips = data.get('clips', [])
967
+
968
+ if not clips:
969
+ return None
970
+
971
+ video_clips = [c for c in clips if c['type'] in ['video', 'image']]
972
+ if not video_clips:
973
+ return None
974
+
975
+ temp_dir = tempfile.mkdtemp()
976
+ output_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4')
977
+
978
+ # ๋‹จ์ผ ํด๋ฆฝ
979
+ if len(video_clips) == 1:
980
+ clip = video_clips[0]
981
+ file_path = uploaded_files.get(clip['filePath'])
982
+
983
+ if not file_path or not os.path.exists(file_path):
984
+ return None
985
+
986
+ duration = clip['te'] - clip['ts']
987
+
988
+ if clip['type'] == 'image':
989
+ 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]
990
+ else:
991
+ 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]
992
+
993
+ subprocess.run(cmd, capture_output=True, timeout=300)
994
+
995
+ if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
996
+ return output_path
997
+ return None
998
+
999
+ # ์—ฌ๋Ÿฌ ํด๋ฆฝ
1000
+ temp_files = []
1001
+ concat_file = os.path.join(temp_dir, 'concat.txt')
1002
+
1003
+ for i, clip in enumerate(sorted(video_clips, key=lambda x: x['start'])):
1004
+ file_path = uploaded_files.get(clip['filePath'])
1005
+ if not file_path or not os.path.exists(file_path):
1006
+ continue
1007
+
1008
+ temp_out = os.path.join(temp_dir, f'temp_{i}.mp4')
1009
+ duration = clip['te'] - clip['ts']
1010
+
1011
+ if clip['type'] == 'image':
1012
+ 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]
1013
+ else:
1014
+ 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]
1015
+
1016
+ subprocess.run(cmd, capture_output=True, timeout=120)
1017
+ if os.path.exists(temp_out):
1018
+ temp_files.append(temp_out)
1019
+
1020
+ if not temp_files:
1021
+ return None
1022
+
1023
+ with open(concat_file, 'w') as f:
1024
+ for tf in temp_files:
1025
+ f.write(f"file '{tf}'\n")
1026
+
1027
+ cmd = ['ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', concat_file, '-c:v', 'libx264', '-preset', 'fast', '-c:a', 'aac', '-movflags', '+faststart', output_path]
1028
+ subprocess.run(cmd, capture_output=True, timeout=300)
1029
+
1030
+ for tf in temp_files:
1031
+ try: os.remove(tf)
1032
+ except: pass
1033
+
1034
+ if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
1035
+ return output_path
1036
+ return None
1037
+
1038
+ except Exception as e:
1039
+ print(f"[Export] Error: {e}")
1040
+ return None
1041
+
1042
+ def convert_webm_to_mp4(webm_base64):
1043
+ """WebM base64 ๋ฐ์ดํ„ฐ๋ฅผ MP4๋กœ ๋ณ€ํ™˜"""
1044
+ if not webm_base64 or len(webm_base64) < 100:
1045
+ return None
1046
+
1047
+ try:
1048
+ # base64 ๋””์ฝ”๋”ฉ
1049
+ webm_data = base64.b64decode(webm_base64)
1050
+
1051
+ temp_dir = tempfile.mkdtemp()
1052
+ webm_path = os.path.join(temp_dir, 'input.webm')
1053
+ mp4_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4')
1054
+
1055
+ # WebM ํŒŒ์ผ ์ €์žฅ
1056
+ with open(webm_path, 'wb') as f:
1057
+ f.write(webm_data)
1058
+
1059
+ # FFmpeg๋กœ MP4 ๋ณ€ํ™˜
1060
+ cmd = [
1061
+ 'ffmpeg', '-y',
1062
+ '-i', webm_path,
1063
+ '-c:v', 'libx264',
1064
+ '-preset', 'fast',
1065
+ '-crf', '23',
1066
+ '-c:a', 'aac',
1067
+ '-b:a', '128k',
1068
+ '-movflags', '+faststart',
1069
+ mp4_path
1070
+ ]
1071
+
1072
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
1073
+
1074
+ # WebM ํŒŒ์ผ ์‚ญ์ œ
1075
+ try:
1076
+ os.remove(webm_path)
1077
+ except:
1078
+ pass
1079
+
1080
+ if os.path.exists(mp4_path) and os.path.getsize(mp4_path) > 0:
1081
+ return mp4_path
1082
+
1083
+ return None
1084
+
1085
+ except Exception as e:
1086
+ print(f"[Convert] Error: {e}")
1087
+ return None
1088
+
1089
+ with gr.Blocks() as demo:
1090
+ gr.Markdown("## ๐ŸŽฌ Video Editor")
1091
+
1092
+ f = gr.File(label="๐Ÿ“ ํŒŒ์ผ ์—…๋กœ๋“œ", file_count="multiple", file_types=["video", "image", "audio"])
1093
+ e = gr.HTML(value=make_iframe([]))
1094
+
1095
+ gr.Markdown("---")
1096
+ gr.Markdown("### ๐Ÿ“ฅ MP4 ๋ณ€ํ™˜")
1097
+ gr.Markdown("์—๋””ํ„ฐ์—์„œ '๋‚ด๋ณด๋‚ด๊ธฐ' โ†’ 'MP4๋ณ€ํ™˜์šฉ ๋ณต์‚ฌ' ํด๋ฆญ ํ›„ ์•„๋ž˜์— ๋ถ™์—ฌ๋„ฃ๊ธฐ")
1098
+
1099
+ with gr.Row():
1100
+ webm_data = gr.Textbox(label="WebM ๋ฐ์ดํ„ฐ (base64)", placeholder="์—ฌ๊ธฐ์— ๋ถ™์—ฌ๋„ฃ๊ธฐ (Ctrl+V)", lines=2, scale=4)
1101
+ convert_btn = gr.Button("๐ŸŽฌ MP4 ๋ณ€ํ™˜", variant="primary", scale=1)
1102
+
1103
+ mp4_output = gr.File(label="๐Ÿ“ฅ MP4 ๋‹ค์šด๋กœ๋“œ")
1104
+
1105
+ f.change(fn=lambda x: make_iframe(process_file(x)), inputs=[f], outputs=[e])
1106
+ convert_btn.click(fn=convert_webm_to_mp4, inputs=[webm_data], outputs=[mp4_output])
1107
+
1108
+ if __name__ == "__main__":
1109
+ demo.launch()