import gradio as gr import argparse import os from time import sleep from pytube import YouTube import re # url = "https://www.youtube.com/watch?v=yX5EJf4R77s" html_social_media = '''
''' html_buy_me_a_coffe = ''' ''' def remove_all_files(): command = f"rm -r audios" os.system(command) command = f"rm -r videos" os.system(command) def reset_frontend(): visible = False return ( "", gr.Image(visible=visible), gr.Button(visible=visible), gr.Textbox(visible=visible), gr.Textbox(visible=visible), gr.Audio(visible=visible), gr.Video(visible=visible), ) def get_youtube_thumbnail(url): yt = YouTube(url) thumbnail_url = yt.thumbnail_url return thumbnail_url def is_valid_youtube_url(url): # This regular expression should match the following YouTube URL formats: # - https://youtube.com/watch?v=video_id # - https://www.youtube.com/watch?v=video_id # - https://youtu.be/video_id patron_youtube = r'(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[\w-]+' return bool(re.match(patron_youtube, url)) def is_valid_twitch_url(url): # This regular expression should match the following Twitch URL formats: # - https://twitch.tv/channel_name # - https://www.twitch.tv/channel_name # - https://twitch.tv/videos/video_id twitch_pattern = r'(https?://)?(www\.)?twitch\.tv/(videos/\d+|\w+)' return bool(re.match(twitch_pattern, url)) def is_valid_url(url): visible = True width = 600 height = 400 image = None # Youtube if "youtube" in url.lower() or "youtu.be" in url.lower(): if is_valid_youtube_url(url): thumbnail = get_youtube_thumbnail(url) if thumbnail: image = thumbnail else: image = "https://maximofn.com/wp-content/uploads/2023/11/youtube-no-thumbnails.webp" # Twitch elif "twitch" in url.lower() or "twitch.tv" in url.lower(): if is_valid_twitch_url(url): image = "https://maximofn.com/wp-content/uploads/2023/11/twitch.webp" # Error else: visible = False image = "https://maximofn.com/wp-content/uploads/2023/11/youtube_error.webp" return ( gr.Image(value=image, visible=visible, width=width, height=height), gr.Button(visible=visible), gr.Audio(visible=visible, show_download_button=False, show_share_button=False, container=True), gr.Video(visible=visible, show_share_button=False, container=True), ) def download_audio_and_video(url): audios_folder = "audios" videos_folder = "videos" if not os.path.exists(audios_folder): os.makedirs(audios_folder) if not os.path.exists(videos_folder): os.makedirs(videos_folder) python_file = "download.py" command = f"python {python_file} {url}" os.system(command) sleep(1) audio_path = "audios/download_audio.mp3" video_path = "videos/download_video.mp4" return ( gr.Textbox(value=audio_path), gr.Textbox(value=video_path), gr.Audio(audio_path, show_download_button=True, show_share_button=True, container=False), gr.Video(video_path, show_share_button=True, container=False), ) def video_downloader(): with gr.Blocks() as demo: # Layout gr.Markdown(html_social_media) gr.Markdown("