Skip to content
Snippets Groups Projects
Commit fd591ce7 authored by gautham's avatar gautham
Browse files

file upload and youtube link downloader

parent 06588f8e
No related branches found
No related tags found
1 merge request!3file upload and youtube link downloader
from flask import Flask, render_template, request, redirect, url_for
import yt_dlp as youtube_dl
import os
import uuid
app = Flask(__name__)
# Directories to save uploaded videos and YouTube downloads
UPLOAD_FOLDER = "static/uploads"
YOUTUBE_FOLDER = "static/videos"
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(YOUTUBE_FOLDER):
os.makedirs(YOUTUBE_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_video():
# Check if the form is a YouTube link submission
if 'video_url' in request.form and request.form['video_url']:
url = request.form['video_url']
unique_id = str(uuid.uuid4())
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'outtmpl': os.path.join(SAVE_PATH, f'{unique_id}.%(ext)s'), # Use UUID for the filename
'merge_output_format': 'mp4',
'ffmpeg_location': r'C:\Program Files\ffmpeg-master-latest-win64-gpl-shared\bin' # Provide the ffmpeg path if necessary
}
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
file_name = f"{unique_id}.mp4"
video_path = os.path.join(YOUTUBE_FOLDER, file_name)
return redirect(url_for('play_video', video_file=file_name))
except Exception as e:
return f"Error: {str(e)}. Please check the YouTube URL or try again later."
# If a file is uploaded
elif 'file' in request.files and request.files['file']:
file = request.files['file']
if file.filename == '':
return 'No selected file'
unique_id = str(uuid.uuid4())
filename = f"{unique_id}.mp4"
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
return redirect(url_for('play_video', video_file=filename))
return 'No video uploaded or URL provided.'
@app.route('/play/<video_file>')
def play_video(video_file):
# Determine the folder the video is in (uploads or YouTube downloads)
if os.path.exists(os.path.join(UPLOAD_FOLDER, video_file)):
video_url = url_for('static', filename=f'uploads/{video_file}')
else:
video_url = url_for('static', filename=f'videos/{video_file}')
return render_template('play.html', video_url=video_url)
if __name__ == '__main__':
app.run(debug=True)
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video Uploader</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
h1 {
color: #4CAF50;
}
form {
margin-top: 20px;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
input[type="text"], input[type="file"] {
margin-bottom: 15px;
padding: 10px;
width: 300px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 4px;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>Upload or Enter a YouTube Link</h1>
<!-- Form for YouTube link -->
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="text" name="video_url" placeholder="Enter YouTube Video URL">
<h2>OR</h2>
<!-- Form for file upload -->
<input type="file" name="file" accept="video/*">
<input type="submit" value="Submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Play Video</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
video {
width: 80%;
max-width: 800px;
border: 1px solid #ccc;
border-radius: 8px;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Watch Your Video</h1>
<video controls>
<source src="{{ video_url }}" type="video/mp4">
Your browser does not support the video tag.
</video>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment