|
import os
|
|
import shutil
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import gradio as gr
|
|
import yt_dlp
|
|
|
|
from App_Function_Libraries.Utils.Utils import sanitize_filename, downloaded_files
|
|
|
|
|
|
def create_utilities_yt_video_tab():
|
|
with gr.Tab("YouTube Video Downloader"):
|
|
with gr.Row():
|
|
with gr.Column():
|
|
gr.Markdown(
|
|
"<h3>Youtube Video Downloader</h3><p>This Input takes a Youtube URL as input and creates a webm file for you to download. </br><em>If you want a full-featured one:</em> <strong><em>https://github.com/StefanLobbenmeier/youtube-dl-gui</strong></em> or <strong><em>https://github.com/yt-dlg/yt-dlg</em></strong></p>")
|
|
youtube_url_input = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here")
|
|
download_button = gr.Button("Download Video")
|
|
with gr.Column():
|
|
output_file = gr.File(label="Download Video")
|
|
output_message = gr.Textbox(label="Status")
|
|
|
|
download_button.click(
|
|
fn=gradio_download_youtube_video,
|
|
inputs=youtube_url_input,
|
|
outputs=[output_file, output_message]
|
|
)
|
|
|
|
def create_utilities_yt_audio_tab():
|
|
with gr.Tab("YouTube Audio Downloader"):
|
|
with gr.Row():
|
|
with gr.Column():
|
|
gr.Markdown(
|
|
"<h3>Youtube Audio Downloader</h3><p>This Input takes a Youtube URL as input and creates an audio file for you to download.</p>"
|
|
+"\n<em>If you want a full-featured one:</em> <strong><em>https://github.com/StefanLobbenmeier/youtube-dl-gui</strong></em>\n or \n<strong><em>https://github.com/yt-dlg/yt-dlg</em></strong></p>")
|
|
youtube_url_input_audio = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here")
|
|
download_button_audio = gr.Button("Download Audio")
|
|
with gr.Column():
|
|
output_file_audio = gr.File(label="Download Audio")
|
|
output_message_audio = gr.Textbox(label="Status")
|
|
|
|
from App_Function_Libraries.Audio.Audio_Files import download_youtube_audio
|
|
download_button_audio.click(
|
|
fn=download_youtube_audio,
|
|
inputs=youtube_url_input_audio,
|
|
outputs=[output_file_audio, output_message_audio]
|
|
)
|
|
|
|
def create_utilities_yt_timestamp_tab():
|
|
with gr.Tab("YouTube Timestamp URL Generator"):
|
|
gr.Markdown("## Generate YouTube URL with Timestamp")
|
|
with gr.Row():
|
|
with gr.Column():
|
|
url_input = gr.Textbox(label="YouTube URL")
|
|
hours_input = gr.Number(label="Hours", value=0, minimum=0, precision=0)
|
|
minutes_input = gr.Number(label="Minutes", value=0, minimum=0, maximum=59, precision=0)
|
|
seconds_input = gr.Number(label="Seconds", value=0, minimum=0, maximum=59, precision=0)
|
|
generate_button = gr.Button("Generate URL")
|
|
with gr.Column():
|
|
output_url = gr.Textbox(label="Timestamped URL")
|
|
|
|
from App_Function_Libraries.Video_DL_Ingestion_Lib import generate_timestamped_url
|
|
generate_button.click(
|
|
fn=generate_timestamped_url,
|
|
inputs=[url_input, hours_input, minutes_input, seconds_input],
|
|
outputs=output_url
|
|
)
|
|
|
|
|
|
def gradio_download_youtube_video(url):
|
|
try:
|
|
|
|
ffmpeg_path = './Bin/ffmpeg.exe' if os.name == 'nt' else 'ffmpeg'
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
with yt_dlp.YoutubeDL({'quiet': True}) as ydl:
|
|
info_dict = ydl.extract_info(url, download=False)
|
|
sanitized_title = sanitize_filename(info_dict['title'])
|
|
original_ext = info_dict['ext']
|
|
|
|
|
|
temp_file_path = Path(temp_dir) / f"{sanitized_title}.{original_ext}"
|
|
|
|
|
|
ydl_opts = {
|
|
'format': 'bestvideo+bestaudio/best',
|
|
'ffmpeg_location': ffmpeg_path,
|
|
'outtmpl': str(temp_file_path),
|
|
'noplaylist': True,
|
|
'quiet': True
|
|
}
|
|
|
|
|
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
ydl.download([url])
|
|
|
|
|
|
if not temp_file_path.exists():
|
|
raise FileNotFoundError(f"Expected file was not found: {temp_file_path}")
|
|
|
|
|
|
persistent_dir = Path("downloads")
|
|
persistent_dir.mkdir(exist_ok=True)
|
|
|
|
|
|
persistent_file_path = persistent_dir / f"{sanitized_title}.{original_ext}"
|
|
shutil.move(str(temp_file_path), str(persistent_file_path))
|
|
|
|
|
|
downloaded_files.append(str(persistent_file_path))
|
|
|
|
return str(persistent_file_path), f"Video downloaded successfully: {sanitized_title}.{original_ext}"
|
|
except Exception as e:
|
|
return None, f"Error downloading video: {str(e)}"
|
|
|
|
|