Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -43,23 +43,6 @@ def get_aspect_ratio(input_path, aspect_ratio):
|
|
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 using the HTTP API."""
|
48 |
-
headers = {"Authorization": f"Bearer {api_key}"}
|
49 |
-
with open(path, 'rb') as f:
|
50 |
-
response = requests.post(
|
51 |
-
"https://api.web3.storage/upload",
|
52 |
-
headers=headers,
|
53 |
-
data=f,
|
54 |
-
)
|
55 |
-
response.raise_for_status() # Raises an exception if the request failed
|
56 |
-
try:
|
57 |
-
cid = response.json()["value"]["cid"]
|
58 |
-
return f"https://dweb.link/ipfs/{cid}"
|
59 |
-
except KeyError:
|
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 create_master_playlist(output_paths, temp_dir):
|
64 |
"""Creates a master playlist .m3u8 file that includes all other .m3u8 files."""
|
65 |
master_playlist_path = temp_dir / "master_playlist.m3u8"
|
@@ -70,7 +53,7 @@ def create_master_playlist(output_paths, temp_dir):
|
|
70 |
f.write(f"{path.name}\n")
|
71 |
return master_playlist_path
|
72 |
|
73 |
-
def convert_video(video_file, quality, aspect_ratio, video_url
|
74 |
standard_resolutions = [4320, 2160, 1440, 1080, 720, 480, 360, 240, 144] # 8K, 4K, 2K, Full HD, HD, SD in pixels
|
75 |
|
76 |
with tempfile.TemporaryDirectory() as temp_dir:
|
@@ -129,9 +112,6 @@ def convert_video(video_file, quality, aspect_ratio, video_url, api_key, upload)
|
|
129 |
output_copy_paths = [shutil.copy2(path, tempfile.gettempdir()) for path in output_paths]
|
130 |
master_playlist_copy_path = shutil.copy2(master_playlist_path, tempfile.gettempdir())
|
131 |
|
132 |
-
if upload:
|
133 |
-
return [upload_to_web3_storage(api_key, path) for path in [master_playlist_copy_path] + output_copy_paths]
|
134 |
-
else:
|
135 |
return [master_playlist_copy_path] + output_copy_paths
|
136 |
|
137 |
|
@@ -159,12 +139,10 @@ def main():
|
|
159 |
"2:1", "1:2", "9:1"],
|
160 |
label="Aspect Ratio", default="16:9")
|
161 |
video_url = gr.inputs.Textbox(label="Video URL")
|
162 |
-
api_key = gr.inputs.Textbox(label="web3.storage API Key")
|
163 |
-
upload = gr.inputs.Checkbox(label="Upload to web3.storage", default=False)
|
164 |
|
165 |
gr.Interface(
|
166 |
convert_video,
|
167 |
-
inputs=[video_file, quality, aspect_ratio, video_url
|
168 |
outputs=gr.outputs.Textbox(),
|
169 |
output_processor=process_output,
|
170 |
allow_flagging=False,
|
|
|
43 |
video = VideoFileClip(str(input_path))
|
44 |
return f"{video.size[0]}:{video.size[1]}"
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
def create_master_playlist(output_paths, temp_dir):
|
47 |
"""Creates a master playlist .m3u8 file that includes all other .m3u8 files."""
|
48 |
master_playlist_path = temp_dir / "master_playlist.m3u8"
|
|
|
53 |
f.write(f"{path.name}\n")
|
54 |
return master_playlist_path
|
55 |
|
56 |
+
def convert_video(video_file, quality, aspect_ratio, video_url):
|
57 |
standard_resolutions = [4320, 2160, 1440, 1080, 720, 480, 360, 240, 144] # 8K, 4K, 2K, Full HD, HD, SD in pixels
|
58 |
|
59 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
112 |
output_copy_paths = [shutil.copy2(path, tempfile.gettempdir()) for path in output_paths]
|
113 |
master_playlist_copy_path = shutil.copy2(master_playlist_path, tempfile.gettempdir())
|
114 |
|
|
|
|
|
|
|
115 |
return [master_playlist_copy_path] + output_copy_paths
|
116 |
|
117 |
|
|
|
139 |
"2:1", "1:2", "9:1"],
|
140 |
label="Aspect Ratio", default="16:9")
|
141 |
video_url = gr.inputs.Textbox(label="Video URL")
|
|
|
|
|
142 |
|
143 |
gr.Interface(
|
144 |
convert_video,
|
145 |
+
inputs=[video_file, quality, aspect_ratio, video_url],
|
146 |
outputs=gr.outputs.Textbox(),
|
147 |
output_processor=process_output,
|
148 |
allow_flagging=False,
|