Spaces:
Sleeping
Sleeping
File size: 3,613 Bytes
5e107b7 9c69ffc 0f55252 6c44d5c 75778a1 8c9b09a 75778a1 8c9b09a 75778a1 b38db57 97b998b b38db57 6aab7cd 0f55252 75778a1 0f55252 9c69ffc 0f55252 97b998b 0f55252 97b998b 0f55252 97b998b 0f55252 97b998b 0f55252 97b998b 0f55252 97b998b 0f55252 97b998b 0f55252 97b998b 0f55252 97b998b 0f55252 97b998b 75778a1 0f55252 75778a1 97b998b b38db57 75778a1 d7bc426 9c69ffc 75778a1 9c69ffc 75778a1 6c44d5c 9c69ffc d7bc426 5e107b7 c0e2e5e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | import os
import gradio as gr
import re
def extract_shortcode(url):
"""Extract Instagram shortcode from various URL formats"""
patterns = [
r'instagram\.com/p/([^/?]+)',
r'instagram\.com/reel/([^/?]+)',
r'instagram\.com/tv/([^/?]+)',
]
for pattern in patterns:
match = re.search(pattern, url)
if match:
return match.group(1)
return None
def download_reel_audio(url, output_folder="downloads"):
"""
Download audio from Instagram reel using instaloader
"""
try:
import instaloader
# Clean URL and create output directory
clean_url = url.split('?')[0].strip()
shortcode = extract_shortcode(clean_url)
if not shortcode:
return "Invalid Instagram URL", None
os.makedirs(output_folder, exist_ok=True)
# Create instaloader instance - download only what we need
L = instaloader.Instaloader(
download_pictures=False,
download_videos=False,
download_video_thumbnails=False,
download_geotags=False,
download_comments=False,
save_metadata=False,
compress_json=False
)
# Get post info
post = instaloader.Post.from_shortcode(L.context, shortcode)
if not post.is_video:
return "This post doesn't contain a video", None
# Get video URL directly
video_url = post.video_url
# Download video content
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15',
}
response = requests.get(video_url, headers=headers)
if response.status_code != 200:
return f"Failed to download video: {response.status_code}", None
# Save video temporarily
import tempfile
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
temp_video.write(response.content)
temp_video_path = temp_video.name
# Extract audio using ffmpeg
audio_filename = f"instagram_audio_{shortcode}.mp3"
audio_path = os.path.join(output_folder, audio_filename)
import subprocess
cmd = [
'ffmpeg', '-i', temp_video_path,
'-vn', '-acodec', 'mp3', '-ab', '192k',
'-y', audio_path
]
result = subprocess.run(cmd, capture_output=True, text=True)
# Clean up temp file
os.unlink(temp_video_path)
if result.returncode != 0:
return f"Audio extraction failed: {result.stderr}", None
return "Audio downloaded successfully!", audio_path
except ImportError:
return "Please install: pip install instaloader requests", None
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() |