File size: 3,225 Bytes
5e107b7
9c69ffc
b38db57
cb503aa
6c44d5c
 
cb503aa
 
 
 
ea5cc73
6c44d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b38db57
 
6c44d5c
b38db57
6aab7cd
9c69ffc
 
c0e2e5e
6c44d5c
 
 
 
 
b38db57
 
 
 
 
 
 
 
 
 
6c44d5c
 
b38db57
 
 
 
 
 
6c44d5c
 
 
 
 
 
 
b38db57
d7bc426
b38db57
6c44d5c
 
 
 
 
 
b38db57
d7bc426
9c69ffc
 
b38db57
 
 
 
 
9c69ffc
 
 
 
 
b38db57
 
6c44d5c
b38db57
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
import os
import gradio as gr
from yt_dlp import YoutubeDL
from pydantic import BaseModel
import browser_cookie3
import tempfile

class Config(BaseModel):
    class Config:
        arbitrary_types_allowed = True

def get_instagram_cookies():
    """
    Get Instagram cookies from the browser
    """
    try:
        # Try Chrome first
        cookies = browser_cookie3.chrome(domain_name='.instagram.com')
    except:
        try:
            # Try Firefox if Chrome fails
            cookies = browser_cookie3.firefox(domain_name='.instagram.com')
        except:
            return None
    
    # Create a temporary cookie file
    cookie_file = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
    with open(cookie_file.name, 'w') as f:
        for cookie in cookies:
            f.write(f'.instagram.com\tTRUE\t/\tFALSE\t{cookie.expires}\t{cookie.name}\t{cookie.value}\n')
    
    return cookie_file.name

def download_reel_audio(url, output_folder="downloads"):
    """
    Download audio from Instagram reel using yt-dlp with browser cookies
    """
    try:
        # Ensure output folder exists
        os.makedirs(output_folder, exist_ok=True)
        
        # Get cookies from browser
        cookie_file = get_instagram_cookies()
        if not cookie_file:
            return "Error: Could not get Instagram cookies from browser. Please ensure you're logged into Instagram in Chrome or Firefox.", None
        
        # 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,
            'cookiefile': cookie_file
        }

        # 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")
            
            # Clean up cookie file
            try:
                os.unlink(cookie_file)
            except:
                pass
                
            return "Audio downloaded successfully!", audio_path

    except Exception as e:
        # Clean up cookie file in case of error
        try:
            if cookie_file:
                os.unlink(cookie_file)
        except:
            pass
        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.
    Note: You must be logged into Instagram in Chrome or Firefox browser for this to work.
    """,
    theme="default"
)

if __name__ == "__main__":
    interface.launch()