| import streamlit as st |
| import subprocess |
| import os |
| import zipfile |
| import uuid |
| import shutil |
|
|
| st.set_page_config(page_title="MP4 → HLS Converter", layout="centered") |
|
|
| st.title("🎬 MP4 → HLS (m3u8) Converter") |
| st.caption("Uses FFmpeg • No re-encode • Streaming-safe") |
|
|
| uploaded_file = st.file_uploader("Upload MP4 file", type=["mp4"]) |
|
|
| if uploaded_file: |
| job_id = str(uuid.uuid4()) |
| workdir = f"/tmp/{job_id}" |
| os.makedirs(workdir, exist_ok=True) |
|
|
| input_path = os.path.join(workdir, "input.mp4") |
| output_path = os.path.join(workdir, "output.m3u8") |
| zip_path = os.path.join(workdir, "hls.zip") |
|
|
| |
| with open(input_path, "wb") as f: |
| f.write(uploaded_file.read()) |
|
|
| st.success("File uploaded") |
|
|
| if st.button("Convert to HLS"): |
| with st.spinner("Running FFmpeg…"): |
| cmd = [ |
| "ffmpeg", |
| "-y", |
| "-i", input_path, |
| "-codec", "copy", |
| "-start_number", "0", |
| "-hls_time", "6", |
| "-hls_list_size", "0", |
| "-f", "hls", |
| output_path |
| ] |
|
|
| result = subprocess.run( |
| cmd, |
| stdout=subprocess.PIPE, |
| stderr=subprocess.PIPE |
| ) |
|
|
| if result.returncode != 0: |
| st.error("FFmpeg failed") |
| st.text(result.stderr.decode()) |
| shutil.rmtree(workdir) |
| st.stop() |
|
|
| |
| with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf: |
| for file in os.listdir(workdir): |
| if file.endswith(".ts") or file.endswith(".m3u8"): |
| zipf.write( |
| os.path.join(workdir, file), |
| arcname=file |
| ) |
|
|
| st.success("Conversion complete") |
|
|
| with open(zip_path, "rb") as f: |
| st.download_button( |
| label="⬇️ Download HLS (ZIP)", |
| data=f, |
| file_name="test2.zip", |
| mime="application/zip" |
| ) |