Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,12 @@
|
|
1 |
import time
|
2 |
-
|
3 |
import os
|
4 |
-
|
5 |
import gradio as gr
|
6 |
-
import
|
7 |
import subprocess
|
8 |
from gradio import components
|
9 |
-
|
10 |
import tempfile
|
11 |
-
|
12 |
from pathlib import Path
|
13 |
-
from gradio import inputs
|
14 |
|
15 |
# Define File object
|
16 |
File = Path
|
@@ -20,82 +16,93 @@ temp_dir = tempfile.mkdtemp()
|
|
20 |
|
21 |
video_file = gr.inputs.File(label="Video File", type="file")
|
22 |
quality = gr.inputs.Dropdown(choices=["18", "23", "28", "32"], label="Quality", default="23")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
# Get the command to get the aspect ratio of the video
|
29 |
-
ffprobe_command = f"{ffprobe_path} -show_entries stream=width,height -of compact=p=0:nk=1 {video_file}"
|
30 |
-
|
31 |
-
# Run the command and get the output
|
32 |
-
output = subprocess.check_output(ffprobe_command, shell=True).decode("utf-8")
|
33 |
-
|
34 |
-
# Get the aspect ratio from the output
|
35 |
-
aspect_ratio = output.split()[0].split(":")[1]
|
36 |
-
|
37 |
-
return aspect_ratio
|
38 |
-
|
39 |
-
def convert_video(video_file: File, quality, aspect_ratio):
|
40 |
-
if video_file is None:
|
41 |
-
video_file = Path(video_url)
|
42 |
-
|
43 |
-
# Get the original aspect ratio of the video
|
44 |
-
original_aspect_ratio = get_aspect_ratio(video_file)
|
45 |
-
|
46 |
-
# Check if the aspect ratio is already set
|
47 |
-
if aspect_ratio == original_aspect_ratio:
|
48 |
-
return video_file
|
49 |
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
return None
|
58 |
-
except FileNotFoundError:
|
59 |
-
print("ffmpeg is not installed.")
|
60 |
-
return None
|
61 |
|
62 |
-
for i in range(10):
|
63 |
-
if os.path.exists(video_file):
|
64 |
-
break
|
65 |
else:
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
try:
|
89 |
-
subprocess.run(ffmpeg_command, shell=True, timeout=10)
|
90 |
-
except subprocess.TimeoutExpired:
|
91 |
-
print("ffmpeg timed out")
|
92 |
-
return None
|
93 |
-
except FileNotFoundError:
|
94 |
-
print("ffmpeg is not installed.")
|
95 |
-
return None
|
96 |
-
|
97 |
-
return m3u8_file
|
98 |
-
|
99 |
-
from gradio import outputs
|
100 |
-
|
101 |
-
gr.Interface(convert_video, inputs=[video_file, quality], outputs=[outputs.Video(), outputs.File()]).launch(share=False)
|
|
|
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 |
|
11 |
# Define File object
|
12 |
File = Path
|
|
|
16 |
|
17 |
video_file = gr.inputs.File(label="Video File", type="file")
|
18 |
quality = gr.inputs.Dropdown(choices=["18", "23", "28", "32"], label="Quality", default="23")
|
19 |
+
aspect_ratio = gr.inputs.Dropdown(choices=[
|
20 |
+
"1:1",
|
21 |
+
"4:3",
|
22 |
+
"3:2",
|
23 |
+
"5:4",
|
24 |
+
"16:9",
|
25 |
+
"21:9",
|
26 |
+
"1.85:1",
|
27 |
+
"2.35:1",
|
28 |
+
"3:1",
|
29 |
+
"360",
|
30 |
+
"9:16",
|
31 |
+
"16:9",
|
32 |
+
"2:1",
|
33 |
+
"1:2",
|
34 |
+
"9:1",
|
35 |
+
], label="Aspect Ratio", default="16:9")
|
36 |
+
|
37 |
+
video_url = gr.inputs.Textbox(label="Video URL")
|
38 |
+
|
39 |
+
def convert_video(video_file: dict, quality, aspect_ratio, video_url):
|
40 |
+
# If a file was uploaded
|
41 |
+
if video_file is not None:
|
42 |
+
# Get the file extension
|
43 |
+
file_extension = video_file['name'].split(".")[-1]
|
44 |
+
|
45 |
+
# Define output file name
|
46 |
+
output_file_name = video_file['name'][:-len(file_extension)] + ".m3u8"
|
47 |
+
|
48 |
+
# Write the uploaded file to the temporary directory
|
49 |
+
with open(os.path.join(temp_dir, video_file['name']), 'wb') as f:
|
50 |
+
f.write(video_file['content'])
|
51 |
+
|
52 |
+
input_path = os.path.join(temp_dir, video_file['name'])
|
53 |
+
|
54 |
+
# If a URL was provided
|
55 |
+
elif video_url is not None:
|
56 |
+
# Get the file extension
|
57 |
+
file_extension = video_url.split(".")[-1]
|
58 |
+
|
59 |
+
# Define output file name
|
60 |
+
output_file_name = video_url.split("/")[-1][:-len(file_extension)] + ".m3u8"
|
61 |
+
|
62 |
+
# Download the file to the temporary directory
|
63 |
+
response = requests.get(video_url)
|
64 |
+
|
65 |
+
if response.status_code == 200:
|
66 |
+
with open(os.path.join(temp_dir, output_file_name), 'wb') as f:
|
67 |
+
f.write(response.content)
|
68 |
+
|
69 |
+
input_path = os.path.join(temp_dir, output_file_name)
|
70 |
+
else:
|
71 |
+
print("Failed to download file from URL.")
|
72 |
+
return None
|
73 |
|
74 |
+
else:
|
75 |
+
print("No input was provided.")
|
76 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
# Define the output path
|
79 |
+
output_path = os.path.join(temp_dir, output_file_name)
|
80 |
|
81 |
+
# Check if the output file exists
|
82 |
+
if os.path.exists(output_path):
|
83 |
+
# The file already exists, so we can just display it in the output viewer
|
84 |
+
return gr.outputs.File(output_path, label="Download")
|
|
|
|
|
|
|
|
|
85 |
|
|
|
|
|
|
|
86 |
else:
|
87 |
+
# The file does not exist, so we need to convert it
|
88 |
+
ffmpeg_command = f"ffmpeg -i {input_path} -c:v libx264 -crf {quality} -vf scale=-1:720,setsar=1:1 -hls_time 6 -hls_playlist_type vod -f hls {output_path}"
|
89 |
+
|
90 |
+
try:
|
91 |
+
subprocess.run(ffmpeg_command, shell=True, timeout=10)
|
92 |
+
except subprocess.TimeoutExpired:
|
93 |
+
print("ffmpeg timed out")
|
94 |
+
return None
|
95 |
+
except FileNotFoundError:
|
96 |
+
print("ffmpeg is not installed.")
|
97 |
+
return None
|
98 |
+
|
99 |
+
for i in range(10):
|
100 |
+
if os.path.exists(output_path):
|
101 |
+
break
|
102 |
+
else:
|
103 |
+
time.sleep(1)
|
104 |
+
|
105 |
+
# The file has now been converted, so we can display it in the output viewer
|
106 |
+
return gr.outputs.File(output_path, label="Download")
|
107 |
+
|
108 |
+
gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs='file').launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|