audio-trim / app.py
sheikhed's picture
Update app.py
0880b7d verified
import os
import gradio as gr
from yt_dlp import YoutubeDL
def download_reel_audio(url, output_folder="downloads"):
"""
Download audio from Instagram reel using yt-dlp without requiring cookies
"""
try:
# Clean URL and create output directory
clean_url = url.split('?')[0].strip()
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',
}],
'restrictfilenames': True,
'quiet': True,
'headers': {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1',
'Referer': 'https://www.instagram.com/',
'Accept-Language': 'en-US,en;q=0.9',
}
}
with YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(clean_url, download=True)
base_path = ydl.prepare_filename(info)
audio_path = os.path.splitext(base_path)[0] + '.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 public Instagram reel URL (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 URL of a public Instagram reel to download its audio as MP3",
theme="default"
)
if __name__ == "__main__":
interface.launch()