Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -60,25 +60,27 @@ def upload_to_web3_storage(api_key, path):
|
|
60 |
logging.exception("An error occurred while uploading to web3.storage.")
|
61 |
return f"An error occurred while uploading to web3.storage: {response.json()}"
|
62 |
|
63 |
-
def convert_video(video_file, quality, aspect_ratio, video_url, api_key, upload
|
|
|
|
|
64 |
with tempfile.TemporaryDirectory() as temp_dir:
|
65 |
temp_dir = Path(temp_dir)
|
66 |
input_path = get_input_path(video_file, video_url, temp_dir)
|
67 |
|
68 |
aspect_ratio = get_aspect_ratio(input_path, aspect_ratio)
|
69 |
|
70 |
-
|
71 |
-
|
72 |
|
73 |
output_paths = [] # Define output_paths as an empty list
|
74 |
|
75 |
-
for res in
|
76 |
-
# Skip if
|
77 |
-
if
|
78 |
continue
|
79 |
|
80 |
-
scale = "-1:" + str(res
|
81 |
-
output_path = get_output_path(input_path, temp_dir, res) # pass the resolution to create a unique output file
|
82 |
|
83 |
ffmpeg_command = [
|
84 |
"ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),
|
@@ -86,12 +88,29 @@ def convert_video(video_file, quality, aspect_ratio, video_url, api_key, upload,
|
|
86 |
"-hls_playlist_type", "vod", "-f", "hls", str(output_path)
|
87 |
]
|
88 |
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
output_paths.append(output_path) # Append the output_path to output_paths
|
92 |
|
93 |
if not output_paths:
|
94 |
-
return "
|
95 |
|
96 |
output_copy_paths = [shutil.copy2(path, tempfile.gettempdir()) for path in output_paths]
|
97 |
|
@@ -100,17 +119,18 @@ def convert_video(video_file, quality, aspect_ratio, video_url, api_key, upload,
|
|
100 |
else:
|
101 |
return output_copy_paths
|
102 |
|
|
|
103 |
def main():
|
104 |
video_file = gr.inputs.File(label="Video File")
|
105 |
quality = gr.inputs.Dropdown(
|
106 |
choices=["18", "23", "27", "28", "32"], label="Quality", default="27")
|
107 |
aspect_ratio = gr.inputs.Dropdown(
|
108 |
-
choices=[
|
109 |
"1.85:1", "2.35:1", "3:1", "360", "9:16", "16:9",
|
110 |
"2:1", "1:2", "9:1"],
|
111 |
label="Aspect Ratio", default="16:9")
|
112 |
-
resolution = gr.components.Dropdown(
|
113 |
-
choices=["480p", "720p", "1080p", "1440p", "2160p", "4320p"], label="Resolution",
|
114 |
video_url = gr.inputs.Textbox(label="Video URL")
|
115 |
api_key = gr.inputs.Textbox(label="web3.storage API Key")
|
116 |
upload = gr.inputs.Checkbox(label="Upload to web3.storage", default=False)
|
|
|
60 |
logging.exception("An error occurred while uploading to web3.storage.")
|
61 |
return f"An error occurred while uploading to web3.storage: {response.json()}"
|
62 |
|
63 |
+
def convert_video(video_file, quality, aspect_ratio, video_url, api_key, upload):
|
64 |
+
standard_resolutions = [4320, 2160, 1440, 1080, 720, 480] # 8K, 4K, 2K, Full HD, HD, SD in pixels
|
65 |
+
|
66 |
with tempfile.TemporaryDirectory() as temp_dir:
|
67 |
temp_dir = Path(temp_dir)
|
68 |
input_path = get_input_path(video_file, video_url, temp_dir)
|
69 |
|
70 |
aspect_ratio = get_aspect_ratio(input_path, aspect_ratio)
|
71 |
|
72 |
+
video = VideoFileClip(str(input_path))
|
73 |
+
original_height = video.size[1]
|
74 |
|
75 |
output_paths = [] # Define output_paths as an empty list
|
76 |
|
77 |
+
for res in standard_resolutions:
|
78 |
+
# Skip if resolution is higher than original
|
79 |
+
if res > original_height:
|
80 |
continue
|
81 |
|
82 |
+
scale = "-1:" + str(res) # we scale the height to res and keep aspect ratio
|
83 |
+
output_path = get_output_path(input_path, temp_dir, str(res) + 'p') # pass the resolution to create a unique output file
|
84 |
|
85 |
ffmpeg_command = [
|
86 |
"ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),
|
|
|
88 |
"-hls_playlist_type", "vod", "-f", "hls", str(output_path)
|
89 |
]
|
90 |
|
91 |
+
try:
|
92 |
+
logging.info("Running command: %s", " ".join(ffmpeg_command))
|
93 |
+
subprocess.run(ffmpeg_command, check=True, timeout=600)
|
94 |
+
except subprocess.CalledProcessError:
|
95 |
+
logging.exception("ffmpeg command failed.")
|
96 |
+
error_file_path = tempfile.gettempdir() + "/error.txt"
|
97 |
+
with open(error_file_path, 'w') as error_file:
|
98 |
+
error_file.write("ffmpeg command failed.")
|
99 |
+
return error_file_path
|
100 |
+
except subprocess.TimeoutExpired:
|
101 |
+
logging.exception("ffmpeg command timed out.")
|
102 |
+
return "ffmpeg command timed out."
|
103 |
+
except FileNotFoundError:
|
104 |
+
logging.exception("ffmpeg is not installed.")
|
105 |
+
return "ffmpeg is not installed."
|
106 |
+
except Exception as e:
|
107 |
+
logging.exception("An error occurred.")
|
108 |
+
return f"An error occurred: {str(e)}"
|
109 |
|
110 |
output_paths.append(output_path) # Append the output_path to output_paths
|
111 |
|
112 |
if not output_paths:
|
113 |
+
return "The video is smaller than the smallest standard resolution."
|
114 |
|
115 |
output_copy_paths = [shutil.copy2(path, tempfile.gettempdir()) for path in output_paths]
|
116 |
|
|
|
119 |
else:
|
120 |
return output_copy_paths
|
121 |
|
122 |
+
|
123 |
def main():
|
124 |
video_file = gr.inputs.File(label="Video File")
|
125 |
quality = gr.inputs.Dropdown(
|
126 |
choices=["18", "23", "27", "28", "32"], label="Quality", default="27")
|
127 |
aspect_ratio = gr.inputs.Dropdown(
|
128 |
+
choices=["16:9", "1:1", "4:3", "3:2", "5:4", "21:9",
|
129 |
"1.85:1", "2.35:1", "3:1", "360", "9:16", "16:9",
|
130 |
"2:1", "1:2", "9:1"],
|
131 |
label="Aspect Ratio", default="16:9")
|
132 |
+
# resolution = gr.components.Dropdown(
|
133 |
+
# choices=["480p", "720p", "1080p", "1440p", "2160p", "4320p"], label="Resolution", default="1080p")
|
134 |
video_url = gr.inputs.Textbox(label="Video URL")
|
135 |
api_key = gr.inputs.Textbox(label="web3.storage API Key")
|
136 |
upload = gr.inputs.Checkbox(label="Upload to web3.storage", default=False)
|