Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from moviepy.editor import VideoFileClip
|
|
7 |
import gradio as gr
|
8 |
import requests
|
9 |
from urllib.parse import urlparse
|
|
|
10 |
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
|
@@ -42,7 +43,14 @@ def get_aspect_ratio(input_path, aspect_ratio):
|
|
42 |
video = VideoFileClip(str(input_path))
|
43 |
return f"{video.size[0]}:{video.size[1]}"
|
44 |
|
45 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
"""Converts a video to HLS format, adjusting the quality and aspect ratio as necessary."""
|
47 |
with tempfile.TemporaryDirectory() as temp_dir:
|
48 |
temp_dir = Path(temp_dir)
|
@@ -74,8 +82,15 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
|
|
74 |
|
75 |
# Copy the output to a new temporary file that won't be deleted when the
|
76 |
# TemporaryDirectory context manager exits.
|
77 |
-
output_copy_path = shutil.copy2(output_path, tempfile.
|
|
|
|
|
78 |
|
|
|
|
|
|
|
|
|
|
|
79 |
return output_copy_path
|
80 |
|
81 |
def main():
|
@@ -88,8 +103,10 @@ def main():
|
|
88 |
"2:1", "1:2", "9:1"],
|
89 |
label="Aspect Ratio", default=None)
|
90 |
video_url = gr.inputs.Textbox(label="Video URL")
|
|
|
|
|
91 |
|
92 |
-
gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs='
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
-
main()
|
|
|
7 |
import gradio as gr
|
8 |
import requests
|
9 |
from urllib.parse import urlparse
|
10 |
+
from web3.storage import Web3Storage
|
11 |
|
12 |
logging.basicConfig(level=logging.INFO)
|
13 |
|
|
|
43 |
video = VideoFileClip(str(input_path))
|
44 |
return f"{video.size[0]}:{video.size[1]}"
|
45 |
|
46 |
+
def upload_to_web3_storage(api_key, path):
|
47 |
+
"""Uploads a file to web3.storage."""
|
48 |
+
with open(path, 'rb') as f:
|
49 |
+
storage = Web3Storage.new(token=api_key)
|
50 |
+
cid = storage.put_file(f)
|
51 |
+
return f"https://dweb.link/ipfs/{cid}"
|
52 |
+
|
53 |
+
def convert_video(video_file, quality, aspect_ratio, video_url, api_key, web3_upload):
|
54 |
"""Converts a video to HLS format, adjusting the quality and aspect ratio as necessary."""
|
55 |
with tempfile.TemporaryDirectory() as temp_dir:
|
56 |
temp_dir = Path(temp_dir)
|
|
|
82 |
|
83 |
# Copy the output to a new temporary file that won't be deleted when the
|
84 |
# TemporaryDirectory context manager exits.
|
85 |
+
output_copy_path = shutil.copy2(output_path, tempfile.gettemp# The previous message was cut off, let's complete it here.
|
86 |
+
|
87 |
+
output_copy_path = shutil.copy2(output_path, tempfile.gettempdir())
|
88 |
|
89 |
+
# If the user wants to upload to web3, do that and return the URL.
|
90 |
+
# Otherwise, just return the local path.
|
91 |
+
if web3_upload:
|
92 |
+
return upload_to_web3_storage(api_key, output_copy_path)
|
93 |
+
else:
|
94 |
return output_copy_path
|
95 |
|
96 |
def main():
|
|
|
103 |
"2:1", "1:2", "9:1"],
|
104 |
label="Aspect Ratio", default=None)
|
105 |
video_url = gr.inputs.Textbox(label="Video URL")
|
106 |
+
api_key = gr.inputs.Textbox(label="web3.storage API Key")
|
107 |
+
web3_upload = gr.inputs.Checkbox(label="Upload to web3.storage")
|
108 |
|
109 |
+
gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url, api_key, web3_upload], outputs='text').launch()
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
+
main()
|