Skip to content

Instantly share code, notes, and snippets.

@miguelcamposfernandes
Created October 17, 2021 20:46
Show Gist options
  • Save miguelcamposfernandes/bd2d3f23d28e8c262e34c8ab9d41060e to your computer and use it in GitHub Desktop.
Save miguelcamposfernandes/bd2d3f23d28e8c262e34c8ab9d41060e to your computer and use it in GitHub Desktop.
YTD - Download Function
from flask import redirect, send_file
from io import BytesIO
@app.route("/download", methods = ["GET", "POST"])
def download_video():
if request.method == "POST":
buffer = BytesIO() # Declaring the buffer
url = YouTube(session['link']) # Getting the URL
itag = request.form.get("itag") # Get the video resolution
video = url.streams.get_by_itag(itag) # Store the video into a variable
video.stream_to_buffer(buffer)
buffer.seek(0)
return send_file(buffer, as_attachment=True, download_name="Video - YT2Video.mp4", mimetype="video/mp4")
return redirect(url_for("home"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment