Online youtube video downloader

Developed a web-based application using Flask and yt-dlp to download YouTube videos in MP4 format. Integrated client-side JavaScript for user interaction and server-side Python for processing video URLs and merging video/audio streams using ffmpeg.

Great to hear it’s working now! βœ… Here's a professional summary of all the steps you followed to create and run a working YouTube MP4 video downloader web app using Flask and yt-dlp.


βœ… Project Summary: YouTube MP4 Downloader (Flask + yt-dlp)

πŸ”§ 1. Set Up the Project Directory

youtube_video_downloader/
β”œβ”€β”€ app.py
β”œβ”€β”€ templates/
β”‚   └── index.html
β”œβ”€β”€ downloads/

πŸ“¦ 2. Install Required Packages

Install Flask and yt-dlp:

pip install flask yt-dlp

Also install ffmpeg, required for merging video and audio streams.


πŸ› οΈ 3. Install & Configure ffmpeg on Windows

ffmpeg -version

πŸ“„ 4. HTML Frontend (templates/index.html)

<!DOCTYPE html>
<html>
<head>
    <title>YouTube MP4 Downloader</title>
</head>
<body>
    <h2>Download YouTube Video (MP4)</h2>
    <input type="text" id="url" placeholder="https://www.youtube.com/watch?v=..." />
    <button onclick="download()">Download</button>

    <script>
        function download() {
            const url = document.getElementById("url").value;
            fetch("/download", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({ url })
            })
            .then(response => {
                if (!response.ok) throw new Error("Failed to download");
                return response.blob();
            })
            .then(blob => {
                const a = document.createElement("a");
                a.href = window.URL.createObjectURL(blob);
                a.download = "video.mp4";
                a.click();
            })
            .catch(err => alert("Error: " + err.message));
        }
    </script>
</body>
</html>

🐍 5. Flask Backend (app.py)

from flask import Flask, request, send_file, render_template
import yt_dlp
import os
import uuid

app = Flask(__name__)

DOWNLOAD_FOLDER = "downloads"
os.makedirs(DOWNLOAD_FOLDER, exist_ok=True)

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/download", methods=["POST"])
def download_video():
    data = request.get_json()
    url = data.get("url")
    if not url:
        return {"error": "No URL provided"}, 400

    filename = f"{uuid.uuid4()}.mp4"
    filepath = os.path.join(DOWNLOAD_FOLDER, filename)

    ydl_opts = {
        'outtmpl': filepath,
        'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',
        'merge_output_format': 'mp4'
    }

    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])
        return send_file(filepath, as_attachment=True)
    except Exception as e:
        print("❌ yt-dlp Error:", e)
        return {"error": "Download failed: " + str(e)}, 500

if __name__ == "__main__":
    app.run(debug=True)

▢️ 6. Run the Flask App

python app.py

Open your browser and navigate to:

http://127.0.0.1:5000

πŸ“Œ Optional Next Steps

  • Add video title extraction
  • Auto-delete downloaded files after sending
  • Dockerize the application
  • Add download queue / background processing
  • Enforce file size limits / validation

Let me know if you’d like help on any of these enhancements or packaging this into a desktop app or deploying it online.

Back to Projects