song download 13th sep 26 working
# Step 1: Install dependencies (yt-dlp, pydub, and system ffmpeg)
!pip install -q -U yt-dlp pydub
!apt-get install -y ffmpeg > /dev/null 2>&1
import os
from google.colab import files
from pydub import AudioSegment
import yt_dlp
# Step 2: Add your YouTube Shorts URLs here
urls = [
"https://youtube.com/shorts/93fVhn248UQ?si=48VqCgZLIKOdyCqM",
"https://youtube.com/shorts/P9U7uqcOFBg?si=PFS3GdcrlvfHdF_6",
"https://youtube.com/shorts/MxoonR572Bo?si=UoswrYNwF-IMMqPK",
"https://youtube.com/shorts/MCGByoxmjWY?si=P67BYqZhB6QgXQkV"
]
# Output path requested
OUTPUT_FILE = "/content/out.mp3"
TEMP_DIR = "/content/temp_audio"
os.makedirs(TEMP_DIR, exist_ok=True)
# Step 3: Configure yt-dlp options for MP3 extraction
ydl_opts = {
"format": "bestaudio/best",
"outtmpl": os.path.join(TEMP_DIR, "%(autonumber)02d_%(title)s.%(ext)s"),
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "192",
}
],
"quiet": False,
}
# Step 4: Download all YouTube Shorts as MP3s
print("Downloading Youtube Shorts audio...")
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download(urls)
# Step 5: Gather and sort downloaded MP3 files
downloaded_files = sorted(
[
os.path.join(TEMP_DIR, f)
for f in os.listdir(TEMP_DIR)
if f.endswith(".mp3")
]
)
# Step 6: Merge audio tracks using Pydub
print("\nMerging audio files...")
combined_audio = AudioSegment.empty()
for file_path in downloaded_files:
print(f" Concatenating: {os.path.basename(file_path)}")
segment = AudioSegment.from_file(file_path, format="mp3")
combined_audio += segment
# Step 7: Export to /content/out.mp3
combined_audio.export(OUTPUT_FILE, format="mp3")
print(f"\nSuccessfully merged into: {OUTPUT_FILE}")
# Step 8: Trigger file download in Google Colab
files.download(OUTPUT_FILE)