Fdghtffhhgfvhytfvbmngf commited on
Commit
6e75268
·
verified ·
1 Parent(s): f56e6a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -27
app.py CHANGED
@@ -1,34 +1,43 @@
1
  import ffmpeg
2
- import streamlit as st
3
  import time
4
-
5
- # ts stores the time in seconds
6
- ts = time.time()
7
 
8
- def convert_m3u8_to_mp4(url, output_path):
 
9
  try:
10
- # Download and convert the m3u8 stream to mp4 using FFmpeg
11
- ffmpeg.input(url).output(output_path, vcodec='copy', acodec='copy').run()
12
- return True
 
 
 
 
 
 
13
  except ffmpeg.Error as e:
14
- st.error(f"An error occurred: {e}")
15
- return False
 
 
 
 
 
 
 
 
16
 
17
- def main():
18
- st.title("M3U8 Stream to MP4 Converter")
 
 
19
 
20
- url = st.text_input("Enter the URL of the .m3u8 stream")
21
- if url:
22
- ts = str(time.time())
23
- output_path = "/tmp/" + ts + ".mp4"
24
-
25
- if st.button("Convert"):
26
- success = convert_m3u8_to_mp4(url, output_path)
27
- if success:
28
- st.success("Conversion successful!")
29
- st.download_button("Download MP4", data=open(output_path, "rb"), file_name= ts +".mp4")
30
- else:
31
- st.error("Conversion failed.")
32
-
33
- if __name__ == "__main__":
34
- main()
 
1
  import ffmpeg
2
+ import gradio as gr
3
  import time
4
+ import os
 
 
5
 
6
+ # Function to convert M3U8 to MP4
7
+ def convert_m3u8_to_mp4(url):
8
  try:
9
+ # Generate a unique output file name based on the current timestamp
10
+ timestamp = str(int(time.time()))
11
+ output_path = f"/tmp/{timestamp}.mp4"
12
+
13
+ # Convert the M3U8 stream to MP4
14
+ ffmpeg.input(url).output(output_path, vcodec='copy', acodec='copy', format='mp4').run()
15
+
16
+ # Return the output file path for download
17
+ return output_path
18
  except ffmpeg.Error as e:
19
+ return f"Error occurred during conversion: {e}"
20
+
21
+ # Function for the Gradio app
22
+ def process_conversion(url):
23
+ # Perform conversion and check for errors
24
+ result = convert_m3u8_to_mp4(url)
25
+ if os.path.exists(result):
26
+ return result
27
+ else:
28
+ return "Conversion failed. Please check the URL or try again."
29
 
30
+ # Gradio Interface
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("# M3U8 Stream to MP4 Converter")
33
+ gr.Markdown("Enter the URL of a `.m3u8` stream below to convert it into an MP4 file.")
34
 
35
+ url_input = gr.Textbox(label="M3U8 Stream URL", placeholder="Enter .m3u8 stream URL here...")
36
+ convert_button = gr.Button("Convert")
37
+ output_file = gr.File(label="Download MP4 File")
38
+
39
+ # Define the workflow when the button is clicked
40
+ convert_button.click(fn=process_conversion, inputs=url_input, outputs=output_file)
41
+
42
+ # Launch the Gradio app
43
+ demo.launch()