Created
June 20, 2025 03:59
-
-
Save JupyterJones/6f3d8792344dcec01f12c690a4095f82 to your computer and use it in GitHub Desktop.
Flask Application Creates mp4 from Images
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
import os | |
from flask import Flask, request, render_template, redirect, flash, url_for, render_template_string | |
from moviepy.editor import ImageSequenceClip | |
app = Flask(__name__) | |
app.config['UPLOAD_FOLDER'] = os.path.join(app.root_path, 'static/uploads') | |
app.secret_key = 'your_secret_key' #Replace with a strong, randomly generated secret key | |
@app.route('/', methods=['GET', 'POST']) | |
def upload_file(): | |
if request.method == 'POST': | |
if 'files[]' not in request.files: | |
flash('No files selected') | |
return redirect(request.url) | |
files = request.files.getlist('files[]') | |
if not files or all(f.filename == '' for f in files): | |
flash('No files selected') | |
return redirect(request.url) | |
#Clean up existing files | |
for filename in os.listdir(app.config['UPLOAD_FOLDER']): | |
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
try: | |
if os.path.isfile(file_path): | |
os.remove(file_path) | |
except OSError as e: | |
print(f"Error deleting file {filename}: {e}") | |
image_paths = [] | |
for file in files: | |
if file.filename.lower().endswith(('.png', '.jpg', '.jpeg')): | |
if file.content_length > 10 * 1024 * 1024: | |
flash(f"File {file.filename} is too large.") | |
continue | |
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename) | |
file.save(file_path) | |
image_paths.append(file_path) | |
else: | |
flash(f"Invalid file type: {file.filename}") | |
if not image_paths: | |
return redirect(request.url) | |
try: | |
image_paths.sort() # Ensure consistent order | |
clip = ImageSequenceClip(image_paths, fps=0.5) #adjust fps as needed | |
video_path = os.path.join(app.config['UPLOAD_FOLDER'], 'flipbookcomplete.mp4') | |
clip.write_videofile(video_path, fps=24, verbose=False, logger=None) | |
clip.close() | |
#Success! | |
return render_template_string(FLIPBOOK) | |
except Exception as e: | |
flash(f"Error creating flipbook: {e}") | |
return redirect(request.url) | |
return render_template_string(UPLOAD) | |
UPLOAD=""" | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Flipbook Creator</title> | |
<style> | |
/*Your CSS styles here*/ | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>🎬 Flipbook Creator</h1> | |
<form method="POST" enctype="multipart/form-data"> | |
<input type="file" name="files[]" multiple class="file-input" accept="image/*"> | |
<button type="submit" class="submit-btn">Create Flipbook</button> | |
</form> | |
{% with messages = get_flashed_messages(with_categories=true) %} | |
{% if messages %} | |
<div class="flash-messages"> | |
{% for category, message in messages %} | |
<div class="flash-message flash-{{ category }}">{{ message }}</div> | |
{% endfor %} | |
</div> | |
{% endif %} | |
{% endwith %} | |
</div> | |
</body> | |
</html> | |
""" | |
FLIPBOOK=""" | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Flipbook Creator</title> | |
<style> | |
/*Your CSS styles here*/ | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>🎬 Flipbook Creator</h1> | |
<div class="video-container"> | |
<h3>Your Flipbook:</h3> | |
<video width="640" height="360" controls> | |
<source src="{{ url_for('static', filename='uploads/flipbookcomplete.mp4') }}" type="video/mp4"> | |
Your browser does not support the video tag. | |
</video> | |
<p><a href="{{ url_for('static', filename='uploads/flipbookcomplete.mp4') }}" download="flipbookcomplete.mp4">Download Video</a></p> | |
</div> | |
</div> | |
</body> | |
</html> | |
""" | |
if __name__ == '__main__': | |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True) | |
os.makedirs(os.path.join(app.root_path, 'templates'), exist_ok=True) #create templates folder | |
app.run(debug=True, host="0.0.0.0", port=5500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment