Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,43 @@
|
|
| 1 |
import ffmpeg
|
| 2 |
-
import
|
| 3 |
import time
|
| 4 |
-
|
| 5 |
-
# ts stores the time in seconds
|
| 6 |
-
ts = time.time()
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
try:
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
except ffmpeg.Error as e:
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|