Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import gradio as gr
|
|
| 8 |
import requests
|
| 9 |
from urllib.parse import urlparse
|
| 10 |
|
|
|
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
|
| 13 |
def download_file(url, destination):
|
|
@@ -69,7 +70,7 @@ def convert_video(video_file, quality, aspect_ratio, video_url, api_key, upload,
|
|
| 69 |
# Check if resolution is a list, if not convert it to a list
|
| 70 |
resolution = [resolution] if not isinstance(resolution, list) else resolution
|
| 71 |
|
| 72 |
-
|
| 73 |
|
| 74 |
for res in resolution:
|
| 75 |
# Skip if res is boolean
|
|
@@ -85,37 +86,19 @@ def convert_video(video_file, quality, aspect_ratio, video_url, api_key, upload,
|
|
| 85 |
"-hls_playlist_type", "vod", "-f", "hls", str(output_path)
|
| 86 |
]
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
error_file_path = tempfile.gettempdir() + "/error.txt"
|
| 94 |
-
with open(error_file_path, 'w') as error_file:
|
| 95 |
-
error_file.write("ffmpeg command failed.")
|
| 96 |
-
return error_file_path
|
| 97 |
-
except subprocess.TimeoutExpired:
|
| 98 |
-
logging.exception("ffmpeg command timed out.")
|
| 99 |
-
return "ffmpeg command timed out."
|
| 100 |
-
except FileNotFoundError:
|
| 101 |
-
logging.exception("ffmpeg is not installed.")
|
| 102 |
-
return "ffmpeg is not installed."
|
| 103 |
-
except Exception as e:
|
| 104 |
-
logging.exception("An error occurred.")
|
| 105 |
-
return f"An error occurred: {str(e)}"
|
| 106 |
-
|
| 107 |
-
# We need to modify this part if you want to return all converted videos
|
| 108 |
-
# Here we only return the last converted video
|
| 109 |
-
# Check if output_path is None after the loop
|
| 110 |
-
if output_path is None:
|
| 111 |
return "No resolution was selected."
|
| 112 |
|
| 113 |
-
|
| 114 |
|
| 115 |
if upload:
|
| 116 |
-
return upload_to_web3_storage(api_key,
|
| 117 |
else:
|
| 118 |
-
return
|
| 119 |
|
| 120 |
def main():
|
| 121 |
video_file = gr.inputs.File(label="Video File")
|
|
@@ -125,9 +108,9 @@ def main():
|
|
| 125 |
choices=[None, "1:1", "4:3", "3:2", "5:4", "16:9", "21:9",
|
| 126 |
"1.85:1", "2.35:1", "3:1", "360", "9:16", "16:9",
|
| 127 |
"2:1", "1:2", "9:1"],
|
| 128 |
-
label="Aspect Ratio", default=
|
| 129 |
resolution = gr.components.Dropdown(
|
| 130 |
-
choices=["480p", "720p", "1080p", "1440p", "2160p", "4320p"], label="Resolution", multiple=True, default=
|
| 131 |
video_url = gr.inputs.Textbox(label="Video URL")
|
| 132 |
api_key = gr.inputs.Textbox(label="web3.storage API Key")
|
| 133 |
upload = gr.inputs.Checkbox(label="Upload to web3.storage", default=False)
|
|
|
|
| 8 |
import requests
|
| 9 |
from urllib.parse import urlparse
|
| 10 |
|
| 11 |
+
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
|
| 14 |
def download_file(url, destination):
|
|
|
|
| 70 |
# Check if resolution is a list, if not convert it to a list
|
| 71 |
resolution = [resolution] if not isinstance(resolution, list) else resolution
|
| 72 |
|
| 73 |
+
output_paths = [] # Define output_paths as an empty list
|
| 74 |
|
| 75 |
for res in resolution:
|
| 76 |
# Skip if res is boolean
|
|
|
|
| 86 |
"-hls_playlist_type", "vod", "-f", "hls", str(output_path)
|
| 87 |
]
|
| 88 |
|
| 89 |
+
# Rest of the function...
|
| 90 |
+
|
| 91 |
+
output_paths.append(output_path) # Append the output_path to output_paths
|
| 92 |
+
|
| 93 |
+
if not output_paths:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
return "No resolution was selected."
|
| 95 |
|
| 96 |
+
output_copy_paths = [shutil.copy2(path, tempfile.gettempdir()) for path in output_paths]
|
| 97 |
|
| 98 |
if upload:
|
| 99 |
+
return [upload_to_web3_storage(api_key, path) for path in output_copy_paths]
|
| 100 |
else:
|
| 101 |
+
return output_copy_paths
|
| 102 |
|
| 103 |
def main():
|
| 104 |
video_file = gr.inputs.File(label="Video File")
|
|
|
|
| 108 |
choices=[None, "1:1", "4:3", "3:2", "5:4", "16:9", "21:9",
|
| 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", multiple=True, default="1080p")
|
| 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)
|