Update src/streamlit_app.py
Browse files- src/streamlit_app.py +68 -42
src/streamlit_app.py
CHANGED
|
@@ -1,47 +1,73 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"ffmpeg",
|
| 21 |
-
"-y",
|
| 22 |
-
"-i",
|
| 23 |
-
"-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
]
|
| 26 |
-
|
| 27 |
-
subprocess.run(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
+
import zipfile
|
| 5 |
+
import uuid
|
| 6 |
+
import shutil
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="MP4 → HLS Converter", layout="centered")
|
| 9 |
+
|
| 10 |
+
st.title("🎬 MP4 → HLS (m3u8) Converter")
|
| 11 |
+
st.caption("Uses FFmpeg • No re-encode • Streaming-safe")
|
| 12 |
+
|
| 13 |
+
uploaded_file = st.file_uploader("Upload MP4 file", type=["mp4"])
|
| 14 |
+
|
| 15 |
+
if uploaded_file:
|
| 16 |
+
job_id = str(uuid.uuid4())
|
| 17 |
+
workdir = f"/tmp/{job_id}"
|
| 18 |
+
os.makedirs(workdir, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
input_path = os.path.join(workdir, "input.mp4")
|
| 21 |
+
output_path = os.path.join(workdir, "output.m3u8")
|
| 22 |
+
zip_path = os.path.join(workdir, "hls.zip")
|
| 23 |
+
|
| 24 |
+
# Save file
|
| 25 |
+
with open(input_path, "wb") as f:
|
| 26 |
+
f.write(uploaded_file.read())
|
| 27 |
+
|
| 28 |
+
st.success("File uploaded")
|
| 29 |
+
|
| 30 |
+
if st.button("Convert to HLS"):
|
| 31 |
+
with st.spinner("Running FFmpeg…"):
|
| 32 |
+
cmd = [
|
| 33 |
"ffmpeg",
|
| 34 |
+
"-y",
|
| 35 |
+
"-i", input_path,
|
| 36 |
+
"-codec", "copy",
|
| 37 |
+
"-start_number", "0",
|
| 38 |
+
"-hls_time", "6",
|
| 39 |
+
"-hls_list_size", "0",
|
| 40 |
+
"-f", "hls",
|
| 41 |
+
output_path
|
| 42 |
]
|
| 43 |
+
|
| 44 |
+
result = subprocess.run(
|
| 45 |
+
cmd,
|
| 46 |
+
stdout=subprocess.PIPE,
|
| 47 |
+
stderr=subprocess.PIPE
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if result.returncode != 0:
|
| 51 |
+
st.error("FFmpeg failed")
|
| 52 |
+
st.text(result.stderr.decode())
|
| 53 |
+
shutil.rmtree(workdir)
|
| 54 |
+
st.stop()
|
| 55 |
+
|
| 56 |
+
# Zip output
|
| 57 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 58 |
+
for file in os.listdir(workdir):
|
| 59 |
+
if file.endswith(".ts") or file.endswith(".m3u8"):
|
| 60 |
+
zipf.write(
|
| 61 |
+
os.path.join(workdir, file),
|
| 62 |
+
arcname=file
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
st.success("Conversion complete")
|
| 66 |
+
|
| 67 |
+
with open(zip_path, "rb") as f:
|
| 68 |
+
st.download_button(
|
| 69 |
+
label="⬇️ Download HLS (ZIP)",
|
| 70 |
+
data=f,
|
| 71 |
+
file_name="test2.zip",
|
| 72 |
+
mime="application/zip"
|
| 73 |
+
)
|