seawolf2357 commited on
Commit
da2f18b
ยท
verified ยท
1 Parent(s): 0fe4fca

Create app-backup6.py

Browse files
Files changed (1) hide show
  1. app-backup6.py +972 -0
app-backup6.py ADDED
@@ -0,0 +1,972 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}}
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='๋…นํ™” ์ค€๋น„ ์ค‘...';
624
+ doExport();
625
+ }}
626
+
627
+ async function doExport(){{
628
+ // ๋ฉ”์ธ ์บ”๋ฒ„์Šค (MediaRecorder ์—ฐ๊ฒฐ์šฉ)
629
+ var canvas=document.createElement('canvas');
630
+ canvas.width=1280;canvas.height=720;
631
+ var ctx=canvas.getContext('2d');
632
+
633
+ // ์˜คํ”„์Šคํฌ๋ฆฐ ์บ”๋ฒ„์Šค (๋ Œ๋”๋ง์šฉ - ๋”๋ธ” ๋ฒ„ํผ๋ง)
634
+ var offCanvas=document.createElement('canvas');
635
+ offCanvas.width=1280;offCanvas.height=720;
636
+ var offCtx=offCanvas.getContext('2d');
637
+
638
+ // ์ดˆ๊ธฐ ๊ฒ€์€ ํ™”๋ฉด
639
+ ctx.fillStyle='#000';
640
+ ctx.fillRect(0,0,1280,720);
641
+
642
+ var stream=canvas.captureStream(30);
643
+
644
+ // ๊ณ ํ™”์งˆ ์„ค์ •
645
+ var opts={{mimeType:'video/webm;codecs=vp9',videoBitsPerSecond:8000000}};
646
+ if(!MediaRecorder.isTypeSupported(opts.mimeType)){{
647
+ opts={{mimeType:'video/webm;codecs=vp8',videoBitsPerSecond:8000000}};
648
+ }}
649
+ if(!MediaRecorder.isTypeSupported(opts.mimeType)){{
650
+ opts={{mimeType:'video/webm',videoBitsPerSecond:5000000}};
651
+ }}
652
+
653
+ var rec=new MediaRecorder(stream,opts);
654
+ var chunks=[];
655
+ rec.ondataavailable=function(e){{if(e.data.size>0)chunks.push(e.data)}};
656
+
657
+ document.getElementById('exportMsg').textContent='๋…นํ™” ์ค‘...';
658
+ rec.start(100);
659
+
660
+ var dur=S.dur;
661
+ var fps=30;
662
+ var totalFrames=Math.ceil(dur*fps);
663
+ var frameTime=1/fps;
664
+
665
+ // ํ”„๋ ˆ์ž„ ๋‹จ์œ„๋กœ ์ •ํ™•ํ•˜๊ฒŒ ๋ Œ๋”๋ง
666
+ for(var frame=0;frame<totalFrames;frame++){{
667
+ if(S.cancelled)break;
668
+
669
+ var t=frame*frameTime;
670
+ document.getElementById('exportBar').style.width=(frame/totalFrames*100)+'%';
671
+ document.getElementById('exportMsg').textContent='๋…นํ™” ์ค‘... '+Math.round(frame/totalFrames*100)+'% ('+Math.floor(t)+'/'+Math.floor(dur)+'์ดˆ)';
672
+
673
+ // ์˜คํ”„์Šคํฌ๋ฆฐ ์บ”๋ฒ„์Šค์— ๋จผ์ € ๊ทธ๋ฆผ
674
+ offCtx.fillStyle='#000';
675
+ offCtx.fillRect(0,0,1280,720);
676
+
677
+ var vc=getClipAt(t,'visual');
678
+ if(vc){{
679
+ var el=S.els[vc.mid];
680
+ if(el){{
681
+ if(vc.type==='video'){{
682
+ var clipT=t-vc.start+vc.ts;
683
+ await seekVideo(el,clipT);
684
+ }}
685
+ try{{
686
+ var sw=el.videoWidth||el.naturalWidth||el.width||1280;
687
+ var sh=el.videoHeight||el.naturalHeight||el.height||720;
688
+ var scale=Math.min(1280/sw,720/sh);
689
+ var dw=sw*scale,dh=sh*scale;
690
+ offCtx.drawImage(el,(1280-dw)/2,(720-dh)/2,dw,dh);
691
+ }}catch(e){{}}
692
+ }}
693
+ }}
694
+
695
+ // ์™„์„ฑ๋œ ํ”„๋ ˆ์ž„์„ ๋ฉ”์ธ ์บ”๋ฒ„์Šค๋กœ ํ•œ ๋ฒˆ์— ๋ณต์‚ฌ (๊นœ๋นก์ž„ ๋ฐฉ์ง€)
696
+ ctx.drawImage(offCanvas,0,0);
697
+
698
+ // ํ”„๋ ˆ์ž„ ๊ฐ„๊ฒฉ๋งŒํผ ๋Œ€๊ธฐ
699
+ await new Promise(function(resolve){{setTimeout(resolve,33)}});
700
+ }}
701
+
702
+ // ๋…นํ™” ์ข…๋ฃŒ
703
+ await new Promise(function(resolve){{
704
+ rec.onstop=resolve;
705
+ rec.stop();
706
+ }});
707
+
708
+ if(S.cancelled)return;
709
+
710
+ var webmBlob=new Blob(chunks,{{type:'video/webm'}});
711
+ if(webmBlob.size<1000){{document.getElementById('exportMsg').textContent='๋…นํ™” ์‹คํŒจ';return}}
712
+
713
+ document.getElementById('exportBar').style.width='100%';
714
+ document.getElementById('exportMsg').textContent='์™„๋ฃŒ! ('+Math.round(webmBlob.size/1024/1024*10)/10+'MB) - ์•„๋ž˜์—์„œ ๋‹ค์šด๋กœ๋“œ';
715
+
716
+ var downloadDiv=document.createElement('div');
717
+ downloadDiv.style.marginTop='10px';
718
+ var webmLink=document.createElement('a');
719
+ webmLink.href=URL.createObjectURL(webmBlob);
720
+ webmLink.download='video_'+Date.now()+'.webm';
721
+ webmLink.className='btn btn-success';
722
+ webmLink.textContent='๐Ÿ“ฅ WebM ๋‹ค์šด๋กœ๋“œ';
723
+ webmLink.style.marginRight='5px';
724
+ downloadDiv.appendChild(webmLink);
725
+
726
+ var copyBtn=document.createElement('button');
727
+ copyBtn.className='btn btn-secondary';
728
+ copyBtn.textContent='๐Ÿ“‹ MP4๋ณ€ํ™˜์šฉ ๋ณต์‚ฌ';
729
+ copyBtn.onclick=async function(){{
730
+ document.getElementById('exportMsg').textContent='๋ฐ์ดํ„ฐ ์ค€๋น„ ์ค‘...';
731
+ var reader=new FileReader();
732
+ reader.onload=function(){{
733
+ var base64=reader.result.split(',')[1];
734
+ navigator.clipboard.writeText(base64).then(function(){{
735
+ document.getElementById('exportMsg').textContent='๋ณต์‚ฌ ์™„๋ฃŒ! ์œ„ ์ž…๋ ฅ๋ž€์— ๋ถ™์—ฌ๋„ฃ๊ธฐ';
736
+ alert('๋ณต์‚ฌ ์™„๋ฃŒ!\\n\\nGradio์˜ "WebM ๋ฐ์ดํ„ฐ" ์ž…๋ ฅ๋ž€์— ๋ถ™์—ฌ๋„ฃ๊ธฐ(Ctrl+V) ํ›„\\n"MP4 ๋ณ€ํ™˜" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์„ธ์š”.');
737
+ }}).catch(function(){{
738
+ prompt('์•„๋ž˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜์„ธ์š”:', base64.substring(0,100)+'...');
739
+ }});
740
+ }};
741
+ reader.readAsDataURL(webmBlob);
742
+ }};
743
+ downloadDiv.appendChild(copyBtn);
744
+ document.querySelector('.modal-box').appendChild(downloadDiv);
745
+ }}
746
+
747
+ // ๋น„๋””์˜ค seek ์™„๋ฃŒ ๋Œ€๊ธฐ ํ•จ์ˆ˜
748
+ function seekVideo(video,time){{
749
+ return new Promise(function(resolve){{
750
+ if(Math.abs(video.currentTime-time)<0.05){{
751
+ resolve();
752
+ return;
753
+ }}
754
+ var onSeeked=function(){{
755
+ video.removeEventListener('seeked',onSeeked);
756
+ resolve();
757
+ }};
758
+ video.addEventListener('seeked',onSeeked);
759
+ video.currentTime=time;
760
+ setTimeout(resolve,200);
761
+ }});
762
+ }}
763
+
764
+ function cancelExport(){{S.cancelled=true;document.getElementById('exportModal').style.display='none'}}
765
+
766
+ document.addEventListener('keydown',function(e){{
767
+ if(e.target.tagName==='INPUT')return;
768
+ if(e.code==='Space'){{e.preventDefault();togglePlay()}}
769
+ else if(e.code==='Delete'){{e.preventDefault();delClip()}}
770
+ else if(e.code==='ArrowLeft'){{seek(S.time-0.1)}}
771
+ else if(e.code==='ArrowRight'){{seek(S.time+0.1)}}
772
+ }});
773
+
774
+ init();
775
+ renderTL();
776
+ stat('์ค€๋น„๋จ');
777
+ var initData={media_data};
778
+ if(initData&&initData.length)initData.forEach(function(m){{addMedia(m.name,m.type,m.dataUrl,m.filePath)}});
779
+ </script>
780
+ </body>
781
+ </html>'''
782
+
783
+ def process_file(files):
784
+ """ํŒŒ์ผ ์ฒ˜๋ฆฌ ๋ฐ ์„œ๋ฒ„์— ์ €์žฅ"""
785
+ global uploaded_files
786
+ if not files:
787
+ return []
788
+ results = []
789
+ file_list = files if isinstance(files, list) else [files]
790
+ for f in file_list:
791
+ if not f:
792
+ continue
793
+ path = f.name if hasattr(f, 'name') else f
794
+ name = os.path.basename(path)
795
+ ext = name.lower().split('.')[-1]
796
+ if ext in ['mp4', 'webm', 'mov', 'avi', 'mkv']:
797
+ t, m = 'video', f'video/{ext}'
798
+ elif ext in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
799
+ t, m = 'image', f'image/{ext}'
800
+ elif ext in ['mp3', 'wav', 'ogg', 'm4a', 'aac']:
801
+ t, m = 'audio', f'audio/{ext}'
802
+ else:
803
+ continue
804
+
805
+ # ์„œ๋ฒ„์— ํŒŒ์ผ ๋ณต์‚ฌ (MP4 ๋‚ด๋ณด๋‚ด๊ธฐ์šฉ)
806
+ dst_path = os.path.join(UPLOAD_DIR, f"{int(time.time()*1000)}_{name}")
807
+ shutil.copy(path, dst_path)
808
+ uploaded_files[name] = dst_path
809
+
810
+ with open(path, 'rb') as fp:
811
+ d = base64.b64encode(fp.read()).decode()
812
+ results.append({'name': name, 'type': t, 'dataUrl': f'data:{m};base64,{d}', 'filePath': name})
813
+ return results
814
+
815
+ def make_iframe(data):
816
+ j = json.dumps(data, ensure_ascii=False)
817
+ h = get_editor_html(j).replace("'", "&#39;")
818
+ return f"<iframe srcdoc='{h}' style='width:100%;height:750px;border:none;border-radius:10px'></iframe>"
819
+
820
+ def export_mp4(export_json):
821
+ """์„œ๋ฒ„ ์‚ฌ์ด๋“œ MP4 ๋‚ด๋ณด๋‚ด๊ธฐ"""
822
+ global uploaded_files
823
+
824
+ if not export_json or len(export_json) < 10:
825
+ return None
826
+
827
+ try:
828
+ data = json.loads(export_json)
829
+ clips = data.get('clips', [])
830
+
831
+ if not clips:
832
+ return None
833
+
834
+ video_clips = [c for c in clips if c['type'] in ['video', 'image']]
835
+ if not video_clips:
836
+ return None
837
+
838
+ temp_dir = tempfile.mkdtemp()
839
+ output_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4')
840
+
841
+ # ๋‹จ์ผ ํด๋ฆฝ
842
+ if len(video_clips) == 1:
843
+ clip = video_clips[0]
844
+ file_path = uploaded_files.get(clip['filePath'])
845
+
846
+ if not file_path or not os.path.exists(file_path):
847
+ return None
848
+
849
+ duration = clip['te'] - clip['ts']
850
+
851
+ if clip['type'] == 'image':
852
+ 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]
853
+ else:
854
+ 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]
855
+
856
+ subprocess.run(cmd, capture_output=True, timeout=300)
857
+
858
+ if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
859
+ return output_path
860
+ return None
861
+
862
+ # ์—ฌ๋Ÿฌ ํด๋ฆฝ
863
+ temp_files = []
864
+ concat_file = os.path.join(temp_dir, 'concat.txt')
865
+
866
+ for i, clip in enumerate(sorted(video_clips, key=lambda x: x['start'])):
867
+ file_path = uploaded_files.get(clip['filePath'])
868
+ if not file_path or not os.path.exists(file_path):
869
+ continue
870
+
871
+ temp_out = os.path.join(temp_dir, f'temp_{i}.mp4')
872
+ duration = clip['te'] - clip['ts']
873
+
874
+ if clip['type'] == 'image':
875
+ 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]
876
+ else:
877
+ 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]
878
+
879
+ subprocess.run(cmd, capture_output=True, timeout=120)
880
+ if os.path.exists(temp_out):
881
+ temp_files.append(temp_out)
882
+
883
+ if not temp_files:
884
+ return None
885
+
886
+ with open(concat_file, 'w') as f:
887
+ for tf in temp_files:
888
+ f.write(f"file '{tf}'\n")
889
+
890
+ cmd = ['ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', concat_file, '-c:v', 'libx264', '-preset', 'fast', '-c:a', 'aac', '-movflags', '+faststart', output_path]
891
+ subprocess.run(cmd, capture_output=True, timeout=300)
892
+
893
+ for tf in temp_files:
894
+ try: os.remove(tf)
895
+ except: pass
896
+
897
+ if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
898
+ return output_path
899
+ return None
900
+
901
+ except Exception as e:
902
+ print(f"[Export] Error: {e}")
903
+ return None
904
+
905
+ def convert_webm_to_mp4(webm_base64):
906
+ """WebM base64 ๋ฐ์ดํ„ฐ๋ฅผ MP4๋กœ ๋ณ€ํ™˜"""
907
+ if not webm_base64 or len(webm_base64) < 100:
908
+ return None
909
+
910
+ try:
911
+ # base64 ๋””์ฝ”๋”ฉ
912
+ webm_data = base64.b64decode(webm_base64)
913
+
914
+ temp_dir = tempfile.mkdtemp()
915
+ webm_path = os.path.join(temp_dir, 'input.webm')
916
+ mp4_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4')
917
+
918
+ # WebM ํŒŒ์ผ ์ €์žฅ
919
+ with open(webm_path, 'wb') as f:
920
+ f.write(webm_data)
921
+
922
+ # FFmpeg๋กœ MP4 ๋ณ€ํ™˜
923
+ cmd = [
924
+ 'ffmpeg', '-y',
925
+ '-i', webm_path,
926
+ '-c:v', 'libx264',
927
+ '-preset', 'fast',
928
+ '-crf', '23',
929
+ '-c:a', 'aac',
930
+ '-b:a', '128k',
931
+ '-movflags', '+faststart',
932
+ mp4_path
933
+ ]
934
+
935
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
936
+
937
+ # WebM ํŒŒ์ผ ์‚ญ์ œ
938
+ try:
939
+ os.remove(webm_path)
940
+ except:
941
+ pass
942
+
943
+ if os.path.exists(mp4_path) and os.path.getsize(mp4_path) > 0:
944
+ return mp4_path
945
+
946
+ return None
947
+
948
+ except Exception as e:
949
+ print(f"[Convert] Error: {e}")
950
+ return None
951
+
952
+ with gr.Blocks() as demo:
953
+ gr.Markdown("## ๐ŸŽฌ Video Editor")
954
+
955
+ f = gr.File(label="๐Ÿ“ ํŒŒ์ผ ์—…๋กœ๋“œ", file_count="multiple", file_types=["video", "image", "audio"])
956
+ e = gr.HTML(value=make_iframe([]))
957
+
958
+ gr.Markdown("---")
959
+ gr.Markdown("### ๐Ÿ“ฅ MP4 ๋ณ€ํ™˜")
960
+ gr.Markdown("์—๋””ํ„ฐ์—์„œ '๋‚ด๋ณด๋‚ด๊ธฐ' โ†’ 'MP4๋ณ€ํ™˜์šฉ ๋ณต์‚ฌ' ํด๋ฆญ ํ›„ ์•„๋ž˜์— ๋ถ™์—ฌ๋„ฃ๊ธฐ")
961
+
962
+ with gr.Row():
963
+ webm_data = gr.Textbox(label="WebM ๋ฐ์ดํ„ฐ (base64)", placeholder="์—ฌ๊ธฐ์— ๋ถ™์—ฌ๋„ฃ๊ธฐ (Ctrl+V)", lines=2, scale=4)
964
+ convert_btn = gr.Button("๐ŸŽฌ MP4 ๋ณ€ํ™˜", variant="primary", scale=1)
965
+
966
+ mp4_output = gr.File(label="๐Ÿ“ฅ MP4 ๋‹ค์šด๋กœ๋“œ")
967
+
968
+ f.change(fn=lambda x: make_iframe(process_file(x)), inputs=[f], outputs=[e])
969
+ convert_btn.click(fn=convert_webm_to_mp4, inputs=[webm_data], outputs=[mp4_output])
970
+
971
+ if __name__ == "__main__":
972
+ demo.launch()