File size: 2,599 Bytes
5e107b7
9c69ffc
 
 
cb503aa
 
 
 
 
 
ea5cc73
9c69ffc
 
 
 
ea5cc73
6aab7cd
9c69ffc
 
6aab7cd
9c69ffc
 
6aab7cd
9c69ffc
 
6aab7cd
9c69ffc
 
 
 
 
 
 
6aab7cd
9c69ffc
 
 
 
 
6aab7cd
9c69ffc
 
 
 
 
 
 
d7bc426
9c69ffc
 
 
 
 
 
 
 
 
 
 
 
 
d7bc426
9c69ffc
 
 
 
 
 
 
 
 
 
 
 
d7bc426
9c69ffc
5e107b7
9c69ffc
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
import os
import instaloader
from moviepy.editor import VideoFileClip
import gradio as gr
from pydantic import BaseModel

# Custom Pydantic Config to avoid schema generation issues
class Config(BaseModel):
    class Config:
        arbitrary_types_allowed = True

# Function to download Instagram reel
def download_instagram_reel(url, output_folder="downloads"):
    loader = instaloader.Instaloader()
    loader.download_video_thumbnails = False  # Disable thumbnail downloads

    try:
        # Extract shortcode from URL
        shortcode = url.split("/")[-2]

        # Ensure output folder exists
        os.makedirs(output_folder, exist_ok=True)

        # Download reel
        loader.download_post(instaloader.Post.from_shortcode(loader.context, shortcode), target=output_folder)

        # Find downloaded video file
        for file in os.listdir(output_folder):
            if file.endswith(".mp4"):
                return os.path.join(output_folder, file)
        return None
    except Exception as e:
        return f"Error downloading reel: {e}"

# Function to extract audio from video
def extract_audio(video_path, output_folder="downloads"):
    try:
        # Ensure output folder exists
        os.makedirs(output_folder, exist_ok=True)

        # Extract audio
        video = VideoFileClip(video_path)
        audio_path = os.path.join(output_folder, os.path.splitext(os.path.basename(video_path))[0] + ".mp3")
        video.audio.write_audiofile(audio_path)
        return audio_path
    except Exception as e:
        return f"Error extracting audio: {e}"

# Gradio interface function
def process_reel(url):
    # Download reel
    video_path = download_instagram_reel(url)
    if isinstance(video_path, str) and video_path.endswith(".mp4"):
        # Extract audio
        audio_path = extract_audio(video_path)
        if isinstance(audio_path, str) and audio_path.endswith(".mp3"):
            return "Audio extracted successfully!", audio_path
        else:
            return "Failed to extract audio.", None
    else:
        return "Failed to download reel.", None

# Gradio Interface
interface = gr.Interface(
    fn=process_reel,
    inputs=gr.Textbox(label="Instagram Reel URL", placeholder="Enter the Instagram reel URL here"),
    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 the audio as an MP3 file.",
    theme="compact"
)

# Launch the interface
if __name__ == "__main__":
    interface.launch()