Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import zipfile
|
4 |
+
import cv2
|
5 |
+
import base64
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
# Function to extract zip file
|
9 |
+
def extract_zip(input_zip, output_folder):
|
10 |
+
with zipfile.ZipFile(input_zip, 'r') as zip_ref:
|
11 |
+
zip_ref.extractall(output_folder)
|
12 |
+
|
13 |
+
# Function to convert images to video
|
14 |
+
def images_to_video(folder_path, output_video):
|
15 |
+
images = [img for img in os.listdir(folder_path) if img.endswith(".png")]
|
16 |
+
frame = cv2.imread(os.path.join(folder_path, images[0]))
|
17 |
+
height, width, layers = frame.shape
|
18 |
+
|
19 |
+
video = cv2.VideoWriter(output_video, cv2.VideoWriter_fourcc(*'mpg2'), 1, (width, height))
|
20 |
+
|
21 |
+
for image in images:
|
22 |
+
video.write(cv2.imread(os.path.join(folder_path, image)))
|
23 |
+
|
24 |
+
cv2.destroyAllWindows()
|
25 |
+
video.release()
|
26 |
+
|
27 |
+
# Function to get base64 of a file for download
|
28 |
+
def get_video_base64(binary_file):
|
29 |
+
with open(binary_file, 'rb') as file:
|
30 |
+
data = file.read()
|
31 |
+
return base64.b64encode(data).decode()
|
32 |
+
|
33 |
+
st.title("ZipToVideo 🎥📦")
|
34 |
+
|
35 |
+
uploaded_file = st.file_uploader("Choose a ZIP file", type="zip")
|
36 |
+
if uploaded_file is not None:
|
37 |
+
file_name = uploaded_file.name
|
38 |
+
st.write("Processing uploaded file:", file_name)
|
39 |
+
|
40 |
+
# Save uploaded file
|
41 |
+
with open(file_name, "wb") as f:
|
42 |
+
f.write(uploaded_file.getbuffer())
|
43 |
+
|
44 |
+
output_folder = file_name.split('.')[0]
|
45 |
+
if not os.path.exists(output_folder):
|
46 |
+
os.mkdir(output_folder)
|
47 |
+
|
48 |
+
# Extract ZIP
|
49 |
+
extract_zip(file_name, output_folder)
|
50 |
+
|
51 |
+
# Convert to Video
|
52 |
+
output_video = f"{output_folder}.mpg"
|
53 |
+
images_to_video(output_folder, output_video)
|
54 |
+
|
55 |
+
# Generate download link
|
56 |
+
video_file_base64 = get_video_base64(output_video)
|
57 |
+
href = f'<a href="data:file/mpg;base64,{video_file_base64}" download="{output_video}">Download Video File</a>'
|
58 |
+
st.markdown(href, unsafe_allow_html=True)
|