Neon-AI commited on
Commit
6d88548
·
verified ·
1 Parent(s): a886b08

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +33 -57
src/streamlit_app.py CHANGED
@@ -1,71 +1,47 @@
1
  import streamlit as st
2
- import requests
3
- import m3u8
4
  import os
5
  import time
6
- from moviepy.editor import concatenate_videoclips, VideoFileClip
7
 
8
- st.title("HLS to MP4 Downloader")
9
 
10
  url = st.text_input("Enter HLS stream URL (.m3u8):")
 
11
 
12
- if st.button("Download as MP4"):
13
  if not url:
14
  st.error("Please enter a valid URL.")
15
  else:
16
  try:
17
  start_time = time.time()
18
-
19
- # Load m3u8 playlist
20
- playlist = m3u8.load(url)
21
- segments = playlist.segments
22
- if not segments:
23
- st.error("No segments found in playlist.")
24
- else:
25
- st.info(f"Found {len(segments)} segments. Downloading...")
26
- ts_files = []
27
- total_bytes = 0
28
-
29
- # Download each segment
30
- for i, seg in enumerate(segments):
31
- seg_url = seg.absolute_uri
32
- ts_filename = f"seg_{i}.ts"
33
- ts_files.append(ts_filename)
34
- r = requests.get(seg_url, stream=True)
35
- with open(ts_filename, "wb") as f:
36
- for chunk in r.iter_content(chunk_size=8192):
37
- if chunk:
38
- f.write(chunk)
39
- total_bytes += len(chunk)
40
- st.write(f"Downloaded segment {i+1}/{len(segments)}")
41
-
42
- # Convert TS segments to MP4 using moviepy
43
- st.info("Merging segments into MP4...")
44
- clips = [VideoFileClip(f) for f in ts_files]
45
- final_clip = concatenate_videoclips(clips)
46
- output_file = "output.mp4"
47
- final_clip.write_videofile(output_file, codec="libx264", audio_codec="aac")
48
- final_clip.close()
49
-
50
- # Clean up TS files
51
- for f in ts_files:
52
- os.remove(f)
53
-
54
- end_time = time.time()
55
- duration = end_time - start_time
56
- file_size_mb = os.path.getsize(output_file) / (1024 * 1024)
57
-
58
- metadata = {
59
- "File Name": output_file,
60
- "File Path": os.path.abspath(output_file),
61
- "File Size (MB)": round(file_size_mb, 2),
62
- "Download Time (s)": round(duration, 2),
63
- "URL": url,
64
- "Total Segments": len(segments)
65
- }
66
-
67
- st.success(f"Downloaded and merged to '{output_file}' successfully!")
68
- st.json(metadata)
69
-
70
  except Exception as e:
71
  st.error(f"Error: {e}")
 
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}")