Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from urllib.parse import urlparse
|
|
10 |
import http.server
|
11 |
import socketserver
|
12 |
import threading
|
|
|
13 |
|
14 |
logging.basicConfig(level=logging.INFO)
|
15 |
|
@@ -28,6 +29,11 @@ def start_server():
|
|
28 |
t = threading.Thread(target=start_server)
|
29 |
t.start()
|
30 |
|
|
|
|
|
|
|
|
|
|
|
31 |
logging.basicConfig(level=logging.INFO)
|
32 |
|
33 |
def download_file(url, destination):
|
@@ -72,6 +78,46 @@ def create_master_playlist(output_paths, temp_dir):
|
|
72 |
f.write(f"{path.name}\n")
|
73 |
return master_playlist_path
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
def convert_video(video_file, quality, aspect_ratio, video_url):
|
76 |
# Use the already-created temp_dir instead of creating a new one
|
77 |
# temp_dir = Path(tempfile.mkdtemp())
|
@@ -106,21 +152,24 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
|
|
106 |
master_playlist_path = create_master_playlist(output_paths, temp_dir)
|
107 |
output_paths.append(master_playlist_path)
|
108 |
|
109 |
-
|
|
|
110 |
|
111 |
video_file = gr.File(label="Video File")
|
112 |
quality = gr.Dropdown(
|
113 |
choices=["18", "23", "27", "28", "32"], label="Quality", default="27")
|
114 |
aspect_ratio = gr.Textbox(default="16:9", lines=1, label="Aspect ratio (width:height)")
|
115 |
-
standard_resolutions =
|
|
|
116 |
video_url = gr.Textbox(label="Or enter video URL")
|
117 |
|
118 |
interface = gr.Interface(
|
119 |
fn=convert_video,
|
120 |
inputs=[video_file, quality, aspect_ratio, video_url],
|
121 |
-
outputs=gr.
|
122 |
title="Video Converter",
|
123 |
description="A simple video converter app",
|
124 |
allow_flagging=False,
|
125 |
)
|
126 |
interface.launch()
|
|
|
|
10 |
import http.server
|
11 |
import socketserver
|
12 |
import threading
|
13 |
+
import atexit
|
14 |
|
15 |
logging.basicConfig(level=logging.INFO)
|
16 |
|
|
|
29 |
t = threading.Thread(target=start_server)
|
30 |
t.start()
|
31 |
|
32 |
+
# Cleanup function to remove the temporary directory when the script is exited
|
33 |
+
@atexit.register
|
34 |
+
def cleanup():
|
35 |
+
shutil.rmtree(temp_dir)
|
36 |
+
|
37 |
logging.basicConfig(level=logging.INFO)
|
38 |
|
39 |
def download_file(url, destination):
|
|
|
78 |
f.write(f"{path.name}\n")
|
79 |
return master_playlist_path
|
80 |
|
81 |
+
import logging
|
82 |
+
import shutil
|
83 |
+
import tempfile
|
84 |
+
import subprocess
|
85 |
+
from pathlib import Path
|
86 |
+
from moviepy.editor import VideoFileClip
|
87 |
+
import gradio as gr
|
88 |
+
import requests
|
89 |
+
from urllib.parse import urlparse
|
90 |
+
import http.server
|
91 |
+
import socketserver
|
92 |
+
import threading
|
93 |
+
import atexit
|
94 |
+
|
95 |
+
logging.basicConfig(level=logging.INFO)
|
96 |
+
|
97 |
+
PORT = 8000
|
98 |
+
temp_dir = Path(tempfile.mkdtemp()) # Create a temporary directory using mkdtemp() instead of TemporaryDirectory()
|
99 |
+
|
100 |
+
class Handler(http.server.SimpleHTTPRequestHandler):
|
101 |
+
def __init__(self, *args, **kwargs):
|
102 |
+
super().__init__(*args, directory=str(temp_dir), **kwargs)
|
103 |
+
|
104 |
+
def start_server():
|
105 |
+
with socketserver.TCPServer(("", PORT), Handler) as httpd:
|
106 |
+
print(f"Serving at port {PORT}")
|
107 |
+
httpd.serve_forever()
|
108 |
+
|
109 |
+
t = threading.Thread(target=start_server)
|
110 |
+
t.start()
|
111 |
+
|
112 |
+
# Cleanup function to remove the temporary directory when the script is exited
|
113 |
+
@atexit.register
|
114 |
+
def cleanup():
|
115 |
+
shutil.rmtree(temp_dir)
|
116 |
+
|
117 |
+
logging.basicConfig(level=logging.INFO)
|
118 |
+
|
119 |
+
# ... rest of your code ...
|
120 |
+
|
121 |
def convert_video(video_file, quality, aspect_ratio, video_url):
|
122 |
# Use the already-created temp_dir instead of creating a new one
|
123 |
# temp_dir = Path(tempfile.mkdtemp())
|
|
|
152 |
master_playlist_path = create_master_playlist(output_paths, temp_dir)
|
153 |
output_paths.append(master_playlist_path)
|
154 |
|
155 |
+
# Convert Path objects to URLs before returning
|
156 |
+
return [{"name": path.stem, "url": f"http://localhost:{PORT}/{path.name}"} for path in output_paths]
|
157 |
|
158 |
video_file = gr.File(label="Video File")
|
159 |
quality = gr.Dropdown(
|
160 |
choices=["18", "23", "27", "28", "32"], label="Quality", default="27")
|
161 |
aspect_ratio = gr.Textbox(default="16:9", lines=1, label="Aspect ratio (width:height)")
|
162 |
+
standard_resolutions = gr.Dropdown(
|
163 |
+
choices=[4320, 2160, 1440, 1080, 720, 480, 360, 240, 144] # 8K, 4K, 2K, Full HD, HD, SD in pixels
|
164 |
video_url = gr.Textbox(label="Or enter video URL")
|
165 |
|
166 |
interface = gr.Interface(
|
167 |
fn=convert_video,
|
168 |
inputs=[video_file, quality, aspect_ratio, video_url],
|
169 |
+
outputs=gr.outputs.HTML(label="Download Links"),
|
170 |
title="Video Converter",
|
171 |
description="A simple video converter app",
|
172 |
allow_flagging=False,
|
173 |
)
|
174 |
interface.launch()
|
175 |
+
|