Spaces:
No application file
No application file
Commit ·
176c6db
1
Parent(s): 8a51e17
Upload folder using huggingface_hub
Browse files- 00-02_youtubeAudio.py +23 -0
- 00-03_youtube2audioGradio.py +46 -0
- README.md +3 -9
00-02_youtubeAudio.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pytube import YouTube
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
# Time string for unique filenames
|
| 5 |
+
timestr = time.strftime("%Y%m%d-%H%M%S")
|
| 6 |
+
|
| 7 |
+
# File name with embedded timestamp
|
| 8 |
+
filename = f"Audio_{timestr}.mp3"
|
| 9 |
+
|
| 10 |
+
# The original video URL
|
| 11 |
+
url = "https://www.youtube.com/watch?v=GF3LotteFFo"
|
| 12 |
+
|
| 13 |
+
# Create a YouTube object
|
| 14 |
+
yt = YouTube(url)
|
| 15 |
+
|
| 16 |
+
# Get the highest quality audio-only stream and download it
|
| 17 |
+
stream = yt.streams.get_audio_only("mp4") # You may also use 'webm' depending on the available audio formats
|
| 18 |
+
|
| 19 |
+
# Define download path
|
| 20 |
+
output_path = './' # Current directory
|
| 21 |
+
|
| 22 |
+
# Download the stream
|
| 23 |
+
stream.download(output_path=output_path, filename=filename)
|
00-03_youtube2audioGradio.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pytube import YouTube
|
| 3 |
+
import time
|
| 4 |
+
from urllib.parse import urlparse, parse_qs
|
| 5 |
+
|
| 6 |
+
def get_youtube_object(url):
|
| 7 |
+
try:
|
| 8 |
+
yt = YouTube(url)
|
| 9 |
+
return yt
|
| 10 |
+
except Exception as e:
|
| 11 |
+
print('Failed to process video. Please check your URL or try again later.')
|
| 12 |
+
return None # Return None if there is an exception
|
| 13 |
+
|
| 14 |
+
def download_audio_from_youtube_url(url: str) -> str:
|
| 15 |
+
# Taking input from the user
|
| 16 |
+
|
| 17 |
+
# Parsing the URL and extracting just the video ID
|
| 18 |
+
parsed_url = urlparse(url)
|
| 19 |
+
video_id = parse_qs(parsed_url.query).get("v")
|
| 20 |
+
|
| 21 |
+
if video_id:
|
| 22 |
+
# Constructing a new URL with just the relevant parameters
|
| 23 |
+
url = f"https://www.youtube.com/watch?v={video_id[0]}"
|
| 24 |
+
|
| 25 |
+
# Time string for unique filenames
|
| 26 |
+
timestr = time.strftime("%Y%m%d-%H%M%S")
|
| 27 |
+
filename = f"Audio_{timestr}.mp4"
|
| 28 |
+
|
| 29 |
+
# YouTube Audio download code
|
| 30 |
+
yt = get_youtube_object(url) # Use the function here
|
| 31 |
+
if yt is None: # If the YouTube object is None, stop the application here
|
| 32 |
+
return 'Failed to process video. Please check your URL or try again later.'
|
| 33 |
+
|
| 34 |
+
stream = yt.streams.get_audio_only("mp4") # You may also use 'webm' depending on the available audio formats
|
| 35 |
+
output_path = './' # Current directory
|
| 36 |
+
stream.download(output_path=output_path, filename=filename)
|
| 37 |
+
return 'Download Complete 🎉'
|
| 38 |
+
|
| 39 |
+
else:
|
| 40 |
+
return 'Invalid YouTube URL! Please check your URL.'
|
| 41 |
+
|
| 42 |
+
iface = gr.Interface(fn=download_audio_from_youtube_url,
|
| 43 |
+
inputs="text",
|
| 44 |
+
outputs="text")
|
| 45 |
+
|
| 46 |
+
iface.launch(share=True)
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: purple
|
| 5 |
-
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 3.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: y2t
|
| 3 |
+
app_file: 00-03_youtube2audioGradio.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
+
sdk_version: 3.42.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|