Monday, 1 March 2021

How to make user download files client-side in a Flask web application?

I'm trying to build a YouTube Downloader using Flask, using the youtube-dl Python API. I've got everything working, but I have an issue with the actual download of the videos.

@app.route("/pytube/video/", methods=["POST", "GET"])
def pytube_video():
    if request.method == "POST":
        pytube_download("https://www.youtube.com/watch?v=kFZ-pW4G-s8", "313")
        return send_file("./videos/test.mp4", as_attachment=True)


@app.route("/pytube/download/", methods=["POST", "GET"])
def pytube_download(url, format_id):
    options = {
        'format': format_id,
        "outtmpl": "./videos/test.mp4",
    }

    with youtube_dl.YoutubeDL(options) as y:
        y.download([url])

This process works, but it can be very slow because I am downloading the videos locally and then sending them. Is there a way to make the user download videos in a more direct way, without downloading them first in the backend?



from How to make user download files client-side in a Flask web application?

No comments:

Post a Comment