This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dotenv import load_dotenv | |
from moviepy import VideoFileClip | |
import cv2 | |
def create_thumbnail(file) -> None: | |
video = VideoFileClip(f"{os.getenv('path/to/video.mp4')}") # Get the video from storage. | |
filenoext = file.rsplit('.', 1)[0] # Get the filename without the extension ['mp4', 'mov', 'mkv', etc.] | |
get_frame = video.get_frame(3) # Get the 3rd frame from the video. You can choose the frame you need. Getting a single frame from the video helps in memory efficiency as the video doesn't need to be loaded in memory completely. | |
cv2.imwrite(f"{os.getenv('path/to/save/image.jpg')}", get_frame) # Save the frame to the desired path. | |
# Don't load the whole video in memory unless it's a lightweight video. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route("/upload", methods=["GET", "POST"]) | |
def upload(): | |
if request.method =="POST": | |
for file in request.files: # Get the file using flask.request from your <input> tag | |
if file.startswith('file'): | |
get_file = request.files.get(file) # When uploading multiple files, use the loop | |
with open(f"{os.getenv('ROOT_DIRECTORY')}/{get_file.filename}", 'wb') as file_binary: # wb will create the file if it doesn't exist and write the chunk | |
for file_chunk in get_file.stream: # Get the file in a stream reader | |
file_binary.write(file_chunk) | |
return render_template("upload.html") |