import os import gradio as gr from yt_dlp import YoutubeDL from pydantic import BaseModel class Config(BaseModel): class Config: arbitrary_types_allowed = True def download_reel_audio(url, output_folder="downloads"): """ Download audio from Instagram reel using yt-dlp """ try: # Ensure output folder exists os.makedirs(output_folder, exist_ok=True) # Configure yt-dlp options ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': os.path.join(output_folder, '%(title)s.%(ext)s'), 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }], 'quiet': True, 'no_warnings': True } # Download the audio with YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(url, download=True) audio_path = os.path.join(output_folder, f"{info['title']}.mp3") return "Audio downloaded successfully!", audio_path except Exception as e: return f"Error downloading audio: {str(e)}", None # Gradio Interface interface = gr.Interface( fn=download_reel_audio, inputs=gr.Textbox( label="Instagram Reel URL", placeholder="Enter the Instagram reel URL here (e.g., https://www.instagram.com/reel/...)" ), outputs=[ gr.Textbox(label="Status"), gr.Audio(label="Downloaded Audio") ], title="Instagram Reel to Audio Downloader", description=""" Enter the URL of an Instagram reel to download its audio as an MP3 file. This tool uses yt-dlp which is more reliable and doesn't require authentication. """, theme="default", examples=[ ["https://www.instagram.com/reel/example1"], ["https://www.instagram.com/reel/example2"] ] ) if __name__ == "__main__": interface.launch()