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


Created by Bismoy Ghosh




---


1. Install Python (If Not Installed)


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


Steps to Install Python:


1. Go to the official Python website: https://www.python.org/downloads/



2. Download and install the latest version of Python.



3. During installation, check the box for "Add Python to PATH".



4. After installation, open Command Prompt (cmd) and run:


python --version


If Python is installed correctly, it will display the version.





---


2. Install Required Packages


Open Command Prompt (cmd) and enter the following commands:


pip install yt-dlp

pip install flask

pip install ffmpeg



---


3. Create a Directory for the Project


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


cd C:\Users\YourUsername\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 Notepad or any text editor (e.g., VS Code, Notepad++).



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__)


# Set the download path to a folder inside the current directory

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 C:\Users\YourUsername\Desktop\downloader\templates, create a new file called index.html.



2. Open it with Notepad or any text editor.



3. Copy 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>


4. Save and close the file.





---


6. Run the Web App


1. Open Command Prompt and navigate to the project folder:


cd C:\Users\YourUsername\Desktop\downloader



2. Start the Flask server:


python 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:


C:\Users\YourUsername\Desktop\downloader\downloads





---


8. Stop the Server


To stop the server, go to Command Prompt and press:


Ctrl + C



---


9. Create a Shortcut for Easy Access


1. Open Notepad and type:


@echo off

cd /d C:\Users\YourUsername\Desktop\downloader

python server.py

pause



2. Save this file as start_downloader.bat.



3. Now, double-click start_downloader.bat 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