sheikhed commited on
Commit
c0e2e5e
·
verified ·
1 Parent(s): ad110b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -1,29 +1,23 @@
1
  import os
 
2
  import instaloader
3
- from moviepy.editor import VideoFileClip
4
  import gradio as gr
5
  from pydantic import BaseModel
6
 
7
- # Custom Pydantic Config to avoid schema generation issues
8
  class Config(BaseModel):
9
  class Config:
10
  arbitrary_types_allowed = True
11
 
12
- # Function to download Instagram reel
13
  def download_instagram_reel(url, output_folder="downloads"):
14
  loader = instaloader.Instaloader()
15
- loader.download_video_thumbnails = False # Disable thumbnail downloads
16
-
17
  try:
18
  # Extract shortcode from URL
19
  shortcode = url.split("/")[-2]
20
-
21
  # Ensure output folder exists
22
  os.makedirs(output_folder, exist_ok=True)
23
-
24
  # Download reel
25
  loader.download_post(instaloader.Post.from_shortcode(loader.context, shortcode), target=output_folder)
26
-
27
  # Find downloaded video file
28
  for file in os.listdir(output_folder):
29
  if file.endswith(".mp4"):
@@ -32,21 +26,39 @@ def download_instagram_reel(url, output_folder="downloads"):
32
  except Exception as e:
33
  return f"Error downloading reel: {e}"
34
 
35
- # Function to extract audio from video
36
  def extract_audio(video_path, output_folder="downloads"):
37
  try:
38
  # Ensure output folder exists
39
  os.makedirs(output_folder, exist_ok=True)
40
-
41
- # Extract audio
42
- video = VideoFileClip(video_path)
43
- audio_path = os.path.join(output_folder, os.path.splitext(os.path.basename(video_path))[0] + ".mp3")
44
- video.audio.write_audiofile(audio_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  return audio_path
46
  except Exception as e:
47
  return f"Error extracting audio: {e}"
48
 
49
- # Gradio interface function
50
  def process_reel(url):
51
  # Download reel
52
  video_path = download_instagram_reel(url)
@@ -54,16 +66,22 @@ def process_reel(url):
54
  # Extract audio
55
  audio_path = extract_audio(video_path)
56
  if isinstance(audio_path, str) and audio_path.endswith(".mp3"):
 
 
 
 
 
57
  return "Audio extracted successfully!", audio_path
58
  else:
59
- return "Failed to extract audio.", None
60
  else:
61
- return "Failed to download reel.", None
62
 
63
  # Gradio Interface
64
  interface = gr.Interface(
65
  fn=process_reel,
66
- inputs=gr.Textbox(label="Instagram Reel URL", placeholder="Enter the Instagram reel URL here"),
 
67
  outputs=[
68
  gr.Textbox(label="Status"),
69
  gr.Audio(label="Downloaded Audio")
@@ -73,6 +91,5 @@ interface = gr.Interface(
73
  theme="compact"
74
  )
75
 
76
- # Launch the interface
77
  if __name__ == "__main__":
78
- interface.launch()
 
1
  import os
2
+ import subprocess
3
  import instaloader
 
4
  import gradio as gr
5
  from pydantic import BaseModel
6
 
 
7
  class Config(BaseModel):
8
  class Config:
9
  arbitrary_types_allowed = True
10
 
 
11
  def download_instagram_reel(url, output_folder="downloads"):
12
  loader = instaloader.Instaloader()
13
+ loader.download_video_thumbnails = False
 
14
  try:
15
  # Extract shortcode from URL
16
  shortcode = url.split("/")[-2]
 
17
  # Ensure output folder exists
18
  os.makedirs(output_folder, exist_ok=True)
 
19
  # Download reel
20
  loader.download_post(instaloader.Post.from_shortcode(loader.context, shortcode), target=output_folder)
 
21
  # Find downloaded video file
22
  for file in os.listdir(output_folder):
23
  if file.endswith(".mp4"):
 
26
  except Exception as e:
27
  return f"Error downloading reel: {e}"
28
 
 
29
  def extract_audio(video_path, output_folder="downloads"):
30
  try:
31
  # Ensure output folder exists
32
  os.makedirs(output_folder, exist_ok=True)
33
+
34
+ # Generate output audio path
35
+ audio_path = os.path.join(output_folder,
36
+ os.path.splitext(os.path.basename(video_path))[0] + ".mp3")
37
+
38
+ # FFmpeg command to extract audio
39
+ command = [
40
+ 'ffmpeg',
41
+ '-i', video_path, # Input file
42
+ '-vn', # Disable video
43
+ '-acodec', 'libmp3lame', # Use MP3 codec
44
+ '-ab', '192k', # Audio bitrate
45
+ '-ar', '44100', # Audio sample rate
46
+ '-y', # Overwrite output file if it exists
47
+ audio_path
48
+ ]
49
+
50
+ # Run FFmpeg command
51
+ result = subprocess.run(command,
52
+ stdout=subprocess.PIPE,
53
+ stderr=subprocess.PIPE)
54
+
55
+ if result.returncode != 0:
56
+ return f"FFmpeg error: {result.stderr.decode()}"
57
+
58
  return audio_path
59
  except Exception as e:
60
  return f"Error extracting audio: {e}"
61
 
 
62
  def process_reel(url):
63
  # Download reel
64
  video_path = download_instagram_reel(url)
 
66
  # Extract audio
67
  audio_path = extract_audio(video_path)
68
  if isinstance(audio_path, str) and audio_path.endswith(".mp3"):
69
+ # Clean up video file after extraction
70
+ try:
71
+ os.remove(video_path)
72
+ except:
73
+ pass
74
  return "Audio extracted successfully!", audio_path
75
  else:
76
+ return f"Failed to extract audio: {audio_path}", None
77
  else:
78
+ return f"Failed to download reel: {video_path}", None
79
 
80
  # Gradio Interface
81
  interface = gr.Interface(
82
  fn=process_reel,
83
+ inputs=gr.Textbox(label="Instagram Reel URL",
84
+ placeholder="Enter the Instagram reel URL here"),
85
  outputs=[
86
  gr.Textbox(label="Status"),
87
  gr.Audio(label="Downloaded Audio")
 
91
  theme="compact"
92
  )
93
 
 
94
  if __name__ == "__main__":
95
+ interface.launch()