sheikhed commited on
Commit
6c44d5c
·
verified ·
1 Parent(s): 6cbba8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -8
app.py CHANGED
@@ -2,19 +2,48 @@ import os
2
  import gradio as gr
3
  from yt_dlp import YoutubeDL
4
  from pydantic import BaseModel
 
 
5
 
6
  class Config(BaseModel):
7
  class Config:
8
  arbitrary_types_allowed = True
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def download_reel_audio(url, output_folder="downloads"):
11
  """
12
- Download audio from Instagram reel using yt-dlp
13
  """
14
  try:
15
  # Ensure output folder exists
16
  os.makedirs(output_folder, exist_ok=True)
17
 
 
 
 
 
 
18
  # Configure yt-dlp options
19
  ydl_opts = {
20
  'format': 'bestaudio/best',
@@ -25,16 +54,30 @@ def download_reel_audio(url, output_folder="downloads"):
25
  'preferredquality': '192',
26
  }],
27
  'quiet': True,
28
- 'no_warnings': True
 
29
  }
30
 
31
  # Download the audio
32
  with YoutubeDL(ydl_opts) as ydl:
33
  info = ydl.extract_info(url, download=True)
34
  audio_path = os.path.join(output_folder, f"{info['title']}.mp3")
 
 
 
 
 
 
 
35
  return "Audio downloaded successfully!", audio_path
36
 
37
  except Exception as e:
 
 
 
 
 
 
38
  return f"Error downloading audio: {str(e)}", None
39
 
40
  # Gradio Interface
@@ -51,13 +94,9 @@ interface = gr.Interface(
51
  title="Instagram Reel to Audio Downloader",
52
  description="""
53
  Enter the URL of an Instagram reel to download its audio as an MP3 file.
54
- This tool uses yt-dlp which is more reliable and doesn't require authentication.
55
  """,
56
- theme="default",
57
- examples=[
58
- ["https://www.instagram.com/reel/example1"],
59
- ["https://www.instagram.com/reel/example2"]
60
- ]
61
  )
62
 
63
  if __name__ == "__main__":
 
2
  import gradio as gr
3
  from yt_dlp import YoutubeDL
4
  from pydantic import BaseModel
5
+ import browser_cookie3
6
+ import tempfile
7
 
8
  class Config(BaseModel):
9
  class Config:
10
  arbitrary_types_allowed = True
11
 
12
+ def get_instagram_cookies():
13
+ """
14
+ Get Instagram cookies from the browser
15
+ """
16
+ try:
17
+ # Try Chrome first
18
+ cookies = browser_cookie3.chrome(domain_name='.instagram.com')
19
+ except:
20
+ try:
21
+ # Try Firefox if Chrome fails
22
+ cookies = browser_cookie3.firefox(domain_name='.instagram.com')
23
+ except:
24
+ return None
25
+
26
+ # Create a temporary cookie file
27
+ cookie_file = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
28
+ with open(cookie_file.name, 'w') as f:
29
+ for cookie in cookies:
30
+ f.write(f'.instagram.com\tTRUE\t/\tFALSE\t{cookie.expires}\t{cookie.name}\t{cookie.value}\n')
31
+
32
+ return cookie_file.name
33
+
34
  def download_reel_audio(url, output_folder="downloads"):
35
  """
36
+ Download audio from Instagram reel using yt-dlp with browser cookies
37
  """
38
  try:
39
  # Ensure output folder exists
40
  os.makedirs(output_folder, exist_ok=True)
41
 
42
+ # Get cookies from browser
43
+ cookie_file = get_instagram_cookies()
44
+ if not cookie_file:
45
+ return "Error: Could not get Instagram cookies from browser. Please ensure you're logged into Instagram in Chrome or Firefox.", None
46
+
47
  # Configure yt-dlp options
48
  ydl_opts = {
49
  'format': 'bestaudio/best',
 
54
  'preferredquality': '192',
55
  }],
56
  'quiet': True,
57
+ 'no_warnings': True,
58
+ 'cookiefile': cookie_file
59
  }
60
 
61
  # Download the audio
62
  with YoutubeDL(ydl_opts) as ydl:
63
  info = ydl.extract_info(url, download=True)
64
  audio_path = os.path.join(output_folder, f"{info['title']}.mp3")
65
+
66
+ # Clean up cookie file
67
+ try:
68
+ os.unlink(cookie_file)
69
+ except:
70
+ pass
71
+
72
  return "Audio downloaded successfully!", audio_path
73
 
74
  except Exception as e:
75
+ # Clean up cookie file in case of error
76
+ try:
77
+ if cookie_file:
78
+ os.unlink(cookie_file)
79
+ except:
80
+ pass
81
  return f"Error downloading audio: {str(e)}", None
82
 
83
  # Gradio Interface
 
94
  title="Instagram Reel to Audio Downloader",
95
  description="""
96
  Enter the URL of an Instagram reel to download its audio as an MP3 file.
97
+ Note: You must be logged into Instagram in Chrome or Firefox browser for this to work.
98
  """,
99
+ theme="default"
 
 
 
 
100
  )
101
 
102
  if __name__ == "__main__":