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=""" Flipbook Creator

🎬 Flipbook Creator

{% with messages = get_flashed_messages(with_categories=true) %} {% if messages %}
{% for category, message in messages %}
{{ message }}
{% endfor %}
{% endif %} {% endwith %}
""" FLIPBOOK=""" Flipbook Creator

🎬 Flipbook Creator

Your Flipbook:

Download Video

""" 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)