Jeffgold commited on
Commit
a6be34b
·
1 Parent(s): 30622c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -56
app.py CHANGED
@@ -11,10 +11,25 @@ import http.server
11
  import socketserver
12
  import threading
13
  import atexit
 
14
 
15
  logging.basicConfig(level=logging.INFO)
16
 
17
- PORT = 8000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  temp_dir = Path(tempfile.mkdtemp()) # Create a temporary directory using mkdtemp() instead of TemporaryDirectory()
19
 
20
  class Handler(http.server.SimpleHTTPRequestHandler):
@@ -23,19 +38,11 @@ class Handler(http.server.SimpleHTTPRequestHandler):
23
 
24
  def start_server():
25
  global PORT # Make sure to define PORT as global
26
- while True:
27
- try:
28
- with socketserver.TCPServer(("", PORT), Handler) as httpd:
29
- print(f"Serving at port {PORT}")
30
- httpd.serve_forever()
31
- except OSError as e:
32
- if e.errno == 98: # errno 98 means address already in use
33
- PORT += 1 # If address in use, increment port and try again
34
- continue
35
- else:
36
- raise
37
- break # If server started successfully, break the loop
38
-
39
  t = threading.Thread(target=start_server)
40
  t.start()
41
 
@@ -88,46 +95,6 @@ def create_master_playlist(output_paths, temp_dir):
88
  f.write(f"{path.name}\n")
89
  return master_playlist_path
90
 
91
- import logging
92
- import shutil
93
- import tempfile
94
- import subprocess
95
- from pathlib import Path
96
- from moviepy.editor import VideoFileClip
97
- import gradio as gr
98
- import requests
99
- from urllib.parse import urlparse
100
- import http.server
101
- import socketserver
102
- import threading
103
- import atexit
104
-
105
- logging.basicConfig(level=logging.INFO)
106
-
107
- PORT = 8000
108
- temp_dir = Path(tempfile.mkdtemp()) # Create a temporary directory using mkdtemp() instead of TemporaryDirectory()
109
-
110
- class Handler(http.server.SimpleHTTPRequestHandler):
111
- def __init__(self, *args, **kwargs):
112
- super().__init__(*args, directory=str(temp_dir), **kwargs)
113
-
114
- def start_server():
115
- with socketserver.TCPServer(("", PORT), Handler) as httpd:
116
- print(f"Serving at port {PORT}")
117
- httpd.serve_forever()
118
-
119
- t = threading.Thread(target=start_server)
120
- t.start()
121
-
122
- # Cleanup function to remove the temporary directory when the script is exited
123
- @atexit.register
124
- def cleanup():
125
- shutil.rmtree(temp_dir)
126
-
127
- logging.basicConfig(level=logging.INFO)
128
-
129
- # ... rest of your code ...
130
-
131
  def convert_video(video_file, quality, aspect_ratio, video_url):
132
  # Use the already-created temp_dir instead of creating a new one
133
  # temp_dir = Path(tempfile.mkdtemp())
@@ -169,8 +136,7 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
169
  video_file = gr.File(label="Video File")
170
  quality = gr.Dropdown(
171
  "27", ["18", "23", "27", "28", "32"], label="Quality")
172
- aspect_ratio = gr.Dropdown("16:9", ["16:9", "1:1", "4:3", "3:2", "5:4", "21:9", "1.85:1", "2.35:1", "3:1", "360", "9:16", "2:1", "1:2", "9:1"],
173
- label="Aspect ratio (width:height)")
174
  standard_resolutions = [4320, 2160, 1440, 1080, 720, 480, 360, 240, 144] # 8K, 4K, 2K, Full HD, HD, SD in pixels
175
  video_url = gr.Textbox(label="Or enter video URL")
176
 
 
11
  import socketserver
12
  import threading
13
  import atexit
14
+ import socket
15
 
16
  logging.basicConfig(level=logging.INFO)
17
 
18
+ PORT = 8000 # Define PORT here
19
+
20
+ def get_available_port(start_port):
21
+ while True:
22
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
23
+ try:
24
+ s.bind(("", start_port))
25
+ return start_port
26
+ except OSError as e:
27
+ if e.errno == 98: # errno 98 means address already in use
28
+ start_port += 1 # If address in use, increment port and try again
29
+ continue
30
+ else:
31
+ raise
32
+
33
  temp_dir = Path(tempfile.mkdtemp()) # Create a temporary directory using mkdtemp() instead of TemporaryDirectory()
34
 
35
  class Handler(http.server.SimpleHTTPRequestHandler):
 
38
 
39
  def start_server():
40
  global PORT # Make sure to define PORT as global
41
+ PORT = get_available_port(PORT)
42
+ with socketserver.TCPServer(("", PORT), Handler) as httpd:
43
+ print(f"Serving at port {PORT}")
44
+ httpd.serve_forever()
45
+
 
 
 
 
 
 
 
 
46
  t = threading.Thread(target=start_server)
47
  t.start()
48
 
 
95
  f.write(f"{path.name}\n")
96
  return master_playlist_path
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  def convert_video(video_file, quality, aspect_ratio, video_url):
99
  # Use the already-created temp_dir instead of creating a new one
100
  # temp_dir = Path(tempfile.mkdtemp())
 
136
  video_file = gr.File(label="Video File")
137
  quality = gr.Dropdown(
138
  "27", ["18", "23", "27", "28", "32"], label="Quality")
139
+ aspect_ratio = gr.Dropdown(["16:9", "1:1", "4:3", "3:2", "5:4", "21:9", "1.85:1", "2.35:1", "3:1", "360", "9:16", "2:1", "1:2", "9:1"], label="Aspect ratio (width:height)", default="16:9")
 
140
  standard_resolutions = [4320, 2160, 1440, 1080, 720, 480, 360, 240, 144] # 8K, 4K, 2K, Full HD, HD, SD in pixels
141
  video_url = gr.Textbox(label="Or enter video URL")
142