| """ |
| Simple Video Editor - Canvas ๊ธฐ๋ฐ ๋ ๋๋ง |
| ๋น ํ๋ ์ ๋ฌธ์ ์์ ํด๊ฒฐ + ์๋ฒ ์ฌ์ด๋ MP4 ๋ด๋ณด๋ด๊ธฐ |
| ๋ธ๋ฌ ๋ฐฐ๊ฒฝ ๋ด๋ณด๋ด๊ธฐ ์์ : ๋๋ธ ๋ฒํผ๋ง |
| ๐จ Comic Classic Theme ์ ์ฉ |
| """ |
|
|
| import gradio as gr |
| import base64 |
| import os |
| import json |
| import subprocess |
| import tempfile |
| import shutil |
| import time |
|
|
| |
| try: |
| import spaces |
| @spaces.GPU |
| def dummy_gpu_function(): |
| """Spaces GPU requirement๋ฅผ ์ถฉ์กฑํ๊ธฐ ์ํ ๋๋ฏธ ํจ์""" |
| return None |
| except ImportError: |
| pass |
|
|
| |
| UPLOAD_DIR = tempfile.mkdtemp() |
| uploaded_files = {} |
|
|
| def get_editor_html(media_data="[]"): |
| return f'''<!DOCTYPE html> |
| <html lang="ko"> |
| <head> |
| <meta charset="UTF-8"> |
| <style> |
| *{{margin:0;padding:0;box-sizing:border-box}} |
| body{{font-family:-apple-system,BlinkMacSystemFont,sans-serif;background:#f5f5f7;font-size:13px}} |
| .editor{{display:flex;flex-direction:column;height:100vh}} |
| .toolbar{{height:44px;background:#fff;border-bottom:1px solid #ddd;display:flex;align-items:center;justify-content:space-between;padding:0 12px}} |
| .toolbar-title{{font-size:15px;font-weight:600}} |
| .btn{{padding:5px 10px;border:none;border-radius:5px;cursor:pointer;font-size:11px;font-weight:500}} |
| .btn-secondary{{background:#f0f0f0;color:#333}} |
| .btn-secondary:hover{{background:#e0e0e0}} |
| .btn-success{{background:#10b981;color:#fff}} |
| .btn-danger{{background:#ef4444;color:#fff}} |
| .main{{display:flex;flex:1;overflow:hidden}} |
| .library{{width:160px;background:#fff;border-right:1px solid #ddd;display:flex;flex-direction:column}} |
| .lib-header{{padding:8px;border-bottom:1px solid #eee;font-size:10px;font-weight:600;color:#666}} |
| .lib-content{{flex:1;overflow-y:auto;padding:6px}} |
| .lib-hint{{text-align:center;padding:15px;color:#999;font-size:10px}} |
| .media-grid{{display:grid;grid-template-columns:repeat(2,1fr);gap:4px}} |
| .media-item{{aspect-ratio:16/9;background:#f0f0f0;border-radius:4px;overflow:hidden;cursor:grab;position:relative;border:1px solid #e0e0e0}} |
| .media-item:hover{{border-color:#6366f1}} |
| .media-item img{{width:100%;height:100%;object-fit:cover}} |
| .media-item-icon{{width:100%;height:100%;display:flex;align-items:center;justify-content:center;font-size:18px}} |
| .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}} |
| .preview-area{{flex:1;display:flex;flex-direction:column;background:#1a1a1a;margin:6px;border-radius:8px;overflow:hidden}} |
| .preview-box{{flex:1;display:flex;align-items:center;justify-content:center;background:#000;overflow:hidden}} |
| #previewCanvas{{background:#000;box-shadow:0 0 20px rgba(0,0,0,0.5);cursor:default}} |
| .controls{{height:45px;background:#222;display:flex;align-items:center;justify-content:center;gap:6px}} |
| .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}} |
| .ctrl-btn:hover{{background:rgba(255,255,255,0.2)}} |
| .ctrl-btn.play{{width:36px;height:36px;background:#6366f1}} |
| .time-display{{font-family:monospace;font-size:10px;color:#aaa;min-width:90px;text-align:center}} |
| .props{{width:140px;background:#fff;border-left:1px solid #ddd}} |
| .props-header{{padding:8px;border-bottom:1px solid #eee;font-size:10px;font-weight:600;color:#666}} |
| .props-content{{padding:8px}} |
| .no-sel{{color:#999;text-align:center;padding:15px;font-size:10px}} |
| .prop-group{{margin-bottom:10px}} |
| .prop-label{{font-size:9px;color:#666;margin-bottom:2px}} |
| .prop-input{{width:100%;padding:4px;border:1px solid #ddd;border-radius:3px;font-size:10px}} |
| .timeline{{height:180px;background:#fff;border-top:1px solid #ddd;display:flex;flex-direction:column}} |
| .tl-toolbar{{height:28px;background:#fafafa;border-bottom:1px solid #eee;display:flex;align-items:center;padding:0 6px;gap:4px}} |
| .tl-toolbar .btn{{padding:2px 6px;font-size:9px}} |
| .tl-zoom{{display:flex;align-items:center;gap:3px;margin-left:auto;font-size:9px;color:#666}} |
| .tl-zoom input{{width:50px}} |
| .tl-container{{flex:1;overflow-x:auto;position:relative}} |
| .tl-ruler{{height:18px;background:#fff;border-bottom:1px solid #eee}} |
| .tl-tracks{{position:relative}} |
| .tl-track{{height:45px;border-bottom:1px solid #eee;display:flex}} |
| .tl-track:nth-child(2){{background:#fffbeb}} |
| .tl-track:nth-child(3){{background:#fdf2f8}} |
| .track-label{{width:50px;padding:0 4px;font-size:8px;color:#666;background:#fafafa;display:flex;align-items:center;border-right:1px solid #eee}} |
| .track-content{{flex:1;position:relative;min-width:600px}} |
| .clip{{position:absolute;height:36px;top:4px;border-radius:4px;cursor:grab;display:flex;align-items:center;overflow:hidden}} |
| .clip:hover{{box-shadow:0 0 0 2px #6366f1}} |
| .clip.selected{{box-shadow:0 0 0 2px #6366f1}} |
| .clip.video{{background:linear-gradient(135deg,#818cf8,#6366f1)}} |
| .clip.image{{background:linear-gradient(135deg,#34d399,#10b981)}} |
| .clip.audio{{background:linear-gradient(135deg,#fbbf24,#f59e0b)}} |
| .clip.text{{background:linear-gradient(135deg,#f472b6,#ec4899)}} |
| .clip-thumb{{width:36px;height:100%;object-fit:cover}} |
| .clip-info{{padding:0 4px;flex:1;overflow:hidden}} |
| .clip-name{{font-size:8px;color:#fff;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}} |
| .clip-dur{{font-size:7px;color:rgba(255,255,255,0.7)}} |
| .clip-handle{{position:absolute;top:0;bottom:0;width:8px;background:rgba(255,255,255,0.5);cursor:ew-resize;opacity:0}} |
| .clip:hover .clip-handle{{opacity:1}} |
| .clip-handle-l{{left:0;border-radius:4px 0 0 4px}} |
| .clip-handle-r{{right:0;border-radius:0 4px 4px 0}} |
| .playhead{{position:absolute;top:0;bottom:0;width:2px;background:#ef4444;z-index:10;pointer-events:none}} |
| .playhead::before{{content:"";position:absolute;top:0;left:-4px;border-left:5px solid transparent;border-right:5px solid transparent;border-top:7px solid #ef4444}} |
| .drop-zone{{background:rgba(99,102,241,0.1)!important;outline:2px dashed #6366f1!important}} |
| .status{{height:20px;background:#f5f5f5;border-top:1px solid #ddd;display:flex;align-items:center;padding:0 8px;font-size:9px;color:#666}} |
| .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}} |
| .ctx-item{{padding:5px 10px;cursor:pointer;font-size:10px}} |
| .ctx-item:hover{{background:#f5f5f5}} |
| .ctx-item.danger{{color:#ef4444}} |
| .modal{{position:fixed;inset:0;background:rgba(0,0,0,0.5);display:flex;align-items:center;justify-content:center;z-index:1000}} |
| .modal-box{{background:#fff;border-radius:8px;padding:16px;min-width:240px;text-align:center}} |
| .modal-box h3{{margin-bottom:8px;font-size:13px}} |
| .progress{{height:5px;background:#eee;border-radius:3px;overflow:hidden;margin:10px 0}} |
| .progress-bar{{height:100%;background:#6366f1;transition:width 0.2s}} |
| .hidden-media{{position:absolute;left:-9999px;top:-9999px;width:1px;height:1px;opacity:0;pointer-events:none}} |
| </style> |
| </head> |
| <body> |
| <div class="editor"> |
| <div class="toolbar"> |
| <div class="toolbar-title">๐ฌ Video Editor</div> |
| <div style="display:flex;gap:8px;align-items:center"> |
| <select id="ratioSelect" onchange="setRatio(this.value)" style="padding:4px 8px;border-radius:4px;border:1px solid #ddd;font-size:11px"> |
| <option value="16:9">16:9 (๊ฐ๋ก)</option> |
| <option value="9:16">9:16 (์ธ๋ก)</option> |
| <option value="1:1">1:1 (์ ์ฌ๊ฐ)</option> |
| <option value="4:3">4:3</option> |
| <option value="4:5">4:5 (์ธ์คํ)</option> |
| </select> |
| <select id="fillMode" onchange="setFillMode(this.value)" style="padding:4px 8px;border-radius:4px;border:1px solid #ddd;font-size:11px"> |
| <option value="fit">๋ง์ถค (์ฌ๋ฐฑ)</option> |
| <option value="fill">์ฑ์ฐ๊ธฐ (ํ๋)</option> |
| <option value="blur">๋ธ๋ฌ ๋ฐฐ๊ฒฝ</option> |
| </select> |
| <button class="btn btn-secondary" onclick="undo()">โฉ ์คํ์ทจ์</button> |
| <button class="btn btn-success" onclick="exportVideo()">๐ฅ ๋ด๋ณด๋ด๊ธฐ</button> |
| </div> |
| </div> |
| <div class="main"> |
| <div class="library"> |
| <div class="lib-header">๐ ๋ฏธ๋์ด</div> |
| <div class="lib-content"> |
| <div class="lib-hint" id="hint">ํ์ผ์ ์
๋ก๋ํ์ธ์</div> |
| <div class="media-grid" id="mediaGrid"></div> |
| </div> |
| </div> |
| <div class="preview-area"> |
| <div class="preview-box"> |
| <canvas id="previewCanvas" width="640" height="360"></canvas> |
| </div> |
| <div class="controls"> |
| <button class="ctrl-btn" onclick="seek(0)">โฎ</button> |
| <button class="ctrl-btn" onclick="seek(S.time-5)">โช</button> |
| <button class="ctrl-btn play" onclick="togglePlay()" id="playBtn">โถ</button> |
| <button class="ctrl-btn" onclick="seek(S.time+5)">โฉ</button> |
| <button class="ctrl-btn" onclick="seek(S.dur)">โญ</button> |
| <div class="time-display"><span id="curT">00:00.00</span> / <span id="totT">00:00.00</span></div> |
| <button class="ctrl-btn" onclick="toggleMute()" id="muteBtn">๐</button> |
| </div> |
| </div> |
| <div class="props"> |
| <div class="props-header">โ๏ธ ์์ฑ</div> |
| <div class="props-content" id="propsBox"><div class="no-sel">ํด๋ฆฝ ์ ํ</div></div> |
| </div> |
| </div> |
| <div class="timeline"> |
| <div class="tl-toolbar"> |
| <button class="btn btn-secondary" onclick="addTextClip()">๐ ํ
์คํธ</button> |
| <button class="btn btn-secondary" onclick="splitClip()">โ ์๋ฅด๊ธฐ</button> |
| <button class="btn btn-secondary" onclick="dupeClip()">๐ ๋ณต์ </button> |
| <button class="btn btn-danger" onclick="delClip()">๐ ์ญ์ </button> |
| <div class="tl-zoom">๐<input type="range" min="0.5" max="3" step="0.1" value="1" oninput="setZoom(this.value)"></div> |
| </div> |
| <div class="tl-container" id="tlBox" onclick="tlClick(event)"> |
| <div class="tl-ruler" id="ruler"></div> |
| <div class="tl-tracks"> |
| <div class="tl-track"><div class="track-label">๐ฌ ์์</div><div class="track-content" id="t0"></div></div> |
| <div class="tl-track"><div class="track-label">๐ต ์ค๋์ค</div><div class="track-content" id="t1"></div></div> |
| <div class="tl-track"><div class="track-label">๐ ํ
์คํธ</div><div class="track-content" id="t2"></div></div> |
| </div> |
| <div class="playhead" id="playhead" style="left:50px"></div> |
| </div> |
| </div> |
| <div class="status" id="status">์ค๋น๋จ</div> |
| </div> |
| <div class="ctx-menu" id="ctx"> |
| <div class="ctx-item" onclick="splitClip()">โ ์๋ฅด๊ธฐ</div> |
| <div class="ctx-item" onclick="dupeClip()">๐ ๋ณต์ </div> |
| <div class="ctx-item danger" onclick="delClip()">๐ ์ญ์ </div> |
| </div> |
| <div class="modal" id="exportModal" style="display:none"> |
| <div class="modal-box"> |
| <h3>๐ฌ ์์ ๋ด๋ณด๋ด๊ธฐ</h3> |
| <p id="exportMsg">์ค๋น์ค...</p> |
| <div class="progress"><div class="progress-bar" id="exportBar"></div></div> |
| <button class="btn btn-secondary" onclick="cancelExport()">์ทจ์</button> |
| </div> |
| </div> |
| <div class="modal" id="textModal" style="display:none"> |
| <div class="modal-box" style="min-width:320px"> |
| <h3>๐ ํ
์คํธ ์ถ๊ฐ</h3> |
| <div style="margin:10px 0;text-align:left"> |
| <div style="margin-bottom:8px"> |
| <label style="font-size:11px;color:#666">ํ
์คํธ ๋ด์ฉ</label> |
| <input type="text" id="textInput" placeholder="ํ
์คํธ ์
๋ ฅ..." style="width:100%;padding:8px;border:1px solid #ddd;border-radius:4px;margin-top:4px"> |
| </div> |
| <div style="display:flex;gap:8px;margin-bottom:8px"> |
| <div style="flex:1"> |
| <label style="font-size:11px;color:#666">๊ธ์ ํฌ๊ธฐ</label> |
| <select id="textSize" style="width:100%;padding:6px;border:1px solid #ddd;border-radius:4px;margin-top:4px"> |
| <option value="24">์๊ฒ (24px)</option> |
| <option value="36">๋ณดํต (36px)</option> |
| <option value="48" selected>ํฌ๊ฒ (48px)</option> |
| <option value="64">๋งค์ฐ ํฌ๊ฒ (64px)</option> |
| <option value="96">์ต๋ (96px)</option> |
| </select> |
| </div> |
| <div style="flex:1"> |
| <label style="font-size:11px;color:#666">๊ธ์ ์์</label> |
| <input type="color" id="textColor" value="#ffffff" style="width:100%;height:32px;border:1px solid #ddd;border-radius:4px;margin-top:4px;cursor:pointer"> |
| </div> |
| </div> |
| <div style="display:flex;gap:8px;margin-bottom:8px"> |
| <div style="flex:1"> |
| <label style="font-size:11px;color:#666">๋ฐฐ๊ฒฝ ์คํ์ผ</label> |
| <select id="textBgStyle" onchange="toggleBgColor()" style="width:100%;padding:6px;border:1px solid #ddd;border-radius:4px;margin-top:4px"> |
| <option value="none">๋ฐฐ๊ฒฝ ์์</option> |
| <option value="solid" selected>๋จ์ ๋ฐฐ๊ฒฝ</option> |
| <option value="rounded">๋ฅ๊ทผ ๋ฐฐ๊ฒฝ</option> |
| </select> |
| </div> |
| <div style="flex:1" id="bgColorBox"> |
| <label style="font-size:11px;color:#666">๋ฐฐ๊ฒฝ ์์</label> |
| <input type="color" id="textBgColor" value="#000000" style="width:100%;height:32px;border:1px solid #ddd;border-radius:4px;margin-top:4px;cursor:pointer"> |
| </div> |
| </div> |
| <div style="margin-bottom:8px"> |
| <label style="font-size:11px;color:#666">ํ์ ์๊ฐ (์ด)</label> |
| <input type="number" id="textDuration" value="5" min="1" max="60" step="0.5" style="width:100%;padding:6px;border:1px solid #ddd;border-radius:4px;margin-top:4px"> |
| </div> |
| </div> |
| <div style="display:flex;gap:8px;justify-content:center"> |
| <button class="btn btn-secondary" onclick="closeTextModal()">์ทจ์</button> |
| <button class="btn btn-success" onclick="confirmAddText()">์ถ๊ฐ</button> |
| </div> |
| </div> |
| </div> |
| <div id="hiddenMedia" class="hidden-media"></div> |
| <script> |
| var S={{ |
| media:[], |
| clips:[], |
| sel:null, |
| playing:false, |
| muted:false, |
| time:0, |
| dur:0, |
| zoom:1, |
| pps:80, |
| history:[], |
| animId:null, |
| cancelled:false, |
| els:{{}}, |
| canvas:null, |
| ctx:null, |
| lastClipId:null, |
| ratio:'16:9', |
| fillMode:'fit', |
| ratioSizes:{{ |
| '16:9':{{w:1280,h:720,pw:640,ph:360}}, |
| '9:16':{{w:720,h:1280,pw:203,ph:360}}, |
| '1:1':{{w:1080,h:1080,pw:360,ph:360}}, |
| '4:3':{{w:1280,h:960,pw:480,ph:360}}, |
| '4:5':{{w:1080,h:1350,pw:288,ph:360}} |
| }} |
| }}; |
| |
| function init(){{ |
| S.canvas=document.getElementById('previewCanvas'); |
| S.ctx=S.canvas.getContext('2d'); |
| updateCanvasSize(); |
| drawPlaceholder(); |
| setupCanvasDrag(); |
| }} |
| |
| // ์บ๋ฒ์ค์์ ํ
์คํธ ๋๋๊ทธ ์ค์ |
| function setupCanvasDrag(){{ |
| var canvas=S.canvas; |
| var dragging=null; |
| var dragOffsetX=0,dragOffsetY=0; |
| |
| canvas.addEventListener('mousedown',function(e){{ |
| var rect=canvas.getBoundingClientRect(); |
| var scaleX=canvas.width/rect.width; |
| var scaleY=canvas.height/rect.height; |
| var mx=(e.clientX-rect.left)*scaleX; |
| var my=(e.clientY-rect.top)*scaleY; |
| |
| // ํ์ฌ ์๊ฐ์ ํ
์คํธ ํด๋ฆฝ ์ค ํด๋ฆญ๋ ๊ฒ ์ฐพ๊ธฐ |
| var textClips=getTextClipsAt(S.time); |
| for(var i=textClips.length-1;i>=0;i--){{ |
| var tc=textClips[i]; |
| if(tc._bounds){{ |
| var b=tc._bounds; |
| if(mx>=b.x&&mx<=b.x+b.w&&my>=b.y&&my<=b.y+b.h){{ |
| dragging=tc; |
| selClip(tc.id); |
| var size=S.ratioSizes[S.ratio]; |
| dragOffsetX=mx-size.pw*tc.posX; |
| dragOffsetY=my-size.ph*tc.posY; |
| canvas.style.cursor='move'; |
| e.preventDefault(); |
| return; |
| }} |
| }} |
| }} |
| }}); |
| |
| canvas.addEventListener('mousemove',function(e){{ |
| var rect=canvas.getBoundingClientRect(); |
| var scaleX=canvas.width/rect.width; |
| var scaleY=canvas.height/rect.height; |
| var mx=(e.clientX-rect.left)*scaleX; |
| var my=(e.clientY-rect.top)*scaleY; |
| |
| if(dragging){{ |
| var size=S.ratioSizes[S.ratio]; |
| dragging.posX=Math.max(0.1,Math.min(0.9,(mx-dragOffsetX)/size.pw)); |
| dragging.posY=Math.max(0.1,Math.min(0.9,(my-dragOffsetY)/size.ph)); |
| drawFrame(); |
| }}else{{ |
| // ํธ๋ฒ ์ปค์ ๋ณ๊ฒฝ |
| var textClips=getTextClipsAt(S.time); |
| var onText=false; |
| for(var i=0;i<textClips.length;i++){{ |
| var tc=textClips[i]; |
| if(tc._bounds){{ |
| var b=tc._bounds; |
| if(mx>=b.x&&mx<=b.x+b.w&&my>=b.y&&my<=b.y+b.h){{ |
| onText=true; |
| break; |
| }} |
| }} |
| }} |
| canvas.style.cursor=onText?'move':'default'; |
| }} |
| }}); |
| |
| canvas.addEventListener('mouseup',function(){{ |
| if(dragging){{ |
| save(); |
| dragging=null; |
| canvas.style.cursor='default'; |
| renderProps(); |
| }} |
| }}); |
| |
| canvas.addEventListener('mouseleave',function(){{ |
| if(dragging){{ |
| save(); |
| dragging=null; |
| canvas.style.cursor='default'; |
| }} |
| }}); |
| }} |
| |
| function updateCanvasSize(){{ |
| var size=S.ratioSizes[S.ratio]; |
| S.canvas.width=size.pw; |
| S.canvas.height=size.ph; |
| S.canvas.style.width=size.pw+'px'; |
| S.canvas.style.height=size.ph+'px'; |
| }} |
| |
| function setRatio(r){{ |
| S.ratio=r; |
| updateCanvasSize(); |
| drawFrame(); |
| stat('ํ๋ฉด ๋น์จ: '+r); |
| }} |
| |
| function setFillMode(m){{ |
| S.fillMode=m; |
| drawFrame(); |
| stat('์ฑ์ฐ๊ธฐ ๋ชจ๋: '+(m==='fit'?'๋ง์ถค (์ฌ๋ฐฑ)':m==='fill'?'์ฑ์ฐ๊ธฐ (ํ๋)':'๋ธ๋ฌ ๋ฐฐ๊ฒฝ')); |
| }} |
| |
| function id(){{return Math.random().toString(36).substr(2,9)}} |
| 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')}} |
| function r(n){{return Math.round(n*1000)/1000}} |
| function stat(m){{document.getElementById('status').textContent=m}} |
| function save(){{S.history.push(JSON.stringify(S.clips));if(S.history.length>30)S.history.shift()}} |
| |
| function drawPlaceholder(){{ |
| var size=S.ratioSizes[S.ratio]; |
| var pw=size.pw,ph=size.ph; |
| S.ctx.fillStyle='#000'; |
| S.ctx.fillRect(0,0,pw,ph); |
| S.ctx.fillStyle='#444'; |
| S.ctx.font='14px sans-serif'; |
| S.ctx.textAlign='center'; |
| S.ctx.fillText('ํ์๋ผ์ธ์ ๋ฏธ๋์ด๋ฅผ ์ถ๊ฐํ์ธ์',pw/2,ph/2); |
| }} |
| |
| function addMedia(name,type,url,filePath){{ |
| var m={{id:id(),name:name,type:type,url:url,filePath:filePath||name,dur:type==='image'?5:0,thumb:type==='image'?url:null}}; |
| S.media.push(m); |
| var container=document.getElementById('hiddenMedia'); |
| |
| if(type==='video'){{ |
| var v=document.createElement('video'); |
| v.src=url; |
| v.muted=false; |
| v.playsInline=true; |
| v.preload='auto'; |
| v.crossOrigin='anonymous'; |
| v.setAttribute('playsinline',''); |
| v.setAttribute('webkit-playsinline',''); |
| container.appendChild(v); |
| S.els[m.id]=v; |
| // ๋ฒํผ๋ง ๊ฐํ: ์ ์ฒด ๋ก๋ ๋๊ธฐ |
| v.load(); |
| |
| v.onloadedmetadata=function(){{ |
| m.dur=r(v.duration); |
| renderLib(); |
| v.currentTime=0.5; |
| }}; |
| v.onseeked=function(){{ |
| if(!m.thumb){{ |
| try{{ |
| var c=document.createElement('canvas'); |
| c.width=160;c.height=90; |
| c.getContext('2d').drawImage(v,0,0,160,90); |
| m.thumb=c.toDataURL(); |
| renderLib(); |
| }}catch(e){{}} |
| }} |
| }}; |
| }}else if(type==='audio'){{ |
| var a=document.createElement('audio'); |
| a.src=url; |
| a.preload='auto'; |
| container.appendChild(a); |
| S.els[m.id]=a; |
| a.onloadedmetadata=function(){{m.dur=r(a.duration);renderLib()}}; |
| }}else if(type==='image'){{ |
| var img=new Image(); |
| img.src=url; |
| img.crossOrigin='anonymous'; |
| S.els[m.id]=img; |
| }} |
| renderLib(); |
| stat('๋ฏธ๋์ด ์ถ๊ฐ: '+name); |
| setTimeout(function(){{addClip(m)}},400); |
| }} |
| |
| function renderLib(){{ |
| var g=document.getElementById('mediaGrid'); |
| var h=document.getElementById('hint'); |
| h.style.display=S.media.length?'none':'block'; |
| g.innerHTML=''; |
| S.media.forEach(function(m){{ |
| var d=document.createElement('div'); |
| d.className='media-item'; |
| d.draggable=true; |
| d.ondblclick=function(){{addClip(m)}}; |
| d.ondragstart=function(e){{e.dataTransfer.setData('mid',m.id)}}; |
| var th=m.thumb?'<img src="'+m.thumb+'">':'<div class="media-item-icon">'+(m.type==='video'?'๐ฌ':m.type==='audio'?'๐ต':'๐ผ')+'</div>'; |
| d.innerHTML=th+(m.dur?'<div class="media-item-dur">'+fmt(m.dur)+'</div>':''); |
| g.appendChild(d); |
| }}); |
| }} |
| |
| function trackEnd(tr){{ |
| var end=0; |
| for(var i=0;i<S.clips.length;i++){{ |
| var c=S.clips[i]; |
| if(c.track===tr){{ |
| var e=r(c.start+(c.te-c.ts)); |
| if(e>end)end=e; |
| }} |
| }} |
| return end; |
| }} |
| |
| function addClip(m,at){{ |
| save(); |
| var tr=m.type==='audio'?1:0; |
| var st=at!==undefined?r(at):trackEnd(tr); |
| S.clips.push({{ |
| id:id(), |
| mid:m.id, |
| name:m.name, |
| type:m.type, |
| track:tr, |
| start:st, |
| dur:m.dur, |
| ts:0, |
| te:m.dur, |
| vol:1, |
| filePath:m.filePath |
| }}); |
| renderTL(); |
| updateDur(); |
| stat('ํด๋ฆฝ ์ถ๊ฐ: '+m.name); |
| drawFrame(); |
| }} |
| |
| function renderTL(){{ |
| ['t0','t1','t2'].forEach(function(tid){{document.getElementById(tid).innerHTML=''}}); |
| S.clips.forEach(function(c){{ |
| var tr=document.getElementById('t'+c.track); |
| var el=document.createElement('div'); |
| el.className='clip '+c.type+(S.sel===c.id?' selected':''); |
| var len=r(c.te-c.ts); |
| el.style.left=r(c.start*S.pps*S.zoom)+'px'; |
| el.style.width=Math.max(25,r(len*S.pps*S.zoom))+'px'; |
| el.draggable=true; |
| el.onclick=function(e){{e.stopPropagation();selClip(c.id)}}; |
| el.oncontextmenu=function(e){{e.preventDefault();selClip(c.id);showCtx(e.clientX,e.clientY)}}; |
| el.ondragstart=function(e){{e.dataTransfer.setData('cid',c.id);e.dataTransfer.setData('ox',e.offsetX)}}; |
| var m=S.media.find(function(x){{return x.id===c.mid}}); |
| var th=m&&m.thumb?'<img class="clip-thumb" src="'+m.thumb+'">':''; |
| var clipName=c.type==='text'?('๐ '+c.text.substring(0,8)):c.name; |
| el.innerHTML=th+'<div class="clip-info"><div class="clip-name">'+clipName+'</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>'; |
| el.querySelector('.clip-handle-l').onmousedown=function(e){{e.stopPropagation();startTrim(c.id,'l',e)}}; |
| el.querySelector('.clip-handle-r').onmousedown=function(e){{e.stopPropagation();startTrim(c.id,'r',e)}}; |
| tr.appendChild(el); |
| }}); |
| renderRuler(); |
| setupDrop(); |
| }} |
| |
| function renderRuler(){{ |
| var ru=document.getElementById('ruler'); |
| var w=Math.max(r(S.dur*S.pps*S.zoom)+150,600); |
| ru.style.width=w+'px'; |
| var h='<svg width="100%" height="18" style="position:absolute;left:50px">'; |
| var step=S.zoom<0.7?5:S.zoom<1.5?2:1; |
| for(var i=0;i<=S.dur+10;i+=step){{ |
| var x=r(i*S.pps*S.zoom); |
| h+='<line x1="'+x+'" y1="13" x2="'+x+'" y2="18" stroke="#ccc"/>'; |
| h+='<text x="'+x+'" y="10" fill="#999" font-size="8" text-anchor="middle">'+fmt(i)+'</text>'; |
| }} |
| ru.innerHTML=h+'</svg>'; |
| }} |
| |
| function setupDrop(){{ |
| ['t0','t1','t2'].forEach(function(tid,idx){{ |
| var tr=document.getElementById(tid); |
| tr.ondragover=function(e){{e.preventDefault();tr.classList.add('drop-zone')}}; |
| tr.ondragleave=function(){{tr.classList.remove('drop-zone')}}; |
| tr.ondrop=function(e){{ |
| e.preventDefault(); |
| tr.classList.remove('drop-zone'); |
| var rect=tr.getBoundingClientRect(); |
| var t=r(Math.max(0,(e.clientX-rect.left)/(S.pps*S.zoom))); |
| var mid=e.dataTransfer.getData('mid'); |
| var cid=e.dataTransfer.getData('cid'); |
| var ox=parseFloat(e.dataTransfer.getData('ox')||0); |
| if(mid){{ |
| var m=S.media.find(function(x){{return x.id===mid}}); |
| if(m)addClip(m,t); |
| }}else if(cid){{ |
| save(); |
| var c=S.clips.find(function(x){{return x.id===cid}}); |
| if(c){{ |
| c.start=r(Math.max(0,t-ox/(S.pps*S.zoom))); |
| // ํ
์คํธ๋ ํ
์คํธ ํธ๋์ผ๋ก๋ง, ๊ทธ ์ธ๋ ์์/์ค๋์ค ํธ๋์ผ๋ก |
| if(c.type==='text'){{ |
| c.track=2; |
| }}else if(c.type==='audio'){{ |
| c.track=1; |
| }}else{{ |
| c.track=idx===2?0:idx; |
| }} |
| renderTL(); |
| updateDur(); |
| drawFrame(); |
| }} |
| }} |
| }}; |
| }}); |
| }} |
| |
| var trimData=null; |
| function startTrim(cid,side,e){{ |
| e.preventDefault(); |
| var c=S.clips.find(function(x){{return x.id===cid}}); |
| if(!c)return; |
| save(); |
| trimData={{cid:cid,side:side,sx:e.clientX,ots:c.ts,ote:c.te,ost:c.start}}; |
| document.addEventListener('mousemove',doTrim); |
| document.addEventListener('mouseup',endTrim); |
| }} |
| function doTrim(e){{ |
| if(!trimData)return; |
| var c=S.clips.find(function(x){{return x.id===trimData.cid}}); |
| if(!c)return; |
| var dx=e.clientX-trimData.sx; |
| var dt=r(dx/(S.pps*S.zoom)); |
| if(trimData.side==='l'){{ |
| var newTs=Math.max(0,Math.min(c.te-0.1,trimData.ots+dt)); |
| c.ts=r(newTs); |
| c.start=r(trimData.ost+(newTs-trimData.ots)); |
| }}else{{ |
| c.te=r(Math.max(c.ts+0.1,Math.min(c.dur,trimData.ote+dt))); |
| }} |
| renderTL(); |
| updateDur(); |
| }} |
| function endTrim(){{ |
| trimData=null; |
| document.removeEventListener('mousemove',doTrim); |
| document.removeEventListener('mouseup',endTrim); |
| }} |
| |
| function selClip(cid){{S.sel=cid;renderTL();renderProps()}} |
| |
| function renderProps(){{ |
| var box=document.getElementById('propsBox'); |
| var c=S.clips.find(function(x){{return x.id===S.sel}}); |
| if(!c){{box.innerHTML='<div class="no-sel">ํด๋ฆฝ ์ ํ</div>';return}} |
| var len=r(c.te-c.ts); |
| |
| if(c.type==='text'){{ |
| // ํ
์คํธ ํด๋ฆฝ ์์ฑ |
| box.innerHTML='<div class="prop-group"><div class="prop-label">ํ
์คํธ</div><input class="prop-input" value="'+c.text+'" onchange="setProp(\\'text\\',this.value)"></div>'+ |
| '<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>'+ |
| '<div class="prop-group"><div class="prop-label">๊ธธ์ด: '+fmt(len)+'</div></div>'+ |
| '<div class="prop-group"><div class="prop-label">์์น X ('+Math.round((c.posX||0.5)*100)+'%)</div><input class="prop-input" type="range" min="0.05" max="0.95" step="0.01" value="'+(c.posX||0.5)+'" oninput="setProp(\\'posX\\',parseFloat(this.value))"></div>'+ |
| '<div class="prop-group"><div class="prop-label">์์น Y ('+Math.round((c.posY||0.85)*100)+'%)</div><input class="prop-input" type="range" min="0.05" max="0.95" step="0.01" value="'+(c.posY||0.85)+'" oninput="setProp(\\'posY\\',parseFloat(this.value))"></div>'+ |
| '<div class="prop-group"><div class="prop-label">๊ธ์ ํฌ๊ธฐ</div><select class="prop-input" onchange="setProp(\\'fontSize\\',parseInt(this.value))">'+ |
| '<option value="24"'+(c.fontSize===24?' selected':'')+'>์๊ฒ</option>'+ |
| '<option value="36"'+(c.fontSize===36?' selected':'')+'>๋ณดํต</option>'+ |
| '<option value="48"'+(c.fontSize===48?' selected':'')+'>ํฌ๊ฒ</option>'+ |
| '<option value="64"'+(c.fontSize===64?' selected':'')+'>๋งค์ฐ ํฌ๊ฒ</option>'+ |
| '<option value="96"'+(c.fontSize===96?' selected':'')+'>์ต๋</option>'+ |
| '</select></div>'+ |
| '<div class="prop-group"><div class="prop-label">๊ธ์ ์์</div><input class="prop-input" type="color" value="'+c.fontColor+'" onchange="setProp(\\'fontColor\\',this.value)"></div>'+ |
| '<div class="prop-group"><div class="prop-label">๋ฐฐ๊ฒฝ ์คํ์ผ</div><select class="prop-input" onchange="setProp(\\'bgStyle\\',this.value)">'+ |
| '<option value="none"'+(c.bgStyle==='none'?' selected':'')+'>์์</option>'+ |
| '<option value="solid"'+(c.bgStyle==='solid'?' selected':'')+'>๋จ์</option>'+ |
| '<option value="rounded"'+(c.bgStyle==='rounded'?' selected':'')+'>๋ฅ๊ทผ</option>'+ |
| '</select></div>'+ |
| '<div class="prop-group"><div class="prop-label">๋ฐฐ๊ฒฝ ์์</div><input class="prop-input" type="color" value="'+c.bgColor+'" onchange="setProp(\\'bgColor\\',this.value)"></div>'; |
| }}else{{ |
| // ๊ธฐ์กด ๋ฏธ๋์ด ํด๋ฆฝ ์์ฑ |
| box.innerHTML='<div class="prop-group"><div class="prop-label">์ด๋ฆ</div><input class="prop-input" value="'+c.name+'" onchange="setProp(\\'name\\',this.value)"></div>'+ |
| '<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>'+ |
| '<div class="prop-group"><div class="prop-label">๊ธธ์ด: '+fmt(len)+'</div></div>'+ |
| (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>':''); |
| }} |
| }} |
| |
| function setProp(p,v){{ |
| save(); |
| var c=S.clips.find(function(x){{return x.id===S.sel}}); |
| if(c){{c[p]=p==='start'?r(v):v;renderTL();updateDur();renderProps();drawFrame()}} |
| }} |
| |
| function splitClip(){{ |
| if(!S.sel){{alert('ํด๋ฆฝ์ ์ ํํ์ธ์');return}} |
| var c=S.clips.find(function(x){{return x.id===S.sel}}); |
| if(!c)return; |
| var cEnd=r(c.start+c.te-c.ts); |
| if(S.time<=c.start||S.time>=cEnd){{alert('ํ๋ ์ดํค๋๊ฐ ํด๋ฆฝ ์์ ์์ด์ผ ํฉ๋๋ค');return}} |
| save(); |
| var splitAt=r(S.time-c.start); |
| var c2=JSON.parse(JSON.stringify(c)); |
| c2.id=id(); |
| c2.start=r(S.time); |
| c2.ts=r(c.ts+splitAt); |
| c.te=r(c.ts+splitAt); |
| S.clips.push(c2); |
| renderTL();updateDur();hideCtx();stat('ํด๋ฆฝ ๋ถํ ๋จ'); |
| }} |
| |
| function dupeClip(){{ |
| if(!S.sel)return; |
| var c=S.clips.find(function(x){{return x.id===S.sel}}); |
| if(!c)return; |
| save(); |
| var len=r(c.te-c.ts); |
| var nc=JSON.parse(JSON.stringify(c)); |
| nc.id=id(); |
| nc.start=r(c.start+len); |
| S.clips.push(nc); |
| renderTL();updateDur();hideCtx();stat('ํด๋ฆฝ ๋ณต์ ๋จ'); |
| }} |
| |
| function delClip(){{ |
| if(!S.sel)return; |
| save(); |
| S.clips=S.clips.filter(function(x){{return x.id!==S.sel}}); |
| S.sel=null; |
| renderTL();renderProps();updateDur();hideCtx();stat('ํด๋ฆฝ ์ญ์ ๋จ'); |
| drawFrame(); |
| }} |
| |
| function undo(){{if(S.history.length){{S.clips=JSON.parse(S.history.pop());renderTL();updateDur();stat('์คํ์ทจ์');drawFrame()}}}} |
| |
| function updateDur(){{ |
| var mx=0; |
| for(var i=0;i<S.clips.length;i++){{ |
| var c=S.clips[i]; |
| var e=r(c.start+c.te-c.ts); |
| if(e>mx)mx=e; |
| }} |
| S.dur=mx; |
| document.getElementById('totT').textContent=fmt(mx); |
| }} |
| |
| function togglePlay(){{ |
| S.playing=!S.playing; |
| document.getElementById('playBtn').textContent=S.playing?'โธ':'โถ'; |
| if(S.playing)play();else stop(); |
| }} |
| |
| function play(){{ |
| // ํ์ฌ ๋น์ฃผ์ผ ํด๋ฆฝ ์ฐพ๊ธฐ |
| var vc=getClipAt(S.time,'visual'); |
| if(vc&&vc.type==='video'){{ |
| var el=S.els[vc.mid]; |
| if(el){{ |
| el.currentTime=S.time-vc.start+vc.ts; |
| el.play().catch(function(){{}}); |
| }} |
| }} |
| // ์ค๋์ค ํด๋ฆฝ๋ค ์์ |
| S.clips.forEach(function(c){{ |
| if(c.type==='audio'){{ |
| var cEnd=c.start+(c.te-c.ts); |
| if(S.time>=c.start&&S.time<cEnd){{ |
| var el=S.els[c.mid]; |
| if(el){{ |
| el.currentTime=S.time-c.start+c.ts; |
| el.volume=S.muted?0:c.vol; |
| el.play().catch(function(){{}}); |
| }} |
| }} |
| }} |
| }}); |
| function loop(){{ |
| if(!S.playing)return; |
| var vc=getClipAt(S.time,'visual'); |
| if(vc&&vc.type==='video'){{ |
| var el=S.els[vc.mid]; |
| if(el&&!el.paused){{ |
| S.time=vc.start+(el.currentTime-vc.ts); |
| }}else{{ |
| S.time+=1/60; |
| }} |
| }}else{{ |
| S.time+=1/60; |
| }} |
| if(S.time>=S.dur){{ |
| S.time=0; |
| if(S.dur===0){{S.playing=false;document.getElementById('playBtn').textContent='โถ';return}} |
| }} |
| updateHead(); |
| drawFrameSmooth(); |
| S.animId=requestAnimationFrame(loop); |
| }} |
| S.animId=requestAnimationFrame(loop); |
| }} |
| |
| function drawFrameSmooth(){{ |
| var t=S.time; |
| var vc=getClipAt(t,'visual'); |
| var size=S.ratioSizes[S.ratio]; |
| var pw=size.pw,ph=size.ph; |
| |
| S.ctx.fillStyle='#000'; |
| S.ctx.fillRect(0,0,pw,ph); |
| |
| if(vc){{ |
| var el=S.els[vc.mid]; |
| if(el){{ |
| if(vc.type==='video'){{ |
| el.volume=S.muted?0:vc.vol; |
| }} |
| try{{ |
| var sw=el.videoWidth||el.naturalWidth||el.width||pw; |
| var sh=el.videoHeight||el.naturalHeight||el.height||ph; |
| drawWithMode(S.ctx,el,sw,sh,pw,ph,S.fillMode); |
| }}catch(e){{}} |
| }} |
| }}else if(S.clips.length===0){{ |
| S.ctx.fillStyle='#444'; |
| S.ctx.font='14px sans-serif'; |
| S.ctx.textAlign='center'; |
| S.ctx.fillText('Add media to timeline',pw/2,ph/2); |
| }} |
| |
| // ์ค๋์ค ๋๊ธฐํ (๊ฐ๋ฒผ์ด ์ฒดํฌ๋ง) |
| S.clips.forEach(function(c){{ |
| if(c.type!=='audio')return; |
| var cEnd=c.start+(c.te-c.ts); |
| var el=S.els[c.mid]; |
| if(!el)return; |
| if(t>=c.start&&t<cEnd){{ |
| el.volume=S.muted?0:c.vol; |
| if(el.paused)el.play().catch(function(){{}}); |
| }}else{{ |
| if(!el.paused)el.pause(); |
| }} |
| }}); |
| |
| if(!vc&&S.clips.length>0){{ |
| var hasText=getTextClipsAt(t).length>0; |
| if(!hasText){{ |
| S.ctx.fillStyle='#333'; |
| S.ctx.font='12px sans-serif'; |
| S.ctx.textAlign='center'; |
| S.ctx.fillText('No media at playhead',pw/2,ph/2); |
| }} |
| }} |
| |
| var textClips=getTextClipsAt(t); |
| textClips.forEach(function(tc){{ |
| drawText(S.ctx,tc,pw,ph); |
| }}); |
| }} |
| |
| function stop(){{ |
| if(S.animId){{cancelAnimationFrame(S.animId);S.animId=null}} |
| Object.keys(S.els).forEach(function(k){{ |
| var el=S.els[k]; |
| if(el&&el.pause){{ |
| el.pause(); |
| }} |
| }}); |
| drawFrame(); |
| }} |
| |
| function seek(t){{ |
| S.time=Math.max(0,Math.min(S.dur||0,t)); |
| // ๋น๋์ค/์ค๋์ค ์์น๋ ๋๊ธฐํ |
| S.clips.forEach(function(c){{ |
| var el=S.els[c.mid]; |
| if(!el)return; |
| var cEnd=c.start+(c.te-c.ts); |
| if(t>=c.start&&t<cEnd){{ |
| el.currentTime=t-c.start+c.ts; |
| }} |
| }}); |
| updateHead(); |
| drawFrame(); |
| }} |
| |
| function updateHead(){{ |
| document.getElementById('playhead').style.left=(50+S.time*S.pps*S.zoom)+'px'; |
| document.getElementById('curT').textContent=fmt(S.time); |
| }} |
| |
| function getClipAt(t,type){{ |
| var sorted=S.clips.filter(function(c){{ |
| if(type==='visual')return c.type==='video'||c.type==='image'; |
| if(type==='audio')return c.type==='audio'; |
| return true; |
| }}).sort(function(a,b){{return a.start-b.start}}); |
| for(var i=0;i<sorted.length;i++){{ |
| var c=sorted[i]; |
| var cEnd=c.start+(c.te-c.ts); |
| if(t>=c.start&&t<cEnd)return c; |
| }} |
| return null; |
| }} |
| |
| function drawFrame(){{ |
| var t=S.time; |
| var vc=getClipAt(t,'visual'); |
| var size=S.ratioSizes[S.ratio]; |
| var pw=size.pw,ph=size.ph; |
| |
| S.ctx.fillStyle='#000'; |
| S.ctx.fillRect(0,0,pw,ph); |
| |
| if(vc){{ |
| var el=S.els[vc.mid]; |
| if(el){{ |
| |
| if(vc.type==='video'){{ |
| var clipT=t-vc.start+vc.ts; |
| if(Math.abs(el.currentTime-clipT)>0.3){{ |
| el.currentTime=clipT; |
| }} |
| |
| |
| if(S.playing&&el.paused)el.play().catch(function(){{}}); |
| if(!S.playing&&!el.paused)el.pause(); |
| el.volume=S.muted?0:vc.vol; |
| }} |
| try{{ |
| var sw=el.videoWidth||el.naturalWidth||el.width||pw; |
| var sh=el.videoHeight||el.naturalHeight||el.height||ph; |
| drawWithMode(S.ctx,el,sw,sh,pw,ph,S.fillMode); |
| }}catch(e){{}} |
| }} |
| }}else if(S.clips.length===0){{ |
| S.ctx.fillStyle='#444'; |
| S.ctx.font='14px sans-serif'; |
| S.ctx.textAlign='center'; |
| S.ctx.fillText('ํ์๋ผ์ธ์ ๋ฏธ๋์ด๋ฅผ ์ถ๊ฐํ์ธ์',pw/2,ph/2); |
| }} |
| var audioClips=S.clips.filter(function(c){{ |
| if(c.type!=='audio')return false; |
| var cEnd=c.start+(c.te-c.ts); |
| return t>=c.start&&t<cEnd; |
| }}); |
| audioClips.forEach(function(ac){{ |
| var el=S.els[ac.mid]; |
| if(el){{ |
| var clipT=t-ac.start+ac.ts; |
| if(Math.abs(el.currentTime-clipT)>0.5)el.currentTime=clipT; |
| el.volume=S.muted?0:ac.vol; |
| if(S.playing&&el.paused)el.play().catch(function(){{}}); |
| if(!S.playing&&!el.paused)el.pause(); |
| }} |
| }}); |
| S.clips.forEach(function(c){{ |
| if(c.type!=='audio')return; |
| var cEnd=c.start+(c.te-c.ts); |
| if(t<c.start||t>=cEnd){{ |
| var el=S.els[c.mid]; |
| if(el&&!el.paused)el.pause(); |
| }} |
| }}); |
| if(!vc&&!audioClips.length&&S.clips.length>0){{ |
| var hasText=getTextClipsAt(t).length>0; |
| if(!hasText){{ |
| S.ctx.fillStyle='#333'; |
| S.ctx.font='12px sans-serif'; |
| S.ctx.textAlign='center'; |
| S.ctx.fillText('์ฌ์ ์์น์ ๋ฏธ๋์ด๊ฐ ์์ต๋๋ค',pw/2,ph/2); |
| }} |
| }} |
| |
| // ํ
์คํธ ์ค๋ฒ๋ ์ด ๋ ๋๋ง |
| var textClips=getTextClipsAt(t); |
| textClips.forEach(function(tc){{ |
| drawText(S.ctx,tc,pw,ph); |
| }}); |
| }} |
| |
| function drawWithMode(ctx,el,sw,sh,dw,dh,mode){{ |
| if(mode==='fill'){{ |
| // ์ฑ์ฐ๊ธฐ: ์บ๋ฒ์ค๋ฅผ ๊ฝ ์ฑ์ฐ๋๋ก ํ๋, ๋์น๋ ๋ถ๋ถ ์๋ฆผ |
| var scale=Math.max(dw/sw,dh/sh); |
| var nw=sw*scale,nh=sh*scale; |
| var ox=(dw-nw)/2,oy=(dh-nh)/2; |
| ctx.drawImage(el,ox,oy,nw,nh); |
| }}else if(mode==='blur'){{ |
| // ํ๋ ์ ๋จผ์ ์บก์ฒ |
| var frameCanvas=document.createElement('canvas'); |
| frameCanvas.width=sw;frameCanvas.height=sh; |
| var frameCtx=frameCanvas.getContext('2d'); |
| frameCtx.drawImage(el,0,0,sw,sh); |
| |
| // ๋ธ๋ฌ์ฉ ์์ ์บ๋ฒ์ค |
| var blurCanvas=document.createElement('canvas'); |
| blurCanvas.width=16;blurCanvas.height=16; |
| var blurCtx=blurCanvas.getContext('2d'); |
| blurCtx.drawImage(frameCanvas,0,0,16,16); |
| |
| // ๋ฐฐ๊ฒฝ |
| ctx.imageSmoothingEnabled=true; |
| ctx.imageSmoothingQuality='high'; |
| var scale=Math.max(dw/sw,dh/sh)*1.1; |
| var nw=sw*scale,nh=sh*scale; |
| var ox=(dw-nw)/2,oy=(dh-nh)/2; |
| ctx.drawImage(blurCanvas,ox,oy,nw,nh); |
| |
| // ์ด๋ก๊ฒ |
| ctx.fillStyle='rgba(0,0,0,0.4)'; |
| ctx.fillRect(0,0,dw,dh); |
| |
| // ์๋ณธ (์บก์ฒ๋ ํ๋ ์) |
| var fitScale=Math.min(dw/sw,dh/sh); |
| var fw=sw*fitScale,fh=sh*fitScale; |
| var fx=(dw-fw)/2,fy=(dh-fh)/2; |
| ctx.drawImage(frameCanvas,fx,fy,fw,fh); |
| }}else{{ |
| // ๋ง์ถค (fit): ์๋ณธ ๋น์จ ์ ์ง, ์ฌ๋ฐฑ |
| var scale=Math.min(dw/sw,dh/sh); |
| var nw=sw*scale,nh=sh*scale; |
| var ox=(dw-nw)/2,oy=(dh-nh)/2; |
| ctx.drawImage(el,ox,oy,nw,nh); |
| }} |
| }} |
| |
| function toggleMute(){{S.muted=!S.muted;document.getElementById('muteBtn').textContent=S.muted?'๐':'๐'}} |
| |
| // ํ
์คํธ ํด๋ฆฝ ๊ด๋ จ ํจ์๋ค |
| function addTextClip(){{ |
| document.getElementById('textModal').style.display='flex'; |
| document.getElementById('textInput').value=''; |
| document.getElementById('textInput').focus(); |
| }} |
| |
| function closeTextModal(){{ |
| document.getElementById('textModal').style.display='none'; |
| }} |
| |
| function toggleBgColor(){{ |
| var style=document.getElementById('textBgStyle').value; |
| document.getElementById('bgColorBox').style.opacity=style==='none'?'0.5':'1'; |
| }} |
| |
| function confirmAddText(){{ |
| var text=document.getElementById('textInput').value.trim(); |
| if(!text){{alert('ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์');return}} |
| save(); |
| var duration=parseFloat(document.getElementById('textDuration').value)||5; |
| S.clips.push({{ |
| id:id(), |
| type:'text', |
| track:2, |
| start:S.time, |
| dur:duration, |
| ts:0, |
| te:duration, |
| text:text, |
| fontSize:parseInt(document.getElementById('textSize').value), |
| fontColor:document.getElementById('textColor').value, |
| bgStyle:document.getElementById('textBgStyle').value, |
| bgColor:document.getElementById('textBgColor').value, |
| posX:0.5, // 0~1 ๋น์จ (๊ฐ์ด๋ฐ) |
| posY:0.85 // 0~1 ๋น์จ (ํ๋จ) |
| }}); |
| closeTextModal(); |
| renderTL(); |
| updateDur(); |
| drawFrame(); |
| stat('ํ
์คํธ ์ถ๊ฐ: '+text); |
| }} |
| |
| function getTextClipsAt(t){{ |
| return S.clips.filter(function(c){{ |
| if(c.type!=='text')return false; |
| var cEnd=c.start+(c.te-c.ts); |
| return t>=c.start&&t<cEnd; |
| }}); |
| }} |
| |
| function drawText(ctx,clip,dw,dh){{ |
| var text=clip.text; |
| var fontSize=clip.fontSize||48; |
| var fontColor=clip.fontColor||'#ffffff'; |
| var bgStyle=clip.bgStyle||'none'; |
| var bgColor=clip.bgColor||'#000000'; |
| var posX=clip.posX!==undefined?clip.posX:0.5; |
| var posY=clip.posY!==undefined?clip.posY:0.85; |
| |
| ctx.font='bold '+fontSize+'px -apple-system,BlinkMacSystemFont,sans-serif'; |
| ctx.textAlign='center'; |
| ctx.textBaseline='middle'; |
| |
| var metrics=ctx.measureText(text); |
| var textW=metrics.width; |
| var textH=fontSize; |
| var x=dw*posX; |
| var y=dh*posY; |
| |
| // ํ
์คํธ ์์ญ ์ ์ฅ (ํด๋ฆญ ๊ฐ์ง์ฉ) |
| clip._bounds={{ |
| x:x-textW/2-fontSize*0.3, |
| y:y-textH/2-fontSize*0.15, |
| w:textW+fontSize*0.6, |
| h:textH+fontSize*0.3 |
| }}; |
| |
| if(bgStyle!=='none'){{ |
| var padding=fontSize*0.3; |
| var bgX=x-textW/2-padding; |
| var bgY=y-textH/2-padding/2; |
| var bgW=textW+padding*2; |
| var bgH=textH+padding; |
| |
| ctx.fillStyle=bgColor+'cc'; |
| if(bgStyle==='rounded'){{ |
| ctx.beginPath(); |
| ctx.roundRect(bgX,bgY,bgW,bgH,10); |
| ctx.fill(); |
| }}else{{ |
| ctx.fillRect(bgX,bgY,bgW,bgH); |
| }} |
| }} |
| |
| // ํ
์คํธ ๊ทธ๋ฆผ์ |
| ctx.fillStyle='rgba(0,0,0,0.5)'; |
| ctx.fillText(text,x+2,y+2); |
| |
| // ํ
์คํธ |
| ctx.fillStyle=fontColor; |
| ctx.fillText(text,x,y); |
| |
| // ์ ํ๋ ํ
์คํธ๋ฉด ํ
๋๋ฆฌ ํ์ |
| if(S.sel===clip.id){{ |
| ctx.strokeStyle='#6366f1'; |
| ctx.lineWidth=2; |
| ctx.setLineDash([5,3]); |
| ctx.strokeRect(clip._bounds.x,clip._bounds.y,clip._bounds.w,clip._bounds.h); |
| ctx.setLineDash([]); |
| }} |
| }} |
| function setZoom(v){{S.zoom=parseFloat(v);renderTL();updateHead()}} |
| function tlClick(e){{ |
| if(e.target.closest('.clip'))return; |
| var rect=document.getElementById('tlBox').getBoundingClientRect(); |
| var scrollL=document.getElementById('tlBox').scrollLeft; |
| S.time=Math.max(0,Math.min(S.dur||0,(e.clientX-rect.left-50+scrollL)/(S.pps*S.zoom))); |
| updateHead(); |
| drawFrame(); |
| }} |
| |
| function showCtx(x,y){{var m=document.getElementById('ctx');m.style.display='block';m.style.left=x+'px';m.style.top=y+'px'}} |
| function hideCtx(){{document.getElementById('ctx').style.display='none'}} |
| document.addEventListener('click',function(e){{if(!e.target.closest('.ctx-menu'))hideCtx()}}); |
| |
| function exportVideo(){{ |
| if(!S.clips.length){{alert('ํด๋ฆฝ์ ์ถ๊ฐํ์ธ์');return}} |
| S.cancelled=false; |
| document.getElementById('exportModal').style.display='flex'; |
| document.getElementById('exportBar').style.width='0%'; |
| document.getElementById('exportMsg').textContent='๋
นํ ์ค๋น ์ค...'; |
| doExport(); |
| }} |
| |
| // ============================================ |
| // ํต์ฌ ์์ : ๋๋ธ ๋ฒํผ๋ง์ผ๋ก ๋ธ๋ฌ ๋ฒ์ฉ์ ๋ฐฉ์ง |
| // ============================================ |
| async function doExport(){{ |
| var size=S.ratioSizes[S.ratio]; |
| var exportW=size.w,exportH=size.h; |
| |
| // โ
๋ฉ์ธ ์บ๋ฒ์ค (MediaRecorder ์ฐ๊ฒฐ) - ์์ฑ๋ ํ๋ ์๋ง ๋ฐ์ |
| var mainCanvas=document.createElement('canvas'); |
| mainCanvas.width=exportW;mainCanvas.height=exportH; |
| var mainCtx=mainCanvas.getContext('2d'); |
| |
| // โ
์คํ์คํฌ๋ฆฐ ์บ๋ฒ์ค (๋ ๋๋ง์ฉ) - ์ฌ๊ธฐ์ ๋ชจ๋ ์์
ํ ํ๋ฒ์ ๋ณต์ฌ |
| var offCanvas=document.createElement('canvas'); |
| offCanvas.width=exportW;offCanvas.height=exportH; |
| var offCtx=offCanvas.getContext('2d'); |
| |
| // ์ด๊ธฐ ๊ฒ์ ํ๋ฉด |
| mainCtx.fillStyle='#000'; |
| mainCtx.fillRect(0,0,exportW,exportH); |
| |
| var stream=mainCanvas.captureStream(30); |
| |
| // ๊ณ ํ์ง ์ค์ |
| var opts={{mimeType:'video/webm;codecs=vp9',videoBitsPerSecond:8000000}}; |
| if(!MediaRecorder.isTypeSupported(opts.mimeType)){{ |
| opts={{mimeType:'video/webm;codecs=vp8',videoBitsPerSecond:8000000}}; |
| }} |
| if(!MediaRecorder.isTypeSupported(opts.mimeType)){{ |
| opts={{mimeType:'video/webm',videoBitsPerSecond:5000000}}; |
| }} |
| |
| var rec=new MediaRecorder(stream,opts); |
| var chunks=[]; |
| rec.ondataavailable=function(e){{if(e.data.size>0)chunks.push(e.data)}}; |
| |
| document.getElementById('exportMsg').textContent='๋
นํ ์ค... ('+S.ratio+')'; |
| rec.start(100); |
| |
| var dur=S.dur; |
| var fillMode=S.fillMode; |
| |
| // ๋ธ๋ฌ์ฉ ์บ๋ฒ์ค (์ฌ์ฌ์ฉ) |
| var frameCanvas=document.createElement('canvas'); |
| var frameCtx=frameCanvas.getContext('2d'); |
| var blurCanvas=document.createElement('canvas'); |
| blurCanvas.width=16;blurCanvas.height=16; |
| var blurCtx=blurCanvas.getContext('2d'); |
| |
| // ๋ชจ๋ ๋น๋์ค๋ฅผ ์ฒ์์ผ๋ก ๋๋๋ฆฌ๊ณ ์ฌ์ |
| videoClips.forEach(function(vc){{ |
| var el=S.els[vc.mid]; |
| if(el){{ |
| el.currentTime=vc.ts; |
| el.muted=true; // ๋ด๋ณด๋ด๊ธฐ ์ค์๋ง ์์๊ฑฐ (์ ์) |
| }} |
| }}); |
| |
| |
| // ์ ์ ๋๊ธฐ ํ ์ฌ์ ์์ |
| await new Promise(function(r){{setTimeout(r,200)}}); |
| |
| videoClips.forEach(function(vc){{ |
| var el=S.els[vc.mid]; |
| if(el)el.play().catch(function(){{}}); |
| }}); |
| |
| var startTime=performance.now(); |
| |
| // ์ค์๊ฐ ๋ ๋๋ง ๋ฃจํ |
| await new Promise(function(resolve){{ |
| function render(){{ |
| if(S.cancelled){{rec.stop();resolve();return}} |
| |
| var elapsed=(performance.now()-startTime)/1000; |
| if(elapsed>=dur){{ |
| rec.stop(); |
| setTimeout(resolve,200); |
| return; |
| }} |
| |
| // ์งํ๋ฅ ์
๋ฐ์ดํธ |
| document.getElementById('exportBar').style.width=(elapsed/dur*100)+'%'; |
| document.getElementById('exportMsg').textContent='๋
นํ ์ค... '+Math.round(elapsed/dur*100)+'%'; |
| |
| // โ
์คํ์คํฌ๋ฆฐ ์บ๋ฒ์ค์ ์์ ํ ๋ ๋๋ง โ
|
| offCtx.fillStyle='#000'; |
| offCtx.fillRect(0,0,exportW,exportH); |
| |
| var vc=getClipAt(elapsed,'visual'); |
| if(vc){{ |
| var el=S.els[vc.mid]; |
| if(el){{ |
| // ๋น๋์ค ์๊ฐ ๋๊ธฐํ (์ฐจ์ด๊ฐ ํฌ๋ฉด ๋ณด์ ) |
| if(vc.type==='video'){{ |
| var targetT=elapsed-vc.start+vc.ts; |
| if(Math.abs(el.currentTime-targetT)>0.5){{ |
| el.currentTime=targetT; |
| }} |
| }} |
| |
| try{{ |
| var sw=el.videoWidth||el.naturalWidth||el.width||exportW; |
| var sh=el.videoHeight||el.naturalHeight||el.height||exportH; |
| |
| if(fillMode==='blur'){{ |
| // โ
๋ธ๋ฌ: ์คํ์คํฌ๋ฆฐ์์ ์์ ํ ์ฒ๋ฆฌ ํ ๋ฉ์ธ์ ๋ณต์ฌ โ
|
| frameCanvas.width=sw; |
| frameCanvas.height=sh; |
| frameCtx.drawImage(el,0,0,sw,sh); |
| blurCtx.drawImage(frameCanvas,0,0,16,16); |
| |
| offCtx.imageSmoothingEnabled=true; |
| offCtx.imageSmoothingQuality='high'; |
| var scale=Math.max(exportW/sw,exportH/sh)*1.1; |
| var nw=sw*scale,nh=sh*scale; |
| offCtx.drawImage(blurCanvas,(exportW-nw)/2,(exportH-nh)/2,nw,nh); |
| offCtx.fillStyle='rgba(0,0,0,0.4)'; |
| offCtx.fillRect(0,0,exportW,exportH); |
| var fitScale=Math.min(exportW/sw,exportH/sh); |
| var fw=sw*fitScale,fh=sh*fitScale; |
| offCtx.drawImage(frameCanvas,(exportW-fw)/2,(exportH-fh)/2,fw,fh); |
| }}else if(fillMode==='fill'){{ |
| var scale=Math.max(exportW/sw,exportH/sh); |
| var nw=sw*scale,nh=sh*scale; |
| offCtx.drawImage(el,(exportW-nw)/2,(exportH-nh)/2,nw,nh); |
| }}else{{ |
| // fit |
| var scale=Math.min(exportW/sw,exportH/sh); |
| var nw=sw*scale,nh=sh*scale; |
| offCtx.drawImage(el,(exportW-nw)/2,(exportH-nh)/2,nw,nh); |
| }} |
| }}catch(e){{console.error('Draw error:',e)}} |
| }} |
| }} |
| |
| // ํ
์คํธ ์ค๋ฒ๋ ์ด ๋ ๋๋ง (์คํ์คํฌ๋ฆฐ์) |
| var textClips=getTextClipsAt(elapsed); |
| textClips.forEach(function(tc){{ |
| drawTextExport(offCtx,tc,exportW,exportH); |
| }}); |
| |
| // โ
์คํ์คํฌ๋ฆฐ โ ๋ฉ์ธ ์บ๋ฒ์ค๋ก ํ๋ฒ์ ๋ณต์ฌ (atomic) โ
|
| mainCtx.drawImage(offCanvas,0,0); |
| |
| requestAnimationFrame(render); |
| }} |
| requestAnimationFrame(render); |
| }}); |
| |
| // ์ ๋ฆฌ |
| Object.keys(S.els).forEach(function(k){{ |
| var el=S.els[k]; |
| if(el&&el.pause)el.pause(); |
| }}); |
| |
| if(S.cancelled)return; |
| |
| var webmBlob=new Blob(chunks,{{type:'video/webm'}}); |
| if(webmBlob.size<1000){{document.getElementById('exportMsg').textContent='๋
นํ ์คํจ';return}} |
| |
| document.getElementById('exportBar').style.width='100%'; |
| document.getElementById('exportMsg').textContent='์๋ฃ! ('+Math.round(webmBlob.size/1024/1024*10)/10+'MB, '+S.ratio+') - ์๋์์ ๋ค์ด๋ก๋'; |
| |
| var downloadDiv=document.createElement('div'); |
| downloadDiv.style.marginTop='10px'; |
| var webmLink=document.createElement('a'); |
| webmLink.href=URL.createObjectURL(webmBlob); |
| webmLink.download='video_'+S.ratio.replace(':','x')+'_'+Date.now()+'.webm'; |
| webmLink.className='btn btn-success'; |
| webmLink.textContent='๐ฅ WebM ๋ค์ด๋ก๋'; |
| webmLink.style.marginRight='5px'; |
| downloadDiv.appendChild(webmLink); |
| |
| var copyBtn=document.createElement('button'); |
| copyBtn.className='btn btn-secondary'; |
| copyBtn.textContent='๐ MP4๋ณํ์ฉ ๋ณต์ฌ'; |
| copyBtn.onclick=async function(){{ |
| document.getElementById('exportMsg').textContent='๋ฐ์ดํฐ ์ค๋น ์ค...'; |
| var reader=new FileReader(); |
| reader.onload=function(){{ |
| var base64=reader.result.split(',')[1]; |
| navigator.clipboard.writeText(base64).then(function(){{ |
| document.getElementById('exportMsg').textContent='๋ณต์ฌ ์๋ฃ! ์ ์
๋ ฅ๋์ ๋ถ์ฌ๋ฃ๊ธฐ'; |
| alert('๋ณต์ฌ ์๋ฃ!\\n\\nGradio์ "WebM ๋ฐ์ดํฐ" ์
๋ ฅ๋์ ๋ถ์ฌ๋ฃ๊ธฐ(Ctrl+V) ํ\\n"MP4 ๋ณํ" ๋ฒํผ์ ํด๋ฆญํ์ธ์.'); |
| }}).catch(function(){{ |
| prompt('์๋ ๋ฐ์ดํฐ๋ฅผ ๋ณต์ฌํ์ธ์:', base64.substring(0,100)+'...'); |
| }}); |
| }}; |
| reader.readAsDataURL(webmBlob); |
| }}; |
| downloadDiv.appendChild(copyBtn); |
| document.querySelector('.modal-box').appendChild(downloadDiv); |
| }} |
| |
| // ๋ด๋ณด๋ด๊ธฐ์ฉ ํ
์คํธ ๋ ๋๋ง |
| function drawTextExport(ctx,clip,dw,dh){{ |
| var text=clip.text; |
| var fontSize=Math.round(clip.fontSize*(dw/640))||72; |
| var fontColor=clip.fontColor||'#ffffff'; |
| var bgStyle=clip.bgStyle||'none'; |
| var bgColor=clip.bgColor||'#000000'; |
| var posX=clip.posX!==undefined?clip.posX:0.5; |
| var posY=clip.posY!==undefined?clip.posY:0.85; |
| |
| ctx.font='bold '+fontSize+'px -apple-system,BlinkMacSystemFont,sans-serif'; |
| ctx.textAlign='center'; |
| ctx.textBaseline='middle'; |
| |
| var metrics=ctx.measureText(text); |
| var textW=metrics.width; |
| var textH=fontSize; |
| var x=dw*posX; |
| var y=dh*posY; |
| |
| if(bgStyle!=='none'){{ |
| var padding=fontSize*0.3; |
| var bgX=x-textW/2-padding; |
| var bgY=y-textH/2-padding/2; |
| var bgW=textW+padding*2; |
| var bgH=textH+padding; |
| |
| ctx.fillStyle=bgColor+'cc'; |
| if(bgStyle==='rounded'){{ |
| ctx.beginPath(); |
| ctx.roundRect(bgX,bgY,bgW,bgH,15); |
| ctx.fill(); |
| }}else{{ |
| ctx.fillRect(bgX,bgY,bgW,bgH); |
| }} |
| }} |
| |
| ctx.fillStyle='rgba(0,0,0,0.5)'; |
| ctx.fillText(text,x+3,y+3); |
| |
| ctx.fillStyle=fontColor; |
| ctx.fillText(text,x,y); |
| }} |
| |
| function cancelExport(){{S.cancelled=true;document.getElementById('exportModal').style.display='none'}} |
| |
| document.addEventListener('keydown',function(e){{ |
| if(e.target.tagName==='INPUT')return; |
| if(e.code==='Space'){{e.preventDefault();togglePlay()}} |
| else if(e.code==='Delete'){{e.preventDefault();delClip()}} |
| else if(e.code==='ArrowLeft'){{seek(S.time-0.1)}} |
| else if(e.code==='ArrowRight'){{seek(S.time+0.1)}} |
| }}); |
| |
| init(); |
| renderTL(); |
| stat('์ค๋น๋จ'); |
| var initData={media_data}; |
| if(initData&&initData.length)initData.forEach(function(m){{addMedia(m.name,m.type,m.dataUrl,m.filePath)}}); |
| </script> |
| </body> |
| </html>''' |
|
|
| def process_file(files): |
| """ํ์ผ ์ฒ๋ฆฌ ๋ฐ ์๋ฒ์ ์ ์ฅ""" |
| global uploaded_files |
| if not files: |
| return [] |
| results = [] |
| file_list = files if isinstance(files, list) else [files] |
| for f in file_list: |
| if not f: |
| continue |
| path = f.name if hasattr(f, 'name') else f |
| name = os.path.basename(path) |
| ext = name.lower().split('.')[-1] |
| if ext in ['mp4', 'webm', 'mov', 'avi', 'mkv']: |
| t, m = 'video', f'video/{ext}' |
| elif ext in ['jpg', 'jpeg', 'png', 'gif', 'webp']: |
| t, m = 'image', f'image/{ext}' |
| elif ext in ['mp3', 'wav', 'ogg', 'm4a', 'aac']: |
| t, m = 'audio', f'audio/{ext}' |
| else: |
| continue |
| |
| |
| dst_path = os.path.join(UPLOAD_DIR, f"{int(time.time()*1000)}_{name}") |
| shutil.copy(path, dst_path) |
| uploaded_files[name] = dst_path |
| |
| with open(path, 'rb') as fp: |
| d = base64.b64encode(fp.read()).decode() |
| results.append({'name': name, 'type': t, 'dataUrl': f'data:{m};base64,{d}', 'filePath': name}) |
| return results |
|
|
| def make_iframe(data): |
| j = json.dumps(data, ensure_ascii=False) |
| h = get_editor_html(j).replace("'", "'") |
| return f"<iframe srcdoc='{h}' style='width:100%;height:750px;border:none;border-radius:10px'></iframe>" |
|
|
| def export_mp4(export_json): |
| """์๋ฒ ์ฌ์ด๋ MP4 ๋ด๋ณด๋ด๊ธฐ""" |
| global uploaded_files |
| |
| if not export_json or len(export_json) < 10: |
| return None |
| |
| try: |
| data = json.loads(export_json) |
| clips = data.get('clips', []) |
| |
| if not clips: |
| return None |
| |
| video_clips = [c for c in clips if c['type'] in ['video', 'image']] |
| if not video_clips: |
| return None |
| |
| temp_dir = tempfile.mkdtemp() |
| output_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4') |
| |
| |
| if len(video_clips) == 1: |
| clip = video_clips[0] |
| file_path = uploaded_files.get(clip['filePath']) |
| |
| if not file_path or not os.path.exists(file_path): |
| return None |
| |
| duration = clip['te'] - clip['ts'] |
| |
| if clip['type'] == 'image': |
| 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] |
| else: |
| 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] |
| |
| subprocess.run(cmd, capture_output=True, timeout=300) |
| |
| if os.path.exists(output_path) and os.path.getsize(output_path) > 0: |
| return output_path |
| return None |
| |
| |
| temp_files = [] |
| concat_file = os.path.join(temp_dir, 'concat.txt') |
| |
| for i, clip in enumerate(sorted(video_clips, key=lambda x: x['start'])): |
| file_path = uploaded_files.get(clip['filePath']) |
| if not file_path or not os.path.exists(file_path): |
| continue |
| |
| temp_out = os.path.join(temp_dir, f'temp_{i}.mp4') |
| duration = clip['te'] - clip['ts'] |
| |
| if clip['type'] == 'image': |
| 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] |
| else: |
| 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] |
| |
| subprocess.run(cmd, capture_output=True, timeout=120) |
| if os.path.exists(temp_out): |
| temp_files.append(temp_out) |
| |
| if not temp_files: |
| return None |
| |
| with open(concat_file, 'w') as f: |
| for tf in temp_files: |
| f.write(f"file '{tf}'\n") |
| |
| cmd = ['ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', concat_file, '-c:v', 'libx264', '-preset', 'fast', '-c:a', 'aac', '-movflags', '+faststart', output_path] |
| subprocess.run(cmd, capture_output=True, timeout=300) |
| |
| for tf in temp_files: |
| try: os.remove(tf) |
| except: pass |
| |
| if os.path.exists(output_path) and os.path.getsize(output_path) > 0: |
| return output_path |
| return None |
| |
| except Exception as e: |
| print(f"[Export] Error: {e}") |
| return None |
|
|
| def convert_webm_to_mp4(webm_base64): |
| """WebM base64 ๋ฐ์ดํฐ๋ฅผ MP4๋ก ๋ณํ""" |
| if not webm_base64 or len(webm_base64) < 100: |
| return None |
| |
| try: |
| |
| webm_data = base64.b64decode(webm_base64) |
| |
| temp_dir = tempfile.mkdtemp() |
| webm_path = os.path.join(temp_dir, 'input.webm') |
| mp4_path = os.path.join(temp_dir, f'output_{int(time.time())}.mp4') |
| |
| |
| with open(webm_path, 'wb') as f: |
| f.write(webm_data) |
| |
| |
| cmd = [ |
| 'ffmpeg', '-y', |
| '-i', webm_path, |
| '-c:v', 'libx264', |
| '-preset', 'fast', |
| '-crf', '23', |
| '-c:a', 'aac', |
| '-b:a', '128k', |
| '-movflags', '+faststart', |
| mp4_path |
| ] |
| |
| result = subprocess.run(cmd, capture_output=True, text=True, timeout=300) |
| |
| |
| try: |
| os.remove(webm_path) |
| except: |
| pass |
| |
| if os.path.exists(mp4_path) and os.path.getsize(mp4_path) > 0: |
| return mp4_path |
| |
| return None |
| |
| except Exception as e: |
| print(f"[Convert] Error: {e}") |
| return None |
|
|
|
|
| |
| |
| |
|
|
| css = """ |
| /* ===== ๐จ Google Fonts Import ===== */ |
| @import url('https://fonts.googleapis.com/css2?family=Bangers&family=Comic+Neue:wght@400;700&display=swap'); |
| |
| /* ===== ๐จ Comic Classic Background ===== */ |
| .gradio-container { |
| background-color: #FEF9C3 !important; |
| background-image: |
| radial-gradient(#1F2937 1px, transparent 1px) !important; |
| background-size: 20px 20px !important; |
| min-height: 100vh !important; |
| font-family: 'Comic Neue', cursive, sans-serif !important; |
| } |
| |
| /* ===== Hide HuggingFace Elements ===== */ |
| .huggingface-space-header, |
| #space-header, |
| .space-header, |
| [class*="space-header"], |
| .svelte-1ed2p3z, |
| .space-header-badge, |
| .header-badge, |
| [data-testid="space-header"], |
| .svelte-kqij2n, |
| .svelte-1ax1toq, |
| .embed-container > div:first-child { |
| display: none !important; |
| visibility: hidden !important; |
| height: 0 !important; |
| width: 0 !important; |
| overflow: hidden !important; |
| opacity: 0 !important; |
| pointer-events: none !important; |
| } |
| |
| /* ===== Hide Footer ===== */ |
| footer, |
| .footer, |
| .gradio-container footer, |
| .built-with, |
| [class*="footer"], |
| .gradio-footer, |
| .main-footer, |
| div[class*="footer"], |
| .show-api, |
| .built-with-gradio, |
| a[href*="gradio.app"], |
| a[href*="huggingface.co/spaces"] { |
| display: none !important; |
| visibility: hidden !important; |
| height: 0 !important; |
| padding: 0 !important; |
| margin: 0 !important; |
| } |
| |
| /* ===== Main Container ===== */ |
| #col-container { |
| max-width: 1200px; |
| margin: 0 auto; |
| } |
| |
| /* ===== ๐จ Header Title - Comic Style ===== */ |
| .header-text h1 { |
| font-family: 'Bangers', cursive !important; |
| color: #1F2937 !important; |
| font-size: 3.5rem !important; |
| font-weight: 400 !important; |
| text-align: center !important; |
| margin-bottom: 0.5rem !important; |
| text-shadow: |
| 4px 4px 0px #FACC15, |
| 6px 6px 0px #1F2937 !important; |
| letter-spacing: 3px !important; |
| -webkit-text-stroke: 2px #1F2937 !important; |
| } |
| |
| /* ===== ๐จ Subtitle ===== */ |
| .subtitle { |
| text-align: center !important; |
| font-family: 'Comic Neue', cursive !important; |
| font-size: 1.2rem !important; |
| color: #1F2937 !important; |
| margin-bottom: 1.5rem !important; |
| font-weight: 700 !important; |
| } |
| |
| /* ===== ๐จ Cards/Panels - Comic Frame Style ===== */ |
| .gr-panel, |
| .gr-box, |
| .gr-form, |
| .block, |
| .gr-group { |
| background: #FFFFFF !important; |
| border: 3px solid #1F2937 !important; |
| border-radius: 8px !important; |
| box-shadow: 6px 6px 0px #1F2937 !important; |
| transition: all 0.2s ease !important; |
| } |
| |
| .gr-panel:hover, |
| .block:hover { |
| transform: translate(-2px, -2px) !important; |
| box-shadow: 8px 8px 0px #1F2937 !important; |
| } |
| |
| /* ===== ๐จ Input Fields ===== */ |
| textarea, |
| input[type="text"], |
| input[type="number"] { |
| background: #FFFFFF !important; |
| border: 3px solid #1F2937 !important; |
| border-radius: 8px !important; |
| color: #1F2937 !important; |
| font-family: 'Comic Neue', cursive !important; |
| font-size: 1rem !important; |
| font-weight: 700 !important; |
| transition: all 0.2s ease !important; |
| } |
| |
| textarea:focus, |
| input[type="text"]:focus, |
| input[type="number"]:focus { |
| border-color: #3B82F6 !important; |
| box-shadow: 4px 4px 0px #3B82F6 !important; |
| outline: none !important; |
| } |
| |
| textarea::placeholder { |
| color: #9CA3AF !important; |
| font-weight: 400 !important; |
| } |
| |
| /* ===== ๐จ Primary Button - Comic Blue ===== */ |
| .gr-button-primary, |
| button.primary, |
| .gr-button.primary { |
| background: #3B82F6 !important; |
| border: 3px solid #1F2937 !important; |
| border-radius: 8px !important; |
| color: #FFFFFF !important; |
| font-family: 'Bangers', cursive !important; |
| font-weight: 400 !important; |
| font-size: 1.3rem !important; |
| letter-spacing: 2px !important; |
| padding: 14px 28px !important; |
| box-shadow: 5px 5px 0px #1F2937 !important; |
| transition: all 0.1s ease !important; |
| text-shadow: 1px 1px 0px #1F2937 !important; |
| } |
| |
| .gr-button-primary:hover, |
| button.primary:hover, |
| .gr-button.primary:hover { |
| background: #2563EB !important; |
| transform: translate(-2px, -2px) !important; |
| box-shadow: 7px 7px 0px #1F2937 !important; |
| } |
| |
| .gr-button-primary:active, |
| button.primary:active, |
| .gr-button.primary:active { |
| transform: translate(3px, 3px) !important; |
| box-shadow: 2px 2px 0px #1F2937 !important; |
| } |
| |
| /* ===== ๐จ Secondary Button - Comic Red ===== */ |
| .gr-button-secondary, |
| button.secondary { |
| background: #EF4444 !important; |
| border: 3px solid #1F2937 !important; |
| border-radius: 8px !important; |
| color: #FFFFFF !important; |
| font-family: 'Bangers', cursive !important; |
| font-weight: 400 !important; |
| font-size: 1.1rem !important; |
| letter-spacing: 1px !important; |
| box-shadow: 4px 4px 0px #1F2937 !important; |
| transition: all 0.1s ease !important; |
| text-shadow: 1px 1px 0px #1F2937 !important; |
| } |
| |
| .gr-button-secondary:hover, |
| button.secondary:hover { |
| background: #DC2626 !important; |
| transform: translate(-2px, -2px) !important; |
| box-shadow: 6px 6px 0px #1F2937 !important; |
| } |
| |
| .gr-button-secondary:active, |
| button.secondary:active { |
| transform: translate(2px, 2px) !important; |
| box-shadow: 2px 2px 0px #1F2937 !important; |
| } |
| |
| /* ===== ๐จ Labels ===== */ |
| label, |
| .gr-input-label, |
| .gr-block-label { |
| color: #1F2937 !important; |
| font-family: 'Comic Neue', cursive !important; |
| font-weight: 700 !important; |
| font-size: 1rem !important; |
| } |
| |
| span.gr-label { |
| color: #1F2937 !important; |
| } |
| |
| /* ===== ๐จ File Upload Area ===== */ |
| .gr-file-upload { |
| border: 3px dashed #1F2937 !important; |
| border-radius: 8px !important; |
| background: #FEF9C3 !important; |
| } |
| |
| .gr-file-upload:hover { |
| border-color: #3B82F6 !important; |
| background: #EFF6FF !important; |
| } |
| |
| /* ===== ๐จ Scrollbar - Comic Style ===== */ |
| ::-webkit-scrollbar { |
| width: 12px; |
| height: 12px; |
| } |
| |
| ::-webkit-scrollbar-track { |
| background: #FEF9C3; |
| border: 2px solid #1F2937; |
| } |
| |
| ::-webkit-scrollbar-thumb { |
| background: #3B82F6; |
| border: 2px solid #1F2937; |
| border-radius: 0px; |
| } |
| |
| ::-webkit-scrollbar-thumb:hover { |
| background: #EF4444; |
| } |
| |
| /* ===== ๐จ Selection Highlight ===== */ |
| ::selection { |
| background: #FACC15; |
| color: #1F2937; |
| } |
| |
| /* ===== ๐จ Links ===== */ |
| a { |
| color: #3B82F6 !important; |
| text-decoration: none !important; |
| font-weight: 700 !important; |
| } |
| |
| a:hover { |
| color: #EF4444 !important; |
| } |
| |
| /* ===== ๐จ Row/Column Spacing ===== */ |
| .gr-row { |
| gap: 1.5rem !important; |
| } |
| |
| .gr-column { |
| gap: 1rem !important; |
| } |
| |
| /* ===== Responsive Adjustments ===== */ |
| @media (max-width: 768px) { |
| .header-text h1 { |
| font-size: 2.2rem !important; |
| text-shadow: |
| 3px 3px 0px #FACC15, |
| 4px 4px 0px #1F2937 !important; |
| } |
| |
| .gr-button-primary, |
| button.primary { |
| padding: 12px 20px !important; |
| font-size: 1.1rem !important; |
| } |
| |
| .gr-panel, |
| .block { |
| box-shadow: 4px 4px 0px #1F2937 !important; |
| } |
| } |
| |
| /* ===== ๐จ Disable Dark Mode ===== */ |
| @media (prefers-color-scheme: dark) { |
| .gradio-container { |
| background-color: #FEF9C3 !important; |
| } |
| } |
| """ |
|
|
|
|
| |
| |
| |
| with gr.Blocks(title="Video Editor") as demo: |
|
|
| |
| |
| gr.HTML(f"<style>{css}</style>") |
| |
| |
| gr.HTML(""" |
| <div style="text-align: center; margin: 20px 0 10px 0;"> |
| <a href="https://www.humangen.ai" target="_blank" style="text-decoration: none;"> |
| <img src="https://img.shields.io/static/v1?label=๐ HOME&message=HUMANGEN.AI&color=0000ff&labelColor=ffcc00&style=for-the-badge" alt="HOME"> |
| </a> |
| </div> |
| """) |
| |
| |
| gr.Markdown( |
| """ |
| # ๐ฌ VIDEO EDITOR ๐ฌ |
| """, |
| elem_classes="header-text" |
| ) |
| |
| gr.Markdown( |
| """ |
| <p class="subtitle">โ๏ธ Edit your videos with timeline, effects & more! ๐ฅ</p> |
| """, |
| ) |
| f = gr.File(label="๐ Upload Files (Video, Image, Audio)", file_count="multiple", file_types=["video", "image", "audio"]) |
|
|
| e = gr.HTML(value=make_iframe([])) |
| |
| gr.Markdown("---") |
|
|
| gr.Markdown("### ๐ฅ MP4 Conversion") |
| gr.Markdown("In editor: 'Export' โ 'Copy for MP4' โ Paste below") |
| with gr.Row(): |
| webm_data = gr.Textbox(label="WebM Data (base64)", placeholder="Paste here (Ctrl+V)", lines=2, scale=4) |
| convert_btn = gr.Button("๐ฌ Convert to MP4", variant="primary", scale=1) |
| |
| mp4_output = gr.File(label="๐ฅ Download MP4 ") |
| |
| f.change(fn=lambda x: make_iframe(process_file(x)), inputs=[f], outputs=[e]) |
| convert_btn.click(fn=convert_webm_to_mp4, inputs=[webm_data], outputs=[mp4_output]) |
|
|
| if __name__ == "__main__": |
| demo.launch() |