Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -59,45 +59,44 @@ def upload_to_web3_storage(api_key, path):
|
|
59 |
logging.exception("An error occurred while uploading to web3.storage.")
|
60 |
return f"An error occurred while uploading to web3.storage: {response.json()}"
|
61 |
|
62 |
-
def convert_video(video_file, quality, aspect_ratio,
|
63 |
-
"""Converts a video to HLS format, adjusting the quality and aspect ratio as necessary."""
|
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 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
#
|
100 |
-
#
|
101 |
output_copy_path = shutil.copy2(output_path, tempfile.gettempdir())
|
102 |
|
103 |
if upload:
|
@@ -114,8 +113,8 @@ def main():
|
|
114 |
"1.85:1", "2.35:1", "3:1", "360", "9:16", "16:9",
|
115 |
"2:1", "1:2", "9:1"],
|
116 |
label="Aspect Ratio", default=None)
|
117 |
-
resolution = gr.
|
118 |
-
|
119 |
video_url = gr.inputs.Textbox(label="Video URL")
|
120 |
api_key = gr.inputs.Textbox(label="web3.storage API Key")
|
121 |
upload = gr.inputs.Checkbox(label="Upload to web3.storage", default=False)
|
|
|
59 |
logging.exception("An error occurred while uploading to web3.storage.")
|
60 |
return f"An error occurred while uploading to web3.storage: {response.json()}"
|
61 |
|
62 |
+
def convert_video(video_file, quality, aspect_ratio, video_url, api_key, upload, resolution):
|
|
|
63 |
with tempfile.TemporaryDirectory() as temp_dir:
|
64 |
temp_dir = Path(temp_dir)
|
65 |
input_path = get_input_path(video_file, video_url, temp_dir)
|
66 |
+
|
67 |
aspect_ratio = get_aspect_ratio(input_path, aspect_ratio)
|
68 |
|
69 |
+
for res in resolution:
|
70 |
+
scale = "-1:" + str(res.replace("p", "")) # we convert '1080p' to '-1:1080'
|
71 |
+
output_path = get_output_path(input_path, temp_dir, res) # pass the resolution to create a unique output file
|
72 |
+
|
73 |
+
ffmpeg_command = [
|
74 |
+
"ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),
|
75 |
+
"-vf", f"scale={scale},setsar={aspect_ratio}", "-hls_time", "6",
|
76 |
+
"-hls_playlist_type", "vod", "-f", "hls", str(output_path)
|
77 |
+
]
|
78 |
+
|
79 |
+
try:
|
80 |
+
logging.info("Running command: %s", " ".join(ffmpeg_command))
|
81 |
+
subprocess.run(ffmpeg_command, check=True, timeout=600)
|
82 |
+
except subprocess.CalledProcessError:
|
83 |
+
logging.exception("ffmpeg command failed.")
|
84 |
+
error_file_path = tempfile.gettempdir() + "/error.txt"
|
85 |
+
with open(error_file_path, 'w') as error_file:
|
86 |
+
error_file.write("ffmpeg command failed.")
|
87 |
+
return error_file_path
|
88 |
+
except subprocess.TimeoutExpired:
|
89 |
+
logging.exception("ffmpeg command timed out.")
|
90 |
+
return "ffmpeg command timed out."
|
91 |
+
except FileNotFoundError:
|
92 |
+
logging.exception("ffmpeg is not installed.")
|
93 |
+
return "ffmpeg is not installed."
|
94 |
+
except Exception as e:
|
95 |
+
logging.exception("An error occurred.")
|
96 |
+
return f"An error occurred: {str(e)}"
|
97 |
+
|
98 |
+
# We need to modify this part if you want to return all converted videos
|
99 |
+
# Here we only return the last converted video
|
100 |
output_copy_path = shutil.copy2(output_path, tempfile.gettempdir())
|
101 |
|
102 |
if upload:
|
|
|
113 |
"1.85:1", "2.35:1", "3:1", "360", "9:16", "16:9",
|
114 |
"2:1", "1:2", "9:1"],
|
115 |
label="Aspect Ratio", default=None)
|
116 |
+
resolution = gr.components.Dropdown(
|
117 |
+
choices=["480p", "720p", "1080p", "1440p", "2160p", "4320p"], label="Resolution", multiple=True, default=["1080p"])
|
118 |
video_url = gr.inputs.Textbox(label="Video URL")
|
119 |
api_key = gr.inputs.Textbox(label="web3.storage API Key")
|
120 |
upload = gr.inputs.Checkbox(label="Upload to web3.storage", default=False)
|