Spaces:
Build error
Build error
Download original youtube audio
Browse files- assets/twitch.webp +0 -0
- create conda environment.md +1 -1
- icons/copy.svg +5 -0
- icons/delete.svg +4 -0
- requirements.txt +2 -0
- translatube.py +88 -4
assets/twitch.webp
ADDED
|
create conda environment.md
CHANGED
|
@@ -17,7 +17,7 @@ pip install huggingface-hub
|
|
| 17 |
pip install torch
|
| 18 |
pip install torchaudio
|
| 19 |
<!-- pip install pysndfile==1.0.0 -->
|
| 20 |
-
|
| 21 |
|
| 22 |
# Download videos
|
| 23 |
pip install twitch-dl
|
|
|
|
| 17 |
pip install torch
|
| 18 |
pip install torchaudio
|
| 19 |
<!-- pip install pysndfile==1.0.0 -->
|
| 20 |
+
mamba install -y -c conda-forge libsndfile==1.0.31 pyperclip
|
| 21 |
|
| 22 |
# Download videos
|
| 23 |
pip install twitch-dl
|
icons/copy.svg
ADDED
|
|
icons/delete.svg
ADDED
|
|
requirements.txt
CHANGED
|
@@ -4,6 +4,8 @@ gradio
|
|
| 4 |
huggingface_hub
|
| 5 |
torch
|
| 6 |
torchaudio
|
|
|
|
| 7 |
# pysndfile
|
| 8 |
twitch-dl
|
| 9 |
pytube
|
|
|
|
|
|
| 4 |
huggingface_hub
|
| 5 |
torch
|
| 6 |
torchaudio
|
| 7 |
+
libsndfile==1.0.31
|
| 8 |
# pysndfile
|
| 9 |
twitch-dl
|
| 10 |
pytube
|
| 11 |
+
pyperclip
|
translatube.py
CHANGED
|
@@ -1,8 +1,92 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
|
| 8 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pyperclip
|
| 3 |
+
import urllib.parse as urlparse
|
| 4 |
+
from pytube import YouTube
|
| 5 |
|
| 6 |
+
YOUTUBE = "youtube"
|
| 7 |
+
TWITCH = "twitch"
|
| 8 |
+
|
| 9 |
+
def copy_url_from_clipboard():
|
| 10 |
+
return pyperclip.paste()
|
| 11 |
+
|
| 12 |
+
def clear_video_url():
|
| 13 |
+
return ""
|
| 14 |
+
|
| 15 |
+
def get_youtube_thumbnail(video_id):
|
| 16 |
+
thumbnail_url = f"https://img.youtube.com/vi/{video_id}/0.jpg"
|
| 17 |
+
return thumbnail_url
|
| 18 |
+
|
| 19 |
+
def get_youtube_video_id(url):
|
| 20 |
+
parsed_url = urlparse.urlparse(url)
|
| 21 |
+
video_id = urlparse.parse_qs(parsed_url.query).get('v')
|
| 22 |
+
if video_id:
|
| 23 |
+
thumbnail_url = get_youtube_thumbnail(video_id[0])
|
| 24 |
+
return thumbnail_url
|
| 25 |
+
else:
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
def is_valid_url(url):
|
| 29 |
+
button = gr.Button(size="sm", value="translate", min_width="10px", scale=0, visible=True)
|
| 30 |
+
original_audio = gr.Audio(label="Original audio", elem_id="original_audio", visible=True)
|
| 31 |
+
translated_audio = gr.Audio(label="Translated audio", elem_id="translated_audio", visible=True)
|
| 32 |
+
if "youtube" in url.lower() or "youtu.be" in url.lower():
|
| 33 |
+
thumbnail = get_youtube_video_id(url)
|
| 34 |
+
if thumbnail:
|
| 35 |
+
return (
|
| 36 |
+
gr.Image(value=thumbnail, visible=True, show_download_button=False, container=False),
|
| 37 |
+
button,
|
| 38 |
+
gr.Textbox(value=YOUTUBE, label="Stream page", elem_id="stream_page", visible=False),
|
| 39 |
+
original_audio,
|
| 40 |
+
translated_audio,
|
| 41 |
+
)
|
| 42 |
+
elif "twitch" in url.lower() or "twitch.tv" in url.lower():
|
| 43 |
+
return (
|
| 44 |
+
gr.Image(value="assets/twitch.webp", visible=True),
|
| 45 |
+
button,
|
| 46 |
+
gr.Textbox(value=TWITCH, label="Stream page", elem_id="stream_page", visible=False),
|
| 47 |
+
original_audio,
|
| 48 |
+
translated_audio,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
def get_audio_from_video(url, stream_page):
|
| 52 |
+
if stream_page == YOUTUBE:
|
| 53 |
+
yt = YouTube(url)
|
| 54 |
+
audio_streams = yt.streams.filter(mime_type="audio/mp4")
|
| 55 |
+
|
| 56 |
+
# Get all available audio bitrates
|
| 57 |
+
abr_list = []
|
| 58 |
+
for stream in audio_streams:
|
| 59 |
+
abr_list.append(stream.abr)
|
| 60 |
+
abr_list = sorted(set(abr_list))
|
| 61 |
+
|
| 62 |
+
# Get the highest audio bitrate
|
| 63 |
+
audio_stream = audio_streams.filter(abr=abr_list[0]).first()
|
| 64 |
+
|
| 65 |
+
# Download the audio
|
| 66 |
+
audio_stream.download(filename="audio.mp3")
|
| 67 |
+
|
| 68 |
+
return gr.Audio("audio.mp3", label="Original audio", elem_id="original_audio", visible=True)
|
| 69 |
+
elif stream_page == TWITCH:
|
| 70 |
+
pass
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
with gr.Row(variant="panel"):
|
| 75 |
+
url_textbox = gr.Textbox(placeholder="Add video URL here", label="Video URL", elem_id="video_url", scale=1, interactive=True)
|
| 76 |
+
copy_button = gr.Button(size="sm", icon="icons/copy.svg", value="", min_width="10px", scale=0)
|
| 77 |
+
delete_button = gr.Button(size="sm", icon="icons/delete.svg", value="", min_width="10px", scale=0)
|
| 78 |
+
copy_button.click(fn=copy_url_from_clipboard, outputs=url_textbox)
|
| 79 |
+
delete_button.click(fn=clear_video_url, outputs=url_textbox)
|
| 80 |
+
|
| 81 |
+
stream_page = gr.Textbox(label="Stream page", elem_id="stream_page", visible=False)
|
| 82 |
+
visible = False
|
| 83 |
+
with gr.Row(equal_height=False):
|
| 84 |
+
image = gr.Image(visible=visible, scale=1)
|
| 85 |
+
translate_button = gr.Button(size="sm", value="translate", min_width="10px", scale=0, visible=visible)
|
| 86 |
+
|
| 87 |
+
original_audio = gr.Audio(label="Original audio", elem_id="original_audio", visible=visible)
|
| 88 |
+
translated_audio = gr.Audio(label="Translated audio", elem_id="translated_audio", visible=visible)
|
| 89 |
+
url_textbox.change(fn=is_valid_url, inputs=url_textbox, outputs=[image, translate_button, stream_page, original_audio, translated_audio])
|
| 90 |
+
translate_button.click(fn=get_audio_from_video, inputs=[url_textbox, stream_page], outputs=original_audio)
|
| 91 |
|
|
|
|
|
|
|
| 92 |
demo.launch()
|