File size: 2,129 Bytes
012f727
6d88548
012f727
2e2192c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d88548
2e2192c
 
 
 
 
 
 
 
6d88548
2e2192c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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")

    # Save file
    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()

        # Zip output
        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"
            )