Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,80 @@
|
|
1 |
import time
|
2 |
-
import os
|
3 |
-
import gradio as gr
|
4 |
import requests
|
5 |
import subprocess
|
6 |
-
from gradio import components
|
7 |
import tempfile
|
8 |
from pathlib import Path
|
9 |
-
from gradio import inputs, outputs
|
10 |
from moviepy.editor import VideoFileClip
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
video_file = gr.inputs.File(label="Video File", type="file")
|
19 |
-
quality = gr.inputs.Dropdown(choices=["18", "23", "28", "32"], label="Quality", default="23")
|
20 |
-
|
21 |
-
# default will now be None, which will retain original aspect ratio
|
22 |
-
aspect_ratio = gr.inputs.Dropdown(choices=[
|
23 |
-
None, # Add None option to retain original aspect ratio
|
24 |
-
"1:1",
|
25 |
-
"4:3",
|
26 |
-
"3:2",
|
27 |
-
"5:4",
|
28 |
-
"16:9",
|
29 |
-
"21:9",
|
30 |
-
"1.85:1",
|
31 |
-
"2.35:1",
|
32 |
-
"3:1",
|
33 |
-
"360",
|
34 |
-
"9:16",
|
35 |
-
"16:9",
|
36 |
-
"2:1",
|
37 |
-
"1:2",
|
38 |
-
"9:1",
|
39 |
-
], label="Aspect Ratio", default=None)
|
40 |
-
|
41 |
-
video_url = gr.inputs.Textbox(label="Video URL")
|
42 |
|
43 |
-
def
|
44 |
-
|
45 |
if video_file is not None:
|
46 |
-
|
47 |
-
file_extension = video_file.name.split(".")[-1]
|
48 |
-
|
49 |
-
# Define output file name
|
50 |
-
output_file_name = video_file.name[:-len(file_extension) - 1] + ".m3u8"
|
51 |
-
|
52 |
-
input_path = video_file.name
|
53 |
-
|
54 |
-
# If a URL was provided
|
55 |
elif video_url:
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
if response.status_code == 200:
|
69 |
-
input_path = os.path.join(temp_dir, file_name)
|
70 |
-
with open(input_path, 'wb') as f:
|
71 |
-
f.write(response.content)
|
72 |
-
else:
|
73 |
-
print("Failed to download file from URL.")
|
74 |
-
return "Failed to download file from URL."
|
75 |
-
else:
|
76 |
-
print("No input was provided.")
|
77 |
-
return "No input was provided."
|
78 |
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
ffmpeg_command = f"ffmpeg -i {input_path} -c:v libx264 -crf {quality} -vf scale=-1:720,setsar={aspect_ratio} -hls_time 6 -hls_playlist_type vod -f hls {output_path}"
|
93 |
-
|
94 |
-
print(f"Input Path: {input_path}")
|
95 |
-
print(f"Output Path: {output_path}")
|
96 |
-
|
97 |
-
try:
|
98 |
-
subprocess.run(ffmpeg_command, shell=True, timeout=600)
|
99 |
except subprocess.TimeoutExpired:
|
100 |
-
print("ffmpeg timed out")
|
101 |
return "ffmpeg command timed out."
|
102 |
except FileNotFoundError:
|
103 |
-
print("ffmpeg is not installed.")
|
104 |
return "ffmpeg is not installed."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
if os.path.exists(output_path):
|
108 |
-
break
|
109 |
-
else:
|
110 |
-
time.sleep(1)
|
111 |
|
112 |
-
|
113 |
-
|
114 |
|
115 |
-
gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs='file').launch()
|
|
|
1 |
import time
|
|
|
|
|
2 |
import requests
|
3 |
import subprocess
|
|
|
4 |
import tempfile
|
5 |
from pathlib import Path
|
|
|
6 |
from moviepy.editor import VideoFileClip
|
7 |
+
import gradio as gr
|
8 |
|
9 |
+
def download_file(url, destination):
|
10 |
+
"""Downloads a file from a url to a destination."""
|
11 |
+
response = requests.get(url)
|
12 |
+
response.raise_for_status() # Raises a HTTPError if the status is 4xx, 5xx
|
13 |
+
with open(destination, 'wb') as f:
|
14 |
+
f.write(response.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
def get_input_path(video_file, video_url, temp_dir):
|
17 |
+
"""Returns the path to the video file, downloading it if necessary."""
|
18 |
if video_file is not None:
|
19 |
+
return video_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
elif video_url:
|
21 |
+
file_name = Path(video_url).name
|
22 |
+
destination = temp_dir / file_name
|
23 |
+
download_file(video_url, destination)
|
24 |
+
return destination
|
25 |
+
else:
|
26 |
+
raise ValueError("No input was provided.")
|
27 |
|
28 |
+
def get_output_path(input_path, temp_dir):
|
29 |
+
"""Returns the path to the output file, creating it if necessary."""
|
30 |
+
output_path = temp_dir / (input_path.stem + ".m3u8")
|
31 |
+
return output_path
|
32 |
|
33 |
+
def get_aspect_ratio(input_path, aspect_ratio):
|
34 |
+
"""Returns the aspect ratio of the video, calculating it if necessary."""
|
35 |
+
if aspect_ratio is not None:
|
36 |
+
return aspect_ratio
|
37 |
+
video = VideoFileClip(str(input_path))
|
38 |
+
return f"{video.size[0]}:{video.size[1]}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
def convert_video(video_file, quality, aspect_ratio, video_url):
|
41 |
+
"""Converts a video to HLS format, adjusting the quality and aspect ratio as necessary."""
|
42 |
+
temp_dir = Path(tempfile.mkdtemp())
|
43 |
+
input_path = get_input_path(video_file, video_url, temp_dir)
|
44 |
+
output_path = get_output_path(input_path, temp_dir)
|
45 |
+
aspect_ratio = get_aspect_ratio(input_path, aspect_ratio)
|
46 |
|
47 |
+
if not output_path.exists():
|
48 |
+
ffmpeg_command = (
|
49 |
+
f"ffmpeg -i {input_path} -c:v libx264 -crf {quality} "
|
50 |
+
f"-vf scale=-1:720,setsar={aspect_ratio} -hls_time 6 "
|
51 |
+
f"-hls_playlist_type vod -f hls {output_path}"
|
52 |
+
)
|
53 |
+
|
54 |
+
try:
|
55 |
+
subprocess.run(ffmpeg_command, shell=True, timeout=600)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
except subprocess.TimeoutExpired:
|
|
|
57 |
return "ffmpeg command timed out."
|
58 |
except FileNotFoundError:
|
|
|
59 |
return "ffmpeg is not installed."
|
60 |
+
except Exception as e:
|
61 |
+
return f"An error occurred: {str(e)}"
|
62 |
+
|
63 |
+
return output_path
|
64 |
+
|
65 |
+
def main():
|
66 |
+
video_file = gr.inputs.File(label="Video File", type="file")
|
67 |
+
quality = gr.inputs.Dropdown(
|
68 |
+
choices=["18", choices=["18", "23", "28", "32"], label="Quality", default="23")
|
69 |
+
aspect_ratio = gr.inputs.Dropdown(
|
70 |
+
choices=[None, "1:1", "4:3", "3:2", "5:4", "16:9", "21:9",
|
71 |
+
"1.85:1", "2.35:1", "3:1", "360", "9:16", "16:9",
|
72 |
+
"2:1", "1:2", "9:1"],
|
73 |
+
label="Aspect Ratio", default=None)
|
74 |
+
video_url = gr.inputs.Textbox(label="Video URL")
|
75 |
|
76 |
+
gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs='file').launch()
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
if __name__ == "__main__":
|
79 |
+
main()
|
80 |
|
|