How to Download Videos and Audios from a Video Link (On macOS)


    Created by Bismoy Ghosh



---


1. Install Python (If Not Installed)


To use this script, you need Python installed on your macOS system.


Steps to Install Python:


1. Open Terminal and check if Python is installed:


python3 --version



2. If Python is not installed, download and install the latest version from:


https://www.python.org/downloads/mac-osx/



3. After installation, verify again:


python3 --version





---


2. Install Required Packages


Open Terminal and enter the following commands:


pip3 install yt-dlp

pip3 install flask

brew install ffmpeg # If Homebrew is installed


> Note: If Homebrew is not installed, install it using:


/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"





---


3. Create a Directory for the Project


1. Open Terminal and navigate to your preferred directory, e.g.:


cd ~/Desktop



2. Create a new folder:


mkdir downloader



3. Navigate to the folder:


cd downloader



4. Create a subfolder for templates:


mkdir templates





---


4. Create the Python Script


1. Open a text editor (e.g., VS Code, Nano, or TextEdit).



2. Copy and paste the following Python code:


from flask import Flask, request, render_template, send_file

import yt_dlp

import os


app = Flask(__name__)


DOWNLOAD_PATH = os.path.join(os.getcwd(), "downloads")


if not os.path.exists(DOWNLOAD_PATH):

    os.makedirs(DOWNLOAD_PATH)


def download_video(url, format_code):

    if format_code == "mp3":

        ydl_opts = {

            "format": "bestaudio/best",

            "outtmpl": f"{DOWNLOAD_PATH}/%(title)s.%(ext)s",

            "postprocessors": [{

                "key": "FFmpegExtractAudio",

                "preferredcodec": "mp3",

                "preferredquality": "192",

            }],

        }

    else:

        ydl_opts = {

            "format": f"bestvideo[height<={format_code}]+bestaudio/best",

            "outtmpl": f"{DOWNLOAD_PATH}/%(title)s.%(ext)s",

            "merge_output_format": "mp4",

            "noplaylist": True,

        }


    with yt_dlp.YoutubeDL(ydl_opts) as ydl:

        info = ydl.extract_info(url, download=True)

        filename = ydl.prepare_filename(info)


        if filename.endswith(".webm"):

            filename = filename.replace(".webm", ".mp4")

        return filename


@app.route("/", methods=["GET", "POST"])

def index():

    if request.method == "POST":

        url = request.form.get("url")

        format_code = request.form.get("format")

        try:

            file_path = download_video(url, format_code)

            return send_file(file_path, as_attachment=True)

        except Exception as e:

            return f"Error: {str(e)}"

    return render_template("index.html")


if __name__ == "__main__":

    app.run(host="0.0.0.0", port=5000, debug=True)



3. Save this file as server.py inside the downloader folder.





---


5. Create the HTML Frontend


1. Inside ~/Desktop/downloader/templates, create a new file called index.html.



2. Open it with any text editor and paste the following HTML code:


<!DOCTYPE html>

<html lang="en">

<head>

    <title>Video Downloader</title>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">

    <style>

        body {

            background: linear-gradient(135deg, #1f1c2c, #928dab);

            color: white;

            text-align: center;

            padding-top: 50px;

        }

        .container {

            max-width: 500px;

            background: rgba(0, 0, 0, 0.7);

            padding: 20px;

            border-radius: 10px;

            box-shadow: 0px 0px 10px rgba(255, 255, 255, 0.2);

        }

        input, select {

            margin-top: 10px;

            width: 100%;

            padding: 10px;

            border-radius: 5px;

            border: none;

        }

        button {

            margin-top: 15px;

            background: #ff4081;

            color: white;

            padding: 10px;

            border: none;

            width: 100%;

            font-size: 18px;

            cursor: pointer;

        }

        button:hover {

            background: #ff79b0;

        }

    </style>

</head>

<body>

    <div class="container">

        <h2><i class="fas fa-download"></i> Video Downloader</h2>

        <form method="POST">

            <label>Enter Video Link:</label>

            <input type="text" name="url" placeholder="Paste video link here..." required>

            <label>Select Format:</label>

            <select name="format">

                <option value="mp3">MP3 (Audio Only)</option>

                <option value="144">MP4 (144p)</option>

                <option value="360">MP4 (360p)</option>

                <option value="720">MP4 (720p)</option>

                <option value="1080">MP4 (1080p)</option>

            </select>

            <button type="submit"><i class="fas fa-download"></i> Download</button>

        </form>

    </div>

</body>

</html>



3. Save and close the file.





---


6. Run the Web App


1. Open Terminal and navigate to the project folder:


cd ~/Desktop/downloader



2. Start the Flask server:


python3 server.py



3. Open your browser and go to:


http://127.0.0.1:5000





---


7. Downloading Videos or Audio


1. Paste a video link (YouTube, Facebook, etc.).



2. Select the format (MP3, 144p, 360p, 720p, or 1080p).



3. Click the Download button.



4. The downloaded file will be saved in:


~/Desktop/downloader/downloads





---


8. Stop the Server


To stop the server, go to Terminal and press:


Ctrl + C



---


9. Create a Shortcut for Easy Access


1. Open a text editor and type:


#!/bin/bash

cd ~/Desktop/downloader

python3 server.py



2. Save this file as start_downloader.sh.



3. Make it executable:


chmod +x start_downloader.sh



4. Now, double-click start_downloader.sh to run the app easily.






---


Conclusion


This tool allows you to download videos and audio from YouTube or other supported sites.


Works only on your local machine (localhost).


Easy to use with a simple web-based interface.


Can be extended to support more features.



Enjoy downloading your favorite media!


Comments

Popular posts from this blog

Fixing apt and dpkg Errors in Ubuntu/Debian

Python Web Scanner(Easy to USE)

Easy Alias