import os
import subprocess
from flask import Flask, request, redirect, url_for, send_from_directory, flash, render_template_string
from werkzeug.utils import secure_filename
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
from moviepy.video.tools.subtitles import SubtitlesClip
# --- Configuration ---
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'mp4', 'mkv', 'avi', 'srt'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024 # 1 GB max upload size
app.secret_key = 'super secret key' # Needed for flashing messages
# --- HTML Template ---
# The entire web interface is defined in this string.
HTML_TEMPLATE = """
Web Subtitle Tool
Subtitle Extractor & Adder
Upload a video, and optionally an SRT file, to begin.
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
- {{ message }}
{% endfor %}
{% endif %}
{% endwith %}
"""
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# --- Core Subtitle Functions (Adapted for Web) ---
def extract_subtitles_web(video_path):
""" Extracts subtitles and returns the path to the resulting SRT file. """
try:
output_filename = os.path.basename(os.path.splitext(video_path)[0]) + ".srt"
output_srt_path = os.path.join(app.config['UPLOAD_FOLDER'], output_filename)
command = f'ffmpeg -y -i "{video_path}" -map 0:s:0 -c:s srt "{output_srt_path}"'
# Use subprocess for better error handling
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode != 0:
if "Subtitle stream" not in result.stderr:
raise Exception("No subtitle stream found in the video file.")
else:
raise Exception(f"FFmpeg command failed. Error: {result.stderr}")
return output_filename
except Exception as e:
# To see the error in the Flask console
print(f"Extraction error: {e}")
return str(e)
def add_subtitles_web(video_path, srt_path):
""" Adds subtitles and returns the path to the new video file. """
try:
video = VideoFileClip(video_path)
generator = lambda txt: TextClip(txt, font='Arial', fontsize=24, color='white', stroke_color='black', stroke_width=1)
subtitles = SubtitlesClip(srt_path, generator)
final_video = CompositeVideoClip([video, subtitles.set_position(('center', 'bottom'))])
output_filename = os.path.basename(os.path.splitext(video_path)[0]) + "_subtitled.mp4"
output_video_path = os.path.join(app.config['UPLOAD_FOLDER'], output_filename)
final_video.write_videofile(output_video_path, codec='libx264', audio_codec='aac')
return output_filename
except Exception as e:
print(f"Add subtitle error: {e}")
return str(e)
# --- Flask Routes ---
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# Check if files are part of the request
if 'video_file' not in request.files:
flash('No video file part in the request.', 'error')
return redirect(request.url)
video_file = request.files['video_file']
if video_file.filename == '':
flash('No video file selected.', 'error')
return redirect(request.url)
if video_file and allowed_file(video_file.filename):
video_filename = secure_filename(video_file.filename)
video_path = os.path.join(app.config['UPLOAD_FOLDER'], video_filename)
video_file.save(video_path)
action = request.form.get('action')
if action == 'extract':
flash('Extracting subtitles... please wait.', 'success')
result_file = extract_subtitles_web(video_path)
if result_file and not "Error" in result_file and not "failed" in result_file and not "found" in result_file:
return redirect(url_for('download_file', filename=result_file))
else:
flash(f'Extraction Failed: {result_file}', 'error')
return redirect(request.url)
elif action == 'add':
if 'srt_file' not in request.files or request.files['srt_file'].filename == '':
flash('You must select an SRT subtitle file to add.', 'error')
return redirect(request.url)
srt_file = request.files['srt_file']
if srt_file and allowed_file(srt_file.filename):
srt_filename = secure_filename(srt_file.filename)
srt_path = os.path.join(app.config['UPLOAD_FOLDER'], srt_filename)
srt_file.save(srt_path)
flash('Adding subtitles... this can take a very long time.', 'success')
result_file = add_subtitles_web(video_path, srt_path)
if result_file and not "Error" in result_file:
return redirect(url_for('download_file', filename=result_file))
else:
flash(f'Adding Subtitles Failed: {result_file}', 'error')
return redirect(request.url)
else:
flash('Invalid subtitle file type.', 'error')
return redirect(request.url)
return render_template_string(HTML_TEMPLATE)
@app.route('/uploads/')
def download_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
# --- Main execution block ---
if __name__ == '__main__':
# Create the upload folder if it doesn't exist
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
# Check for FFmpeg - this is not needed for Hugging Face deployment
# as we handle it in packages.txt, but good for local testing.
try:
subprocess.run(['ffmpeg', '-version'], check=True, capture_output=True)
print("FFmpeg found. Starting server...")
# When running on Hugging Face, they control the host and port.
# For local running, we use the old command.
# The 'app.run' command is often not needed in production servers.
# However, for simple Hugging Face deployment, this works.
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
except (subprocess.CalledProcessError, FileNotFoundError):
print("\n--- FATAL ERROR ---")
print("FFmpeg could not be found. Please install it and ensure it's in your system's PATH.")
print("You can download it from ffmpeg.org.")
input("Press Enter to exit.")