Spaces:
Paused

seawolf2357 commited on
Commit
d081790
Β·
verified Β·
1 Parent(s): 1770633

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -54
app.py CHANGED
@@ -98,7 +98,7 @@ body{{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Noto Sans
98
  .clip-info{{padding:0 6px;flex:1;overflow:hidden}}
99
  .clip-name{{font-size:10px;font-weight:500;color:#fff;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}}
100
  .clip-duration{{font-size:9px;color:rgba(255,255,255,0.7)}}
101
- .clip-handles{{position:absolute;top:0;bottom:0;width:6px;background:rgba(255,255,255,0.4);cursor:ew-resize;opacity:0;transition:opacity 0.15s}}
102
  .timeline-clip:hover .clip-handles{{opacity:1}}
103
  .clip-handle-left{{left:0;border-radius:6px 0 0 6px}}
104
  .clip-handle-right{{right:0;border-radius:0 6px 6px 0}}
@@ -208,7 +208,7 @@ Simple Video Editor
208
  </div>
209
  </div>
210
  <script>
211
- var S={{mediaLibrary:[],timelineClips:[],selectedClipId:null,isPlaying:false,isMuted:false,currentTime:0,totalDuration:0,zoom:1,pps:80,undoStack:[],animId:null,audioElements:{{}}}};
212
  function gid(){{return Date.now().toString(36)+Math.random().toString(36).substr(2)}}
213
  function fmt(s){{if(!s||isNaN(s))s=0;var m=Math.floor(s/60),sec=Math.floor(s%60),ms=Math.floor((s%1)*100);return(m<10?'0':'')+m+':'+(sec<10?'0':'')+sec+'.'+(ms<10?'0':'')+ms}}
214
  function upStat(msg){{var e=document.getElementById('statusBar');if(e)e.textContent=msg+' | λ―Έλ””μ–΄: '+S.mediaLibrary.length+'개 | 클립: '+S.timelineClips.length+'개'}}
@@ -249,7 +249,7 @@ var c={{id:gid(),mid:m.id,name:m.name,type:m.type,url:m.url,thumb:m.thumb,track:
249
  S.timelineClips.push(c);
250
  renderTL();upDur();upStat('클립 좔가됨: '+c.name);
251
  }}
252
- function getTrEnd(tr){{var mx=0;S.timelineClips.forEach(function(c){{if(c.track===tr){{var e=c.start+(c.te-c.ts);if(e>mx)mx=e}}}});return mx}}
253
  function renderTL(){{
254
  var t0=document.getElementById('track0'),t1=document.getElementById('track1');
255
  if(t0)t0.innerHTML='';if(t1)t1.innerHTML='';
@@ -263,11 +263,44 @@ d.onclick=function(e){{e.stopPropagation();selClip(c.id)}};
263
  d.oncontextmenu=function(e){{e.preventDefault();selClip(c.id);showCtx(e.clientX,e.clientY)}};
264
  d.ondragstart=function(e){{e.dataTransfer.setData('cid',c.id);e.dataTransfer.setData('ox',e.offsetX.toString())}};
265
  var th=c.thumb?'<img class="clip-thumbnail" src="'+c.thumb+'">':'';
266
- d.innerHTML=th+'<div class="clip-info"><div class="clip-name">'+c.name+'</div><div class="clip-duration">'+fmt(cd)+'</div></div><div class="clip-handles clip-handle-left"></div><div class="clip-handles clip-handle-right"></div>';
 
 
 
 
267
  tr.appendChild(d);
268
  }});
269
  renderRuler();setupDrop();
270
  }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  function renderRuler(){{
272
  var r=document.getElementById('timelineRuler');if(!r)return;
273
  var w=Math.max(S.totalDuration*S.pps*S.zoom+300,1000);r.style.width=w+'px';
@@ -306,12 +339,19 @@ ct.innerHTML=h;
306
  }}
307
  function upProp(p,v){{save();var c=S.timelineClips.find(function(x){{return x.id===S.selectedClipId}});if(c){{c[p]=v;renderTL();upDur();renderProps()}}}}
308
  function editorSplit(){{
309
- if(!S.selectedClipId)return;
310
  var c=S.timelineClips.find(function(x){{return x.id===S.selectedClipId}});if(!c)return;
311
- save();var cd=c.te-c.ts,sp=cd/2;
312
- var c2=JSON.parse(JSON.stringify(c));c2.id=gid();c2.start=c.start+sp;c2.ts=c.ts+sp;
313
- c.te=c.ts+sp;
314
- S.timelineClips.push(c2);renderTL();hideCtx();upStat('클립 뢄할됨');
 
 
 
 
 
 
 
315
  }}
316
  function editorDuplicate(){{
317
  if(!S.selectedClipId)return;
@@ -418,58 +458,82 @@ function editorExport(){{
418
  if(S.timelineClips.length===0){{alert('νƒ€μž„λΌμΈμ— 클립을 μΆ”κ°€ν•΄μ£Όμ„Έμš”.');return}}
419
  document.getElementById('exportModal').style.display='flex';
420
  document.getElementById('exportProgress').style.width='0%';
421
- document.getElementById('exportStatus').textContent='ν”„λ‘œμ νŠΈ 정보 μ€€λΉ„ 쀑...';
422
- var p=0,iv=setInterval(function(){{
423
- p+=5;document.getElementById('exportProgress').style.width=p+'%';
424
- if(p===30)document.getElementById('exportStatus').textContent='클립 정보 μˆ˜μ§‘ 쀑...';
425
- if(p===60)document.getElementById('exportStatus').textContent='파일 생성 쀑...';
426
- if(p>=100){{
427
- clearInterval(iv);
428
- document.getElementById('exportStatus').textContent='βœ… μ™„λ£Œ! λ‹€μš΄λ‘œλ“œλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€.';
429
- document.getElementById('cancelExportBtn').textContent='λ‹«κΈ°';
430
- setTimeout(function(){{downloadProject()}},500);
431
- }}
432
- }},30);
433
- }}
434
- function downloadProject(){{
435
- var project={{
436
- version:'1.0',
437
- duration:S.totalDuration,
438
- clips:S.timelineClips.map(function(c){{
439
- return {{id:c.id,name:c.name,type:c.type,track:c.track,start:c.start,duration:c.dur,trimStart:c.ts,trimEnd:c.te,volume:c.vol}};
440
- }}),
441
- media:S.mediaLibrary.map(function(m){{
442
- return {{id:m.id,name:m.name,type:m.type,duration:m.duration}};
443
- }})
444
- }};
445
- var json=JSON.stringify(project,null,2);
446
- var blob=new Blob([json],{{type:'application/json'}});
447
  var url=URL.createObjectURL(blob);
448
  var a=document.createElement('a');
449
- a.href=url;
450
- a.download='video_project_'+Date.now()+'.json';
451
- document.body.appendChild(a);
452
- a.click();
453
- document.body.removeChild(a);
454
  URL.revokeObjectURL(url);
455
- if(S.timelineClips.length===1){{
456
- var clip=S.timelineClips[0];
457
- var media=S.mediaLibrary.find(function(m){{return m.id===clip.mid}});
458
- if(media&&(media.type==='video'||media.type==='audio')){{
459
- setTimeout(function(){{
460
- if(confirm('원본 λ―Έλ””μ–΄ νŒŒμΌλ„ λ‹€μš΄λ‘œλ“œν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?')){{
461
- var a2=document.createElement('a');
462
- a2.href=media.url;
463
- a2.download=media.name;
464
- document.body.appendChild(a2);
465
- a2.click();
466
- document.body.removeChild(a2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467
  }}
468
- }},1000);
 
469
  }}
 
470
  }}
 
 
 
 
 
 
 
471
  }}
472
- function editorCancelExport(){{document.getElementById('exportModal').style.display='none';document.getElementById('cancelExportBtn').textContent='μ·¨μ†Œ'}}
473
  document.addEventListener('keydown',function(e){{
474
  if(e.target.tagName==='INPUT')return;
475
  if(e.code==='Space'){{e.preventDefault();editorTogglePlay()}}
 
98
  .clip-info{{padding:0 6px;flex:1;overflow:hidden}}
99
  .clip-name{{font-size:10px;font-weight:500;color:#fff;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}}
100
  .clip-duration{{font-size:9px;color:rgba(255,255,255,0.7)}}
101
+ .clip-handles{{position:absolute;top:0;bottom:0;width:10px;background:rgba(255,255,255,0.5);cursor:ew-resize;opacity:0;transition:opacity 0.15s;z-index:10}}
102
  .timeline-clip:hover .clip-handles{{opacity:1}}
103
  .clip-handle-left{{left:0;border-radius:6px 0 0 6px}}
104
  .clip-handle-right{{right:0;border-radius:0 6px 6px 0}}
 
208
  </div>
209
  </div>
210
  <script>
211
+ var S={{mediaLibrary:[],timelineClips:[],selectedClipId:null,isPlaying:false,isMuted:false,currentTime:0,totalDuration:0,zoom:1,pps:80,undoStack:[],animId:null,audioElements:{{}},trimData:null,exportCancelled:false}};
212
  function gid(){{return Date.now().toString(36)+Math.random().toString(36).substr(2)}}
213
  function fmt(s){{if(!s||isNaN(s))s=0;var m=Math.floor(s/60),sec=Math.floor(s%60),ms=Math.floor((s%1)*100);return(m<10?'0':'')+m+':'+(sec<10?'0':'')+sec+'.'+(ms<10?'0':'')+ms}}
214
  function upStat(msg){{var e=document.getElementById('statusBar');if(e)e.textContent=msg+' | λ―Έλ””μ–΄: '+S.mediaLibrary.length+'개 | 클립: '+S.timelineClips.length+'개'}}
 
249
  S.timelineClips.push(c);
250
  renderTL();upDur();upStat('클립 좔가됨: '+c.name);
251
  }}
252
+ function getTrEnd(tr){{var mx=0;S.timelineClips.forEach(function(c){{if(c.track===tr){{var e=c.start+(c.te-c.ts);if(e>mx)mx=e}}}});return Math.round(mx*1000)/1000}}
253
  function renderTL(){{
254
  var t0=document.getElementById('track0'),t1=document.getElementById('track1');
255
  if(t0)t0.innerHTML='';if(t1)t1.innerHTML='';
 
263
  d.oncontextmenu=function(e){{e.preventDefault();selClip(c.id);showCtx(e.clientX,e.clientY)}};
264
  d.ondragstart=function(e){{e.dataTransfer.setData('cid',c.id);e.dataTransfer.setData('ox',e.offsetX.toString())}};
265
  var th=c.thumb?'<img class="clip-thumbnail" src="'+c.thumb+'">':'';
266
+ d.innerHTML=th+'<div class="clip-info"><div class="clip-name">'+c.name+'</div><div class="clip-duration">'+fmt(cd)+'</div></div><div class="clip-handles clip-handle-left" data-cid="'+c.id+'" data-side="left"></div><div class="clip-handles clip-handle-right" data-cid="'+c.id+'" data-side="right"></div>';
267
+ var hl=d.querySelector('.clip-handle-left');
268
+ var hr=d.querySelector('.clip-handle-right');
269
+ hl.onmousedown=function(e){{e.stopPropagation();startTrim(e,c.id,'left')}};
270
+ hr.onmousedown=function(e){{e.stopPropagation();startTrim(e,c.id,'right')}};
271
  tr.appendChild(d);
272
  }});
273
  renderRuler();setupDrop();
274
  }}
275
+ function startTrim(e,cid,side){{
276
+ e.preventDefault();
277
+ var c=S.timelineClips.find(function(x){{return x.id===cid}});if(!c)return;
278
+ save();
279
+ S.trimData={{cid:cid,side:side,startX:e.clientX,origTs:c.ts,origTe:c.te,origStart:c.start}};
280
+ document.addEventListener('mousemove',doTrim);
281
+ document.addEventListener('mouseup',endTrim);
282
+ }}
283
+ function doTrim(e){{
284
+ if(!S.trimData)return;
285
+ var c=S.timelineClips.find(function(x){{return x.id===S.trimData.cid}});if(!c)return;
286
+ var dx=e.clientX-S.trimData.startX;
287
+ var dt=dx/(S.pps*S.zoom);
288
+ if(S.trimData.side==='left'){{
289
+ var newTs=Math.max(0,Math.min(c.te-0.1,S.trimData.origTs+dt));
290
+ var delta=newTs-S.trimData.origTs;
291
+ c.ts=newTs;
292
+ c.start=S.trimData.origStart+delta;
293
+ }}else{{
294
+ var newTe=Math.max(c.ts+0.1,Math.min(c.dur,S.trimData.origTe+dt));
295
+ c.te=newTe;
296
+ }}
297
+ renderTL();upDur();
298
+ }}
299
+ function endTrim(){{
300
+ S.trimData=null;
301
+ document.removeEventListener('mousemove',doTrim);
302
+ document.removeEventListener('mouseup',endTrim);
303
+ }}
304
  function renderRuler(){{
305
  var r=document.getElementById('timelineRuler');if(!r)return;
306
  var w=Math.max(S.totalDuration*S.pps*S.zoom+300,1000);r.style.width=w+'px';
 
339
  }}
340
  function upProp(p,v){{save();var c=S.timelineClips.find(function(x){{return x.id===S.selectedClipId}});if(c){{c[p]=v;renderTL();upDur();renderProps()}}}}
341
  function editorSplit(){{
342
+ if(!S.selectedClipId){{alert('클립을 μ„ νƒν•΄μ£Όμ„Έμš”.');return}}
343
  var c=S.timelineClips.find(function(x){{return x.id===S.selectedClipId}});if(!c)return;
344
+ var clipEnd=c.start+(c.te-c.ts);
345
+ if(S.currentTime<=c.start||S.currentTime>=clipEnd){{alert('ν”Œλ ˆμ΄ν—€λ“œκ°€ μ„ νƒν•œ 클립 λ²”μœ„ μ•ˆμ— μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.');return}}
346
+ save();
347
+ var splitAt=S.currentTime-c.start;
348
+ var c2=JSON.parse(JSON.stringify(c));
349
+ c2.id=gid();
350
+ c2.start=S.currentTime;
351
+ c2.ts=c.ts+splitAt;
352
+ c.te=c.ts+splitAt;
353
+ S.timelineClips.push(c2);
354
+ renderTL();upDur();hideCtx();upStat('클립이 ν˜„μž¬ μœ„μΉ˜μ—μ„œ 뢄할됨');
355
  }}
356
  function editorDuplicate(){{
357
  if(!S.selectedClipId)return;
 
458
  if(S.timelineClips.length===0){{alert('νƒ€μž„λΌμΈμ— 클립을 μΆ”κ°€ν•΄μ£Όμ„Έμš”.');return}}
459
  document.getElementById('exportModal').style.display='flex';
460
  document.getElementById('exportProgress').style.width='0%';
461
+ document.getElementById('exportStatus').textContent='μ˜μƒ λ Œλ”λ§ μ€€λΉ„ 쀑...';
462
+ document.getElementById('cancelExportBtn').textContent='μ·¨μ†Œ';
463
+ S.exportCancelled=false;
464
+ setTimeout(function(){{startRender()}},100);
465
+ }}
466
+ function startRender(){{
467
+ var canvas=document.createElement('canvas');
468
+ canvas.width=1280;canvas.height=720;
469
+ var ctx=canvas.getContext('2d');
470
+ var stream=canvas.captureStream(30);
471
+ var audioCtx=new(window.AudioContext||window.webkitAudioContext)();
472
+ var dest=audioCtx.createMediaStreamDestination();
473
+ stream.addTrack(dest.stream.getAudioTracks()[0]||stream.getAudioTracks()[0]);
474
+ var chunks=[];
475
+ var recorder=new MediaRecorder(stream,{{mimeType:'video/webm;codecs=vp9',videoBitsPerSecond:5000000}});
476
+ recorder.ondataavailable=function(e){{if(e.data.size>0)chunks.push(e.data)}};
477
+ recorder.onstop=function(){{
478
+ if(S.exportCancelled)return;
479
+ var blob=new Blob(chunks,{{type:'video/webm'}});
 
 
 
 
 
 
 
480
  var url=URL.createObjectURL(blob);
481
  var a=document.createElement('a');
482
+ a.href=url;a.download='edited_video_'+Date.now()+'.webm';
483
+ document.body.appendChild(a);a.click();document.body.removeChild(a);
 
 
 
484
  URL.revokeObjectURL(url);
485
+ document.getElementById('exportStatus').textContent='βœ… λ‹€μš΄λ‘œλ“œ μ™„λ£Œ!';
486
+ document.getElementById('cancelExportBtn').textContent='λ‹«κΈ°';
487
+ }};
488
+ recorder.start();
489
+ var renderTime=0;
490
+ var duration=S.totalDuration;
491
+ var fps=30;
492
+ var frameTime=1/fps;
493
+ var audioSources={{}};
494
+ S.mediaLibrary.forEach(function(m){{
495
+ if(m.type==='video'||m.type==='audio'){{
496
+ var el=document.createElement(m.type);
497
+ el.src=m.url;el.crossOrigin='anonymous';el.preload='auto';el.muted=true;
498
+ audioSources[m.id]=el;
499
+ }}
500
+ }});
501
+ function renderFrame(){{
502
+ if(S.exportCancelled){{recorder.stop();return}}
503
+ if(renderTime>=duration){{recorder.stop();return}}
504
+ var prog=Math.min(100,Math.round((renderTime/duration)*100));
505
+ document.getElementById('exportProgress').style.width=prog+'%';
506
+ document.getElementById('exportStatus').textContent='λ Œλ”λ§ 쀑... '+prog+'% ('+fmt(renderTime)+' / '+fmt(duration)+')';
507
+ ctx.fillStyle='#000';ctx.fillRect(0,0,1280,720);
508
+ var curClips=S.timelineClips.filter(function(c){{var ce=c.start+(c.te-c.ts);return renderTime>=c.start&&renderTime<ce}});
509
+ var vc=curClips.find(function(c){{return c.type==='video'||c.type==='image'}});
510
+ if(vc){{
511
+ var clipTime=renderTime-vc.start+vc.ts;
512
+ var media=S.mediaLibrary.find(function(m){{return m.id===vc.mid}});
513
+ if(media){{
514
+ if(vc.type==='image'){{
515
+ var img=new Image();img.src=media.url;
516
+ try{{drawFit(ctx,img,1280,720)}}catch(e){{}}
517
+ }}else if(vc.type==='video'){{
518
+ var vid=audioSources[media.id];
519
+ if(vid){{vid.currentTime=clipTime;try{{drawFit(ctx,vid,1280,720)}}catch(e){{}}}}
520
+ }}
521
+ }}
522
  }}
523
+ renderTime+=frameTime;
524
+ setTimeout(renderFrame,1);
525
  }}
526
+ setTimeout(renderFrame,500);
527
  }}
528
+ function drawFit(ctx,src,w,h){{
529
+ var sw=src.videoWidth||src.width||1280;
530
+ var sh=src.videoHeight||src.height||720;
531
+ var scale=Math.min(w/sw,h/sh);
532
+ var dw=sw*scale,dh=sh*scale;
533
+ var dx=(w-dw)/2,dy=(h-dh)/2;
534
+ ctx.drawImage(src,dx,dy,dw,dh);
535
  }}
536
+ function editorCancelExport(){{S.exportCancelled=true;document.getElementById('exportModal').style.display='none';document.getElementById('cancelExportBtn').textContent='μ·¨μ†Œ'}}
537
  document.addEventListener('keydown',function(e){{
538
  if(e.target.tagName==='INPUT')return;
539
  if(e.code==='Space'){{e.preventDefault();editorTogglePlay()}}