Spaces:
Build error
Build error
Create clipper.py
Browse files- clipper.py +68 -0
clipper.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import yt_dlp
|
| 3 |
+
from moviepy.editor import VideoFileClip, vfx
|
| 4 |
+
import tempfile
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
def download_youtube(url, out_dir="downloads"):
|
| 9 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 10 |
+
ydl_opts = {
|
| 11 |
+
"format": "bestvideo+bestaudio/best",
|
| 12 |
+
"outtmpl": os.path.join(out_dir, "%(id)s.%(ext)s"),
|
| 13 |
+
"merge_output_format": "mp4",
|
| 14 |
+
"quiet": True,
|
| 15 |
+
}
|
| 16 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 17 |
+
info = ydl.extract_info(url, download=True)
|
| 18 |
+
filename = ydl.prepare_filename(info)
|
| 19 |
+
if not filename.endswith(".mp4"):
|
| 20 |
+
filename = filename.rsplit(".", 1)[0] + ".mp4"
|
| 21 |
+
return filename
|
| 22 |
+
|
| 23 |
+
def detect_highlights_simple(video_path):
|
| 24 |
+
clip = VideoFileClip(video_path)
|
| 25 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
| 26 |
+
tmp.close()
|
| 27 |
+
clip.audio.write_audiofile(tmp.name, logger=None)
|
| 28 |
+
audio = AudioSegment.from_wav(tmp.name)
|
| 29 |
+
os.unlink(tmp.name)
|
| 30 |
+
|
| 31 |
+
window = 1000
|
| 32 |
+
energies = [audio[i:i+window].rms for i in range(0, len(audio), window)]
|
| 33 |
+
energies = np.array(energies)
|
| 34 |
+
|
| 35 |
+
best = energies.argsort()[-3:][::-1]
|
| 36 |
+
clips = []
|
| 37 |
+
for idx in best:
|
| 38 |
+
start = int(idx)
|
| 39 |
+
end = start + 10
|
| 40 |
+
if end > clip.duration:
|
| 41 |
+
end = int(clip.duration)
|
| 42 |
+
clips.append((start, end))
|
| 43 |
+
clip.close()
|
| 44 |
+
return clips
|
| 45 |
+
|
| 46 |
+
def make_vertical_clip(video_path, start, end):
|
| 47 |
+
clip = VideoFileClip(video_path).subclip(start, end)
|
| 48 |
+
out_path = f"outputs/clip_{start}.mp4"
|
| 49 |
+
|
| 50 |
+
w, h = clip.size
|
| 51 |
+
target_w, target_h = 1080, 1920
|
| 52 |
+
scale = max(target_h / h, target_w / w)
|
| 53 |
+
clip = clip.resize(scale)
|
| 54 |
+
clip = clip.crop(x_center=clip.w/2, y_center=clip.h/2, width=target_w, height=target_h)
|
| 55 |
+
clip.write_videofile(out_path, fps=24, audio_codec="aac", logger=None)
|
| 56 |
+
clip.close()
|
| 57 |
+
return out_path
|
| 58 |
+
|
| 59 |
+
def process_youtube_link(url):
|
| 60 |
+
video = download_youtube(url)
|
| 61 |
+
return process_uploaded_file(video)
|
| 62 |
+
|
| 63 |
+
def process_uploaded_file(video_path):
|
| 64 |
+
highlights = detect_highlights_simple(video_path)
|
| 65 |
+
final = []
|
| 66 |
+
for start, end in highlights:
|
| 67 |
+
final.append(make_vertical_clip(video_path, start, end))
|
| 68 |
+
return final
|