sheikhed commited on
Commit
9c69ffc
·
verified ·
1 Parent(s): a9cf04c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -61
app.py CHANGED
@@ -1,74 +1,72 @@
1
- import gradio as gr
2
- import yt_dlp
3
  import os
 
 
 
4
 
5
- def download_video_as_mp3(video_url, output_dir="downloads"):
6
- """
7
- Downloads a YouTube video as an MP3 file using yt-dlp.
8
-
9
- :param video_url: The URL of the YouTube video.
10
- :param output_dir: Directory where the MP3 file will be saved.
11
- :return: A tuple containing the filepath of the downloaded MP3 and a status message.
12
- Returns (None, error_message) if an error occurred.
13
- """
14
- if not os.path.exists(output_dir):
15
- os.makedirs(output_dir)
16
-
17
- ydl_opts = {
18
- 'format': 'bestaudio/best',
19
- 'postprocessors': [{
20
- 'key': 'FFmpegExtractAudio',
21
- 'preferredcodec': 'mp3',
22
- 'preferredquality': '192',
23
- }],
24
- 'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'),
25
- 'quiet': True, # Set to True for cleaner Gradio output, remove if you want yt-dlp's progress in terminal
26
- 'noplaylist': True,
27
- }
28
 
29
  try:
30
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
31
- info_dict = ydl.extract_info(video_url, download=True)
32
- video_title = info_dict.get('title', 'video')
33
- output_filepath = os.path.join(output_dir, f"{video_title}.mp3")
34
- status_message = "Download complete!"
35
- return output_filepath, status_message
36
 
37
- except Exception as e:
38
- error_message = f"An error occurred: {e}"
39
- return None, error_message
40
 
 
 
41
 
42
- def gradio_interface(video_url):
43
- """
44
- Gradio interface function to download YouTube video as MP3.
45
-
46
- :param video_url: YouTube video URL from Gradio Textbox.
47
- :return: A tuple containing the filepath of the downloaded MP3 (for Gradio File output) and status message (for Gradio Textbox).
48
- """
49
- if not video_url:
50
- return None, "Please enter a YouTube video URL."
51
 
52
- output_directory = "downloads" # Default output directory
 
 
 
 
53
 
54
- filepath, status_message = download_video_as_mp3(video_url, output_directory)
 
 
 
 
 
 
55
 
56
- return filepath, status_message
 
 
 
 
 
 
 
 
 
 
 
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
 
59
  if __name__ == "__main__":
60
- iface = gr.Interface(
61
- fn=gradio_interface,
62
- inputs=gr.Textbox(label="YouTube Video URL", placeholder="Enter YouTube URL here"),
63
- outputs=[
64
- gr.File(label="Downloaded MP3 File"),
65
- gr.Textbox(label="Status"),
66
- ],
67
- title="YouTube to MP3 Downloader",
68
- description="Enter a YouTube video URL and click 'Submit' to download it as MP3. The file will be saved in the 'downloads' directory.",
69
- examples=[
70
- ["https://www.youtube.com/watch?v=dQw4w9WgXcQ"],
71
- ["https://www.youtube.com/watch?v=your_video_id"],
72
- ]
73
- )
74
- iface.launch()
 
 
 
1
  import os
2
+ import instaloader
3
+ from moviepy.editor import VideoFileClip
4
+ import gradio as gr
5
 
6
+ # Function to download Instagram reel
7
+ def download_instagram_reel(url, output_folder="downloads"):
8
+ loader = instaloader.Instaloader()
9
+ loader.download_video_thumbnails = False # Disable thumbnail downloads
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  try:
12
+ # Extract shortcode from URL
13
+ shortcode = url.split("/")[-2]
 
 
 
 
14
 
15
+ # Ensure output folder exists
16
+ os.makedirs(output_folder, exist_ok=True)
 
17
 
18
+ # Download reel
19
+ loader.download_post(instaloader.Post.from_shortcode(loader.context, shortcode), target=output_folder)
20
 
21
+ # Find downloaded video file
22
+ for file in os.listdir(output_folder):
23
+ if file.endswith(".mp4"):
24
+ return os.path.join(output_folder, file)
25
+ return None
26
+ except Exception as e:
27
+ return f"Error downloading reel: {e}"
 
 
28
 
29
+ # Function to extract audio from video
30
+ def extract_audio(video_path, output_folder="downloads"):
31
+ try:
32
+ # Ensure output folder exists
33
+ os.makedirs(output_folder, exist_ok=True)
34
 
35
+ # Extract audio
36
+ video = VideoFileClip(video_path)
37
+ audio_path = os.path.join(output_folder, os.path.splitext(os.path.basename(video_path))[0] + ".mp3")
38
+ video.audio.write_audiofile(audio_path)
39
+ return audio_path
40
+ except Exception as e:
41
+ return f"Error extracting audio: {e}"
42
 
43
+ # Gradio interface function
44
+ def process_reel(url):
45
+ # Download reel
46
+ video_path = download_instagram_reel(url)
47
+ if isinstance(video_path, str) and video_path.endswith(".mp4"):
48
+ # Extract audio
49
+ audio_path = extract_audio(video_path)
50
+ if isinstance(audio_path, str) and audio_path.endswith(".mp3"):
51
+ return "Audio extracted successfully!", audio_path
52
+ else:
53
+ return "Failed to extract audio.", None
54
+ else:
55
+ return "Failed to download reel.", None
56
 
57
+ # Gradio Interface
58
+ interface = gr.Interface(
59
+ fn=process_reel,
60
+ inputs=gr.Textbox(label="Instagram Reel URL", placeholder="Enter the Instagram reel URL here"),
61
+ outputs=[
62
+ gr.Textbox(label="Status"),
63
+ gr.Audio(label="Downloaded Audio")
64
+ ],
65
+ title="Instagram Reel to Audio Downloader",
66
+ description="Enter the URL of an Instagram reel to download the audio as an MP3 file.",
67
+ theme="compact"
68
+ )
69
 
70
+ # Launch the interface
71
  if __name__ == "__main__":
72
+ interface.launch()