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2 |
+
from yt_dlp import YoutubeDL
|
| 3 |
+
import os
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
+
|
| 6 |
+
DOWNLOADS_FOLDER = "downloads"
|
| 7 |
+
os.makedirs(DOWNLOADS_FOLDER, exist_ok=True)
|
| 8 |
+
|
| 9 |
+
def download_audio(url, file_format, duration_sec):
|
| 10 |
+
"""
|
| 11 |
+
Download audio from YouTube or SoundCloud and convert to desired format
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
# Download best audio available
|
| 15 |
+
ydl_opts = {
|
| 16 |
+
'format': 'bestaudio/best',
|
| 17 |
+
'outtmpl': os.path.join(DOWNLOADS_FOLDER, '%(title)s.%(ext)s'),
|
| 18 |
+
'quiet': True,
|
| 19 |
+
'no_warnings': True,
|
| 20 |
+
'extract_flat': False,
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
with YoutubeDL(ydl_opts) as ydl:
|
| 24 |
+
info = ydl.extract_info(url, download=True)
|
| 25 |
+
|
| 26 |
+
original_file = os.path.join(DOWNLOADS_FOLDER, f"{info['title']}.{info['ext']}")
|
| 27 |
+
|
| 28 |
+
# Clean filename (remove special characters)
|
| 29 |
+
safe_title = "".join(c for c in info['title'] if c.isalnum() or c in (' ', '-', '_')).rstrip()
|
| 30 |
+
original_file_safe = os.path.join(DOWNLOADS_FOLDER, f"{safe_title}.{info['ext']}")
|
| 31 |
+
|
| 32 |
+
# Rename if necessary
|
| 33 |
+
if original_file != original_file_safe:
|
| 34 |
+
os.rename(original_file, original_file_safe)
|
| 35 |
+
original_file = original_file_safe
|
| 36 |
+
|
| 37 |
+
# Load audio and trim to duration
|
| 38 |
+
audio = AudioSegment.from_file(original_file)
|
| 39 |
+
duration_ms = min(len(audio), int(duration_sec * 1000))
|
| 40 |
+
trimmed_audio = audio[:duration_ms]
|
| 41 |
+
|
| 42 |
+
# Determine output file path based on desired format
|
| 43 |
+
base_name = os.path.splitext(original_file)[0]
|
| 44 |
+
|
| 45 |
+
if file_format.lower() == "mp3":
|
| 46 |
+
output_file = base_name + ".mp3"
|
| 47 |
+
trimmed_audio.export(output_file, format="mp3", bitrate="192k")
|
| 48 |
+
|
| 49 |
+
elif file_format.lower() == "opus":
|
| 50 |
+
output_file = base_name + ".opus"
|
| 51 |
+
trimmed_audio.export(output_file, format="opus", bitrate="128k")
|
| 52 |
+
|
| 53 |
+
elif file_format.lower() == "wav":
|
| 54 |
+
output_file = base_name + ".wav"
|
| 55 |
+
trimmed_audio.export(output_file, format="wav")
|
| 56 |
+
|
| 57 |
+
elif file_format.lower() == "m4a":
|
| 58 |
+
output_file = base_name + ".m4a"
|
| 59 |
+
trimmed_audio.export(output_file, format="ipod", codec="aac")
|
| 60 |
+
|
| 61 |
+
else: # keep original format
|
| 62 |
+
output_file = original_file
|
| 63 |
+
|
| 64 |
+
# Optional: Remove original downloaded file to save space
|
| 65 |
+
if output_file != original_file and os.path.exists(original_file):
|
| 66 |
+
os.remove(original_file)
|
| 67 |
+
|
| 68 |
+
return output_file
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
raise gr.Error(f"Error downloading audio: {str(e)}")
|
| 72 |
+
|
| 73 |
+
# Create Gradio interface
|
| 74 |
+
with gr.Blocks(title="Audio Downloader", theme=gr.themes.Soft()) as iface:
|
| 75 |
+
gr.Markdown("# 🎵 YouTube & SoundCloud Audio Downloader")
|
| 76 |
+
gr.Markdown("Download audio from YouTube or SoundCloud and convert to your preferred format")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
url_input = gr.Textbox(
|
| 80 |
+
label="Enter URL",
|
| 81 |
+
placeholder="Paste YouTube or SoundCloud URL here...",
|
| 82 |
+
scale=4
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
with gr.Column(scale=1):
|
| 87 |
+
format_choice = gr.Dropdown(
|
| 88 |
+
choices=["mp3", "m4a", "opus", "wav"],
|
| 89 |
+
value="mp3",
|
| 90 |
+
label="Output Format"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
with gr.Column(scale=3):
|
| 94 |
+
duration_slider = gr.Slider(
|
| 95 |
+
minimum=5,
|
| 96 |
+
maximum=600, # Increased to 10 minutes
|
| 97 |
+
value=60,
|
| 98 |
+
step=5,
|
| 99 |
+
label="Duration (seconds)",
|
| 100 |
+
info="Maximum duration to download (0 = full track)"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
download_button = gr.Button("Download Audio", variant="primary", size="lg")
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
download_file = gr.File(label="Download your audio", interactive=False)
|
| 108 |
+
|
| 109 |
+
# Example URLs
|
| 110 |
+
with gr.Accordion("Example URLs (click to use)", open=False):
|
| 111 |
+
gr.Examples(
|
| 112 |
+
examples=[
|
| 113 |
+
["https://www.youtube.com/watch?v=dQw4w9WgXcQ"],
|
| 114 |
+
["https://soundcloud.com/antonio-antetomaso/mutiny-on-the-bounty-closing-titles-cover"],
|
| 115 |
+
["https://www.youtube.com/watch?v=JGwWNGJdvx8"],
|
| 116 |
+
["https://soundcloud.com/officialnikkig/kill-bill-sza-kill-bill-remix"]
|
| 117 |
+
],
|
| 118 |
+
inputs=url_input,
|
| 119 |
+
label=""
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# Add instructions
|
| 123 |
+
with gr.Accordion("Instructions", open=False):
|
| 124 |
+
gr.Markdown("""
|
| 125 |
+
### How to use:
|
| 126 |
+
1. Paste a YouTube or SoundCloud URL
|
| 127 |
+
2. Choose your desired output format
|
| 128 |
+
3. Set the maximum duration (or leave at 0 for full track)
|
| 129 |
+
4. Click "Download Audio"
|
| 130 |
+
|
| 131 |
+
### Supported formats:
|
| 132 |
+
- **MP3**: Most compatible, good quality
|
| 133 |
+
- **M4A**: Apple format, smaller file size
|
| 134 |
+
- **Opus**: Best quality at low bitrates
|
| 135 |
+
- **WAV**: Lossless, large file size
|
| 136 |
+
|
| 137 |
+
### Notes:
|
| 138 |
+
- Downloads are saved to a 'downloads' folder
|
| 139 |
+
- Some tracks may have download restrictions
|
| 140 |
+
- Very long videos may take time to process
|
| 141 |
+
""")
|
| 142 |
+
|
| 143 |
+
download_button.click(
|
| 144 |
+
fn=download_audio,
|
| 145 |
+
inputs=[url_input, format_choice, duration_slider],
|
| 146 |
+
outputs=download_file,
|
| 147 |
+
show_progress=True
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Launch the interface
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
iface.launch(
|
| 153 |
+
show_error=True,
|
| 154 |
+
share=False, # Set to True to create a public link
|
| 155 |
+
server_name="0.0.0.0" if os.getenv("GRADIO_SHARE") else "127.0.0.1",
|
| 156 |
+
server_port=7860
|
| 157 |
+
)
|