Delete app-backup6.py
Browse files- app-backup6.py +0 -992
app-backup6.py
DELETED
|
@@ -1,992 +0,0 @@
|
|
| 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 frameInterval=1000/fps; // 33.33ms
|
| 663 |
-
var totalFrames=Math.ceil(dur*fps);
|
| 664 |
-
var startTime=performance.now();
|
| 665 |
-
|
| 666 |
-
// 실시간 기반 렌더링 (정확한 타이밍)
|
| 667 |
-
await new Promise(function(resolve){{
|
| 668 |
-
var frameCount=0;
|
| 669 |
-
var lastVideoEl=null;
|
| 670 |
-
var lastClipId=null;
|
| 671 |
-
|
| 672 |
-
function render(){{
|
| 673 |
-
if(S.cancelled){{rec.stop();resolve();return}}
|
| 674 |
-
|
| 675 |
-
var elapsed=performance.now()-startTime;
|
| 676 |
-
var t=elapsed/1000; // 현재 시간 (초)
|
| 677 |
-
|
| 678 |
-
if(t>=dur){{
|
| 679 |
-
rec.stop();
|
| 680 |
-
setTimeout(resolve,100);
|
| 681 |
-
return;
|
| 682 |
-
}}
|
| 683 |
-
|
| 684 |
-
// 프로그레스 업데이트
|
| 685 |
-
document.getElementById('exportBar').style.width=(t/dur*100)+'%';
|
| 686 |
-
document.getElementById('exportMsg').textContent='녹화 중... '+Math.round(t/dur*100)+'% ('+t.toFixed(1)+'/'+dur.toFixed(1)+'초)';
|
| 687 |
-
|
| 688 |
-
// 오프스크린 캔버스에 렌더링
|
| 689 |
-
offCtx.fillStyle='#000';
|
| 690 |
-
offCtx.fillRect(0,0,1280,720);
|
| 691 |
-
|
| 692 |
-
var vc=getClipAt(t,'visual');
|
| 693 |
-
if(vc){{
|
| 694 |
-
var el=S.els[vc.mid];
|
| 695 |
-
if(el){{
|
| 696 |
-
if(vc.type==='video'){{
|
| 697 |
-
var clipT=t-vc.start+vc.ts;
|
| 698 |
-
|
| 699 |
-
// 클립이 바뀌면 seek하고 재생
|
| 700 |
-
if(lastClipId!==vc.id){{
|
| 701 |
-
el.currentTime=clipT;
|
| 702 |
-
el.play().catch(function(){{}});
|
| 703 |
-
lastClipId=vc.id;
|
| 704 |
-
lastVideoEl=el;
|
| 705 |
-
}}
|
| 706 |
-
|
| 707 |
-
// 이전 비디오 정지
|
| 708 |
-
if(lastVideoEl&&lastVideoEl!==el&&!lastVideoEl.paused){{
|
| 709 |
-
lastVideoEl.pause();
|
| 710 |
-
}}
|
| 711 |
-
lastVideoEl=el;
|
| 712 |
-
}}
|
| 713 |
-
|
| 714 |
-
try{{
|
| 715 |
-
var sw=el.videoWidth||el.naturalWidth||el.width||1280;
|
| 716 |
-
var sh=el.videoHeight||el.naturalHeight||el.height||720;
|
| 717 |
-
var scale=Math.min(1280/sw,720/sh);
|
| 718 |
-
var dw=sw*scale,dh=sh*scale;
|
| 719 |
-
offCtx.drawImage(el,(1280-dw)/2,(720-dh)/2,dw,dh);
|
| 720 |
-
}}catch(e){{}}
|
| 721 |
-
}}
|
| 722 |
-
}}else{{
|
| 723 |
-
// 클립 없으면 이전 비디오 정지
|
| 724 |
-
if(lastVideoEl&&!lastVideoEl.paused){{
|
| 725 |
-
lastVideoEl.pause();
|
| 726 |
-
lastClipId=null;
|
| 727 |
-
}}
|
| 728 |
-
}}
|
| 729 |
-
|
| 730 |
-
// 완성된 프레임을 메인 캔버스로 복사
|
| 731 |
-
ctx.drawImage(offCanvas,0,0);
|
| 732 |
-
|
| 733 |
-
requestAnimationFrame(render);
|
| 734 |
-
}}
|
| 735 |
-
|
| 736 |
-
requestAnimationFrame(render);
|
| 737 |
-
}});
|
| 738 |
-
|
| 739 |
-
// 모든 비디오 정지
|
| 740 |
-
Object.keys(S.els).forEach(function(k){{
|
| 741 |
-
var el=S.els[k];
|
| 742 |
-
if(el&&el.pause)el.pause();
|
| 743 |
-
}});
|
| 744 |
-
|
| 745 |
-
if(S.cancelled)return;
|
| 746 |
-
|
| 747 |
-
var webmBlob=new Blob(chunks,{{type:'video/webm'}});
|
| 748 |
-
if(webmBlob.size<1000){{document.getElementById('exportMsg').textContent='녹화 실패';return}}
|
| 749 |
-
|
| 750 |
-
document.getElementById('exportBar').style.width='100%';
|
| 751 |
-
document.getElementById('exportMsg').textContent='완료! ('+Math.round(webmBlob.size/1024/1024*10)/10+'MB) - 아래에서 다운로드';
|
| 752 |
-
|
| 753 |
-
var downloadDiv=document.createElement('div');
|
| 754 |
-
downloadDiv.style.marginTop='10px';
|
| 755 |
-
var webmLink=document.createElement('a');
|
| 756 |
-
webmLink.href=URL.createObjectURL(webmBlob);
|
| 757 |
-
webmLink.download='video_'+Date.now()+'.webm';
|
| 758 |
-
webmLink.className='btn btn-success';
|
| 759 |
-
webmLink.textContent='📥 WebM 다운로드';
|
| 760 |
-
webmLink.style.marginRight='5px';
|
| 761 |
-
downloadDiv.appendChild(webmLink);
|
| 762 |
-
|
| 763 |
-
var copyBtn=document.createElement('button');
|
| 764 |
-
copyBtn.className='btn btn-secondary';
|
| 765 |
-
copyBtn.textContent='📋 MP4변환용 복사';
|
| 766 |
-
copyBtn.onclick=async function(){{
|
| 767 |
-
document.getElementById('exportMsg').textContent='데이터 준비 중...';
|
| 768 |
-
var reader=new FileReader();
|
| 769 |
-
reader.onload=function(){{
|
| 770 |
-
var base64=reader.result.split(',')[1];
|
| 771 |
-
navigator.clipboard.writeText(base64).then(function(){{
|
| 772 |
-
document.getElementById('exportMsg').textContent='복사 완료! 위 입력란에 붙여넣기';
|
| 773 |
-
alert('복사 완료!\\n\\nGradio의 "WebM 데이터" 입력란에 붙여넣기(Ctrl+V) 후\\n"MP4 변환" 버튼을 클릭하세요.');
|
| 774 |
-
}}).catch(function(){{
|
| 775 |
-
prompt('아래 데이터를 복사하세요:', base64.substring(0,100)+'...');
|
| 776 |
-
}});
|
| 777 |
-
}};
|
| 778 |
-
reader.readAsDataURL(webmBlob);
|
| 779 |
-
}};
|
| 780 |
-
downloadDiv.appendChild(copyBtn);
|
| 781 |
-
document.querySelector('.modal-box').appendChild(downloadDiv);
|
| 782 |
-
}}
|
| 783 |
-
|
| 784 |
-
function cancelExport(){{S.cancelled=true;document.getElementById('exportModal').style.display='none'}}
|
| 785 |
-
|
| 786 |
-
document.addEventListener('keydown',function(e){{
|
| 787 |
-
if(e.target.tagName==='INPUT')return;
|
| 788 |
-
if(e.code==='Space'){{e.preventDefault();togglePlay()}}
|
| 789 |
-
else if(e.code==='Delete'){{e.preventDefault();delClip()}}
|
| 790 |
-
else if(e.code==='ArrowLeft'){{seek(S.time-0.1)}}
|
| 791 |
-
else if(e.code==='ArrowRight'){{seek(S.time+0.1)}}
|
| 792 |
-
}});
|
| 793 |
-
|
| 794 |
-
init();
|
| 795 |
-
renderTL();
|
| 796 |
-
stat('준비됨');
|
| 797 |
-
var initData={media_data};
|
| 798 |
-
if(initData&&initData.length)initData.forEach(function(m){{addMedia(m.name,m.type,m.dataUrl,m.filePath)}});
|
| 799 |
-
</script>
|
| 800 |
-
</body>
|
| 801 |
-
</html>'''
|
| 802 |
-
|
| 803 |
-
def process_file(files):
|
| 804 |
-
"""파일 처리 및 서버에 저장"""
|
| 805 |
-
global uploaded_files
|
| 806 |
-
if not files:
|
| 807 |
-
return []
|
| 808 |
-
results = []
|
| 809 |
-
file_list = files if isinstance(files, list) else [files]
|
| 810 |
-
for f in file_list:
|
| 811 |
-
if not f:
|
| 812 |
-
continue
|
| 813 |
-
path = f.name if hasattr(f, 'name') else f
|
| 814 |
-
name = os.path.basename(path)
|
| 815 |
-
ext = name.lower().split('.')[-1]
|
| 816 |
-
if ext in ['mp4', 'webm', 'mov', 'avi', 'mkv']:
|
| 817 |
-
t, m = 'video', f'video/{ext}'
|
| 818 |
-
elif ext in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
|
| 819 |
-
t, m = 'image', f'image/{ext}'
|
| 820 |
-
elif ext in ['mp3', 'wav', 'ogg', 'm4a', 'aac']:
|
| 821 |
-
t, m = 'audio', f'audio/{ext}'
|
| 822 |
-
else:
|
| 823 |
-
continue
|
| 824 |
-
|
| 825 |
-
# 서버에 파일 복사 (MP4 내보내기용)
|
| 826 |
-
dst_path = os.path.join(UPLOAD_DIR, f"{int(time.time()*1000)}_{name}")
|
| 827 |
-
shutil.copy(path, dst_path)
|
| 828 |
-
uploaded_files[name] = dst_path
|
| 829 |
-
|
| 830 |
-
with open(path, 'rb') as fp:
|
| 831 |
-
d = base64.b64encode(fp.read()).decode()
|
| 832 |
-
results.append({'name': name, 'type': t, 'dataUrl': f'data:{m};base64,{d}', 'filePath': name})
|
| 833 |
-
return results
|
| 834 |
-
|
| 835 |
-
def make_iframe(data):
|
| 836 |
-
j = json.dumps(data, ensure_ascii=False)
|
| 837 |
-
h = get_editor_html(j).replace("'", "'")
|
| 838 |
-
return f"<iframe srcdoc='{h}' style='width:100%;height:750px;border:none;border-radius:10px'></iframe>"
|
| 839 |
-
|
| 840 |
-
def export_mp4(export_json):
|
| 841 |
-
"""서버 사이드 MP4 내보내기"""
|
| 842 |
-
global uploaded_files
|
| 843 |
-
|
| 844 |
-
if not export_json or len(export_json) < 10:
|
| 845 |
-
return None
|
| 846 |
-
|
| 847 |
-
try:
|
| 848 |
-
data = json.loads(export_json)
|
| 849 |
-
clips = data.get('clips', [])
|
| 850 |
-
|
| 851 |
-
if not clips:
|
| 852 |
-
return None
|
| 853 |
-
|
| 854 |
-
video_clips = [c for c in clips if c['type'] in ['video', 'image']]
|
| 855 |
-
if not video_clips:
|
| 856 |
-
return None
|
| 857 |
-
|
| 858 |
-
temp_dir = tempfile.mkdtemp()
|
| 859 |
-
output_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4')
|
| 860 |
-
|
| 861 |
-
# 단일 클립
|
| 862 |
-
if len(video_clips) == 1:
|
| 863 |
-
clip = video_clips[0]
|
| 864 |
-
file_path = uploaded_files.get(clip['filePath'])
|
| 865 |
-
|
| 866 |
-
if not file_path or not os.path.exists(file_path):
|
| 867 |
-
return None
|
| 868 |
-
|
| 869 |
-
duration = clip['te'] - clip['ts']
|
| 870 |
-
|
| 871 |
-
if clip['type'] == 'image':
|
| 872 |
-
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]
|
| 873 |
-
else:
|
| 874 |
-
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]
|
| 875 |
-
|
| 876 |
-
subprocess.run(cmd, capture_output=True, timeout=300)
|
| 877 |
-
|
| 878 |
-
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 879 |
-
return output_path
|
| 880 |
-
return None
|
| 881 |
-
|
| 882 |
-
# 여러 클립
|
| 883 |
-
temp_files = []
|
| 884 |
-
concat_file = os.path.join(temp_dir, 'concat.txt')
|
| 885 |
-
|
| 886 |
-
for i, clip in enumerate(sorted(video_clips, key=lambda x: x['start'])):
|
| 887 |
-
file_path = uploaded_files.get(clip['filePath'])
|
| 888 |
-
if not file_path or not os.path.exists(file_path):
|
| 889 |
-
continue
|
| 890 |
-
|
| 891 |
-
temp_out = os.path.join(temp_dir, f'temp_{i}.mp4')
|
| 892 |
-
duration = clip['te'] - clip['ts']
|
| 893 |
-
|
| 894 |
-
if clip['type'] == 'image':
|
| 895 |
-
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]
|
| 896 |
-
else:
|
| 897 |
-
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]
|
| 898 |
-
|
| 899 |
-
subprocess.run(cmd, capture_output=True, timeout=120)
|
| 900 |
-
if os.path.exists(temp_out):
|
| 901 |
-
temp_files.append(temp_out)
|
| 902 |
-
|
| 903 |
-
if not temp_files:
|
| 904 |
-
return None
|
| 905 |
-
|
| 906 |
-
with open(concat_file, 'w') as f:
|
| 907 |
-
for tf in temp_files:
|
| 908 |
-
f.write(f"file '{tf}'\n")
|
| 909 |
-
|
| 910 |
-
cmd = ['ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', concat_file, '-c:v', 'libx264', '-preset', 'fast', '-c:a', 'aac', '-movflags', '+faststart', output_path]
|
| 911 |
-
subprocess.run(cmd, capture_output=True, timeout=300)
|
| 912 |
-
|
| 913 |
-
for tf in temp_files:
|
| 914 |
-
try: os.remove(tf)
|
| 915 |
-
except: pass
|
| 916 |
-
|
| 917 |
-
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 918 |
-
return output_path
|
| 919 |
-
return None
|
| 920 |
-
|
| 921 |
-
except Exception as e:
|
| 922 |
-
print(f"[Export] Error: {e}")
|
| 923 |
-
return None
|
| 924 |
-
|
| 925 |
-
def convert_webm_to_mp4(webm_base64):
|
| 926 |
-
"""WebM base64 데이터를 MP4로 변환"""
|
| 927 |
-
if not webm_base64 or len(webm_base64) < 100:
|
| 928 |
-
return None
|
| 929 |
-
|
| 930 |
-
try:
|
| 931 |
-
# base64 디코딩
|
| 932 |
-
webm_data = base64.b64decode(webm_base64)
|
| 933 |
-
|
| 934 |
-
temp_dir = tempfile.mkdtemp()
|
| 935 |
-
webm_path = os.path.join(temp_dir, 'input.webm')
|
| 936 |
-
mp4_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4')
|
| 937 |
-
|
| 938 |
-
# WebM 파일 저장
|
| 939 |
-
with open(webm_path, 'wb') as f:
|
| 940 |
-
f.write(webm_data)
|
| 941 |
-
|
| 942 |
-
# FFmpeg로 MP4 변환
|
| 943 |
-
cmd = [
|
| 944 |
-
'ffmpeg', '-y',
|
| 945 |
-
'-i', webm_path,
|
| 946 |
-
'-c:v', 'libx264',
|
| 947 |
-
'-preset', 'fast',
|
| 948 |
-
'-crf', '23',
|
| 949 |
-
'-c:a', 'aac',
|
| 950 |
-
'-b:a', '128k',
|
| 951 |
-
'-movflags', '+faststart',
|
| 952 |
-
mp4_path
|
| 953 |
-
]
|
| 954 |
-
|
| 955 |
-
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
|
| 956 |
-
|
| 957 |
-
# WebM 파일 삭제
|
| 958 |
-
try:
|
| 959 |
-
os.remove(webm_path)
|
| 960 |
-
except:
|
| 961 |
-
pass
|
| 962 |
-
|
| 963 |
-
if os.path.exists(mp4_path) and os.path.getsize(mp4_path) > 0:
|
| 964 |
-
return mp4_path
|
| 965 |
-
|
| 966 |
-
return None
|
| 967 |
-
|
| 968 |
-
except Exception as e:
|
| 969 |
-
print(f"[Convert] Error: {e}")
|
| 970 |
-
return None
|
| 971 |
-
|
| 972 |
-
with gr.Blocks() as demo:
|
| 973 |
-
gr.Markdown("## 🎬 Video Editor")
|
| 974 |
-
|
| 975 |
-
f = gr.File(label="📁 파일 업로드", file_count="multiple", file_types=["video", "image", "audio"])
|
| 976 |
-
e = gr.HTML(value=make_iframe([]))
|
| 977 |
-
|
| 978 |
-
gr.Markdown("---")
|
| 979 |
-
gr.Markdown("### 📥 MP4 변환")
|
| 980 |
-
gr.Markdown("에디터에서 '내보내기' → 'MP4변환용 복사' 클릭 후 아래에 붙여넣기")
|
| 981 |
-
|
| 982 |
-
with gr.Row():
|
| 983 |
-
webm_data = gr.Textbox(label="WebM 데이터 (base64)", placeholder="여기에 붙여넣기 (Ctrl+V)", lines=2, scale=4)
|
| 984 |
-
convert_btn = gr.Button("🎬 MP4 변환", variant="primary", scale=1)
|
| 985 |
-
|
| 986 |
-
mp4_output = gr.File(label="📥 MP4 다운로드")
|
| 987 |
-
|
| 988 |
-
f.change(fn=lambda x: make_iframe(process_file(x)), inputs=[f], outputs=[e])
|
| 989 |
-
convert_btn.click(fn=convert_webm_to_mp4, inputs=[webm_data], outputs=[mp4_output])
|
| 990 |
-
|
| 991 |
-
if __name__ == "__main__":
|
| 992 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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