Spaces:
Sleeping
Sleeping
root
commited on
Commit
·
935cd4d
1
Parent(s):
f7b5679
first commit
Browse files- .gitignore +1 -0
- README.md +3 -1
- app.py +74 -0
- packages.txt +1 -0
- requirements.txt +2 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.ipynb_checkpoints
|
README.md
CHANGED
@@ -10,4 +10,6 @@ pinned: false
|
|
10 |
license: unlicense
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
10 |
license: unlicense
|
11 |
---
|
12 |
|
13 |
+
## YouTube Sprite Generator
|
14 |
+
|
15 |
+
Generate sprites from a YouTube URL
|
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yt_dlp
|
2 |
+
import ffmpeg
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
def download_video(url):
|
8 |
+
ydl_opts = {
|
9 |
+
'format': 'bestvideo[height<=480]+bestaudio/best[height<=480]',
|
10 |
+
'merge_output_format': 'mp4',
|
11 |
+
'quiet': True,
|
12 |
+
}
|
13 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
14 |
+
info_dict = ydl.extract_info(url, download=False)
|
15 |
+
video_id = info_dict.get("id", None)
|
16 |
+
filename = f"{video_id}.mp4"
|
17 |
+
ydl_opts['outtmpl'] = filename
|
18 |
+
ydl = yt_dlp.YoutubeDL(ydl_opts)
|
19 |
+
ydl.download([url])
|
20 |
+
return filename
|
21 |
+
|
22 |
+
def create_sprite(video_path, sprite_output="sprite.jpg", frame_interval=10, num_frames=20):
|
23 |
+
frames_folder = "frames"
|
24 |
+
if not os.path.exists(frames_folder):
|
25 |
+
os.makedirs(frames_folder)
|
26 |
+
|
27 |
+
frames_pattern = os.path.join(frames_folder, "frame_%03d.jpg")
|
28 |
+
|
29 |
+
# Extract frames from the video at the specified interval
|
30 |
+
(
|
31 |
+
ffmpeg
|
32 |
+
.input(video_path)
|
33 |
+
.filter('fps', fps=1/frame_interval)
|
34 |
+
.output(frames_pattern, vframes=num_frames)
|
35 |
+
.run(overwrite_output=True)
|
36 |
+
)
|
37 |
+
|
38 |
+
# Calculate the tile layout
|
39 |
+
rows = (num_frames + 3) // 4 # Ensure at least one row
|
40 |
+
|
41 |
+
# Create the sprite from the extracted frames
|
42 |
+
(
|
43 |
+
ffmpeg
|
44 |
+
.input(os.path.join(frames_folder, "frame_*.jpg"), pattern_type='glob', framerate=1)
|
45 |
+
.filter('scale', 320, -1)
|
46 |
+
.filter('tile', f"4x{rows}")
|
47 |
+
.output(sprite_output)
|
48 |
+
.run(overwrite_output=True)
|
49 |
+
)
|
50 |
+
|
51 |
+
# Clean up frame files
|
52 |
+
shutil.rmtree(frames_folder)
|
53 |
+
|
54 |
+
return sprite_output
|
55 |
+
|
56 |
+
def process_youtube_url(url):
|
57 |
+
video_path = download_video(url)
|
58 |
+
sprite_output = "sprite.jpg"
|
59 |
+
sprite_path = create_sprite(video_path, sprite_output)
|
60 |
+
if os.path.exists(video_path):
|
61 |
+
os.remove(video_path)
|
62 |
+
|
63 |
+
return sprite_path
|
64 |
+
|
65 |
+
iface = gr.Interface(
|
66 |
+
fn=process_youtube_url,
|
67 |
+
inputs=gr.Textbox(label="YouTube URL"),
|
68 |
+
outputs=gr.Image(type="filepath", label="Sprite Image"),
|
69 |
+
title="YouTube Video Sprite Generator",
|
70 |
+
description="Enter a YouTube URL to download the video and create a sprite image of frames."
|
71 |
+
)
|
72 |
+
|
73 |
+
iface.queue()
|
74 |
+
iface.launch()
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
ffmpeg
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
ffmpeg-python
|
2 |
+
yt_dlp
|