Neon-AI commited on
Commit
2e2192c
·
verified ·
1 Parent(s): 1564cf3

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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 time
5
-
6
- st.title("HLS to MP4 Downloader (FFmpeg)")
7
-
8
- url = st.text_input("Enter HLS stream URL (.m3u8):")
9
- output_file = st.text_input("Output MP4 filename:", "output.mp4")
10
-
11
- if st.button("Download"):
12
- if not url:
13
- st.error("Please enter a valid URL.")
14
- else:
15
- try:
16
- start_time = time.time()
17
-
18
- # Run FFmpeg command
19
- command = [
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  "ffmpeg",
21
- "-y", # Overwrite output if exists
22
- "-i", url, # Input HLS URL
23
- "-c", "copy", # Copy codecs without re-encoding
24
- output_file
 
 
 
 
25
  ]
26
- st.info("Downloading stream using FFmpeg...")
27
- subprocess.run(command, check=True)
28
-
29
- end_time = time.time()
30
- duration = end_time - start_time
31
- file_size_mb = os.path.getsize(output_file) / (1024 * 1024)
32
-
33
- metadata = {
34
- "File Name": output_file,
35
- "File Path": os.path.abspath(output_file),
36
- "File Size (MB)": round(file_size_mb, 2),
37
- "Download Time (s)": round(duration, 2),
38
- "URL": url
39
- }
40
-
41
- st.success(f"Downloaded successfully to '{output_file}'!")
42
- st.json(metadata)
43
-
44
- except subprocess.CalledProcessError as e:
45
- st.error(f"FFmpeg failed: {e}")
46
- except Exception as e:
47
- st.error(f"Error: {e}")
 
 
 
 
 
 
 
 
 
 
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
+ )