Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,72 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import yt_dlp
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
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 |
-
|
31 |
-
|
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 |
-
|
38 |
-
|
39 |
-
return None, error_message
|
40 |
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
if not video_url:
|
50 |
-
return None, "Please enter a YouTube video URL."
|
51 |
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
|
|
59 |
if __name__ == "__main__":
|
60 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|