Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import shutil
|
| 3 |
import os
|
| 4 |
from modules.split import split_file
|
|
@@ -7,118 +8,64 @@ from modules.video import create_video_from_images, extract_frames_from_video
|
|
| 7 |
from modules.frame_decoder import recover_binary_files
|
| 8 |
from modules.merge import merge_cunks
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
yield f"Progress: {int((current_step/total_steps)*100)}% - Splitting file into chunks...", None
|
| 20 |
-
split_file(input_file=file_path)
|
| 21 |
-
current_step += 1
|
| 22 |
-
|
| 23 |
-
# Step 2: Rendering frames from chunks
|
| 24 |
-
yield f"Progress: {int((current_step/total_steps)*100)}% - Rendering frames from chunks...", None
|
| 25 |
render_frame()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
yield f"Progress: {int((current_step/total_steps)*100)}% - Creating video from frames...", None
|
| 30 |
-
create_video_from_images()
|
| 31 |
-
current_step += 1
|
| 32 |
-
|
| 33 |
-
# Step 4: Cleaning up temporary files
|
| 34 |
-
yield f"Progress: {int((current_step/total_steps)*100)}% - Cleaning up temporary files...", None
|
| 35 |
shutil.rmtree("./chunks", ignore_errors=True)
|
| 36 |
shutil.rmtree("./images", ignore_errors=True)
|
| 37 |
shutil.rmtree("./extracted_frames", ignore_errors=True)
|
| 38 |
shutil.rmtree("./recovered", ignore_errors=True)
|
| 39 |
-
current_step += 1
|
| 40 |
-
|
| 41 |
-
# Step 5: Completed
|
| 42 |
-
yield f"Progress: {int((current_step/total_steps)*100)}% - Encoding completed! Encoded video is ready.", "encoded_video.mp4"
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
extract_frames_from_video()
|
| 55 |
-
current_step += 1
|
| 56 |
-
|
| 57 |
-
# Step 2: Recovering binary files from frames
|
| 58 |
-
yield f"Progress: {int((current_step/total_steps)*100)}% - Recovering binary files from frames...", None
|
| 59 |
recover_binary_files()
|
| 60 |
-
current_step += 1
|
| 61 |
-
|
| 62 |
-
# Step 3: Merging chunks to reconstruct the original file
|
| 63 |
-
yield f"Progress: {int((current_step/total_steps)*100)}% - Merging chunks to reconstruct the original file...", None
|
| 64 |
merge_cunks("./recovered", output_file)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# Step 4: Cleaning up temporary files
|
| 68 |
-
yield f"Progress: {int((current_step/total_steps)*100)}% - Cleaning up temporary files...", None
|
| 69 |
shutil.rmtree("./extracted_frames", ignore_errors=True)
|
| 70 |
shutil.rmtree("./chunks", ignore_errors=True)
|
| 71 |
shutil.rmtree("./recovered", ignore_errors=True)
|
| 72 |
shutil.rmtree("./images", ignore_errors=True)
|
| 73 |
-
current_step += 1
|
| 74 |
-
|
| 75 |
-
# Step 5: Completed
|
| 76 |
-
yield f"Progress: {int((current_step/total_steps)*100)}% - Decoding completed! File successfully reconstructed.", f"Decoding completed! Output file: {output_file}"
|
| 77 |
|
| 78 |
-
|
| 79 |
-
# Start encoding and stream progress updates.
|
| 80 |
-
return encode(file.name)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
# Gradio interface
|
| 87 |
-
with gr.Blocks() as demo:
|
| 88 |
-
gr.Markdown("# File Encoder/Decoder using Video Frames")
|
| 89 |
-
|
| 90 |
with gr.Tab("Encode"):
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
video_output = gr.
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
# Use stream=True to update progress in real time.
|
| 98 |
-
encode_button.click(
|
| 99 |
-
encode_interface,
|
| 100 |
-
inputs=file_input,
|
| 101 |
-
outputs=[progress_output, video_output],
|
| 102 |
-
stream=True
|
| 103 |
-
)
|
| 104 |
-
download_button.click(
|
| 105 |
-
lambda: "encoded_video.mp4",
|
| 106 |
-
inputs=None,
|
| 107 |
-
outputs=gr.File(label="Download Encoded Video")
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
with gr.Tab("Decode"):
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
inputs=[video_input, output_file_input],
|
| 119 |
-
outputs=decode_progress,
|
| 120 |
-
stream=True
|
| 121 |
-
)
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tempfile
|
| 3 |
import shutil
|
| 4 |
import os
|
| 5 |
from modules.split import split_file
|
|
|
|
| 8 |
from modules.frame_decoder import recover_binary_files
|
| 9 |
from modules.merge import merge_cunks
|
| 10 |
|
| 11 |
+
# π Encoding Function
|
| 12 |
+
def encode(file):
|
| 13 |
+
if not file:
|
| 14 |
+
return "No file uploaded!"
|
| 15 |
+
|
| 16 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_vid:
|
| 17 |
+
video_path = temp_vid.name
|
| 18 |
+
|
| 19 |
+
split_file(input_file=file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
render_frame()
|
| 21 |
+
create_video_from_images(output_video=video_path)
|
| 22 |
+
|
| 23 |
+
# Cleanup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
shutil.rmtree("./chunks", ignore_errors=True)
|
| 25 |
shutil.rmtree("./images", ignore_errors=True)
|
| 26 |
shutil.rmtree("./extracted_frames", ignore_errors=True)
|
| 27 |
shutil.rmtree("./recovered", ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
return video_path
|
| 30 |
+
|
| 31 |
+
# π Decoding Function
|
| 32 |
+
def decode(video):
|
| 33 |
+
if not video:
|
| 34 |
+
return "No video uploaded!"
|
| 35 |
+
|
| 36 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
| 37 |
+
output_file = temp_file.name
|
| 38 |
+
|
| 39 |
+
extract_frames_from_video(video)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
recover_binary_files()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
merge_cunks("./recovered", output_file)
|
| 42 |
+
|
| 43 |
+
# Cleanup
|
|
|
|
|
|
|
| 44 |
shutil.rmtree("./extracted_frames", ignore_errors=True)
|
| 45 |
shutil.rmtree("./chunks", ignore_errors=True)
|
| 46 |
shutil.rmtree("./recovered", ignore_errors=True)
|
| 47 |
shutil.rmtree("./images", ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
return output_file
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# π¨ Gradio UI
|
| 52 |
+
with gr.Blocks() as app:
|
| 53 |
+
gr.Markdown("# π File to Video Encoder & Decoder")
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
with gr.Tab("Encode"):
|
| 56 |
+
with gr.Row():
|
| 57 |
+
file_input = gr.File(label="Upload a file to encode")
|
| 58 |
+
encode_button = gr.Button("Encode")
|
| 59 |
+
video_output = gr.File(label="Download encoded video")
|
| 60 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
with gr.Tab("Decode"):
|
| 62 |
+
with gr.Row():
|
| 63 |
+
video_input = gr.File(label="Upload encoded video")
|
| 64 |
+
decode_button = gr.Button("Decode")
|
| 65 |
+
file_output = gr.File(label="Download decoded file")
|
| 66 |
+
|
| 67 |
+
encode_button.click(encode, inputs=[file_input], outputs=[video_output])
|
| 68 |
+
decode_button.click(decode, inputs=[video_input], outputs=[file_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# π Run the app
|
| 71 |
+
app.launch()
|