Spaces:
Sleeping
Sleeping
import yt_dlp | |
import ffmpeg | |
import os | |
import shutil | |
import gradio as gr | |
# import subprocess | |
# def download_from_youtube(video_id): | |
# cmd = f"yt-dlp -o '%(id)s.mp4' --format mp4 --prefer-ffmpeg -- {video_id}" | |
# process = subprocess.Popen( | |
# command, | |
# bufsize=2048, | |
# shell=True, | |
# stdin=subprocess.PIPE, | |
# stdout=subprocess.PIPE, | |
# stderr=subprocess.PIPE, | |
# close_fds=True, | |
# ) | |
# _ = process.communicate() | |
# print(f"Command status: {rocess.returncode}") | |
# return True | |
def download_video(url): | |
ydl_opts = { | |
'format': 'bestvideo[height<=480]+bestaudio/best[height<=480]', | |
'merge_output_format': 'mp4', | |
'quiet': True, | |
} | |
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
info_dict = ydl.extract_info(url, download=False) | |
video_id = info_dict.get("id", None) | |
filename = f"{video_id}.mp4" | |
ydl_opts['outtmpl'] = filename | |
ydl = yt_dlp.YoutubeDL(ydl_opts) | |
ydl.download([url]) | |
return filename | |
def create_sprite(video_path, sprite_output="sprite.jpg"): | |
( | |
ffmpeg | |
.input(video_path) | |
.filter('select', 'not(mod(t,15))') | |
.filter('scale', 320, -1) | |
.filter('tile', '5x4') | |
.output(sprite_output, vframes=1) | |
.run(overwrite_output=True) | |
) | |
return sprite_output | |
def process_youtube_url(url): | |
video_path = download_video(url) | |
sprite_output = "sprite.jpg" | |
sprite_path = create_sprite(video_path, sprite_output) | |
if os.path.exists(video_path): | |
os.remove(video_path) | |
return sprite_path | |
iface = gr.Interface( | |
fn=process_youtube_url, | |
inputs=gr.Textbox(label="YouTube URL"), | |
outputs=gr.Image(type="filepath", label="Sprite Image"), | |
title="YouTube Video Sprite Generator", | |
description="Enter a YouTube URL to download the video and create a sprite image of frames." | |
) | |
iface.queue() | |
iface.launch(server_name='0.0.0.0') | |