Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,68 +7,80 @@ 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 |
-
def encode(file_path
|
| 11 |
"""
|
| 12 |
Encodes a file by splitting it into chunks, rendering frames, and creating a video from the frames.
|
|
|
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
def decode(output_file
|
| 40 |
"""
|
| 41 |
Decodes a video by extracting frames, recovering binary files, and merging the chunks back into the original file.
|
|
|
|
| 42 |
"""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
def encode_interface(file):
|
|
|
|
| 69 |
return encode(file.name)
|
| 70 |
|
| 71 |
def decode_interface(video, output_file):
|
|
|
|
| 72 |
return decode(output_file)
|
| 73 |
|
| 74 |
# Gradio interface
|
|
@@ -78,20 +90,35 @@ with gr.Blocks() as demo:
|
|
| 78 |
with gr.Tab("Encode"):
|
| 79 |
file_input = gr.File(label="Upload file to encode")
|
| 80 |
encode_button = gr.Button("Encode")
|
| 81 |
-
|
| 82 |
video_output = gr.Video(label="Encoded Video")
|
| 83 |
download_button = gr.Button("Download Encoded Video")
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
with gr.Tab("Decode"):
|
| 89 |
video_input = gr.Video(label="Upload video to decode")
|
| 90 |
output_file_input = gr.Textbox(label="Output file name")
|
| 91 |
decode_button = gr.Button("Decode")
|
| 92 |
-
decode_progress = gr.Progress
|
| 93 |
|
| 94 |
-
decode_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# Launch the Gradio app
|
| 97 |
-
demo.launch()
|
|
|
|
| 7 |
from modules.frame_decoder import recover_binary_files
|
| 8 |
from modules.merge import merge_cunks
|
| 9 |
|
| 10 |
+
def encode(file_path):
|
| 11 |
"""
|
| 12 |
Encodes a file by splitting it into chunks, rendering frames, and creating a video from the frames.
|
| 13 |
+
Streams progress updates as text.
|
| 14 |
"""
|
| 15 |
+
total_steps = 5
|
| 16 |
+
current_step = 0
|
| 17 |
+
|
| 18 |
+
# Step 1: Splitting file into chunks
|
| 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 |
+
current_step += 1
|
| 27 |
+
|
| 28 |
+
# Step 3: Creating video from frames
|
| 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 |
+
def decode(output_file):
|
| 45 |
"""
|
| 46 |
Decodes a video by extracting frames, recovering binary files, and merging the chunks back into the original file.
|
| 47 |
+
Streams progress updates as text.
|
| 48 |
"""
|
| 49 |
+
total_steps = 5
|
| 50 |
+
current_step = 0
|
| 51 |
+
|
| 52 |
+
# Step 1: Extracting frames from video
|
| 53 |
+
yield f"Progress: {int((current_step/total_steps)*100)}% - Extracting frames from video...", None
|
| 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 |
+
current_step += 1
|
| 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 |
def encode_interface(file):
|
| 79 |
+
# Start encoding and stream progress updates.
|
| 80 |
return encode(file.name)
|
| 81 |
|
| 82 |
def decode_interface(video, output_file):
|
| 83 |
+
# Start decoding and stream progress updates.
|
| 84 |
return decode(output_file)
|
| 85 |
|
| 86 |
# Gradio interface
|
|
|
|
| 90 |
with gr.Tab("Encode"):
|
| 91 |
file_input = gr.File(label="Upload file to encode")
|
| 92 |
encode_button = gr.Button("Encode")
|
| 93 |
+
progress_output = gr.Textbox(label="Encoding Progress", interactive=False)
|
| 94 |
video_output = gr.Video(label="Encoded Video")
|
| 95 |
download_button = gr.Button("Download Encoded Video")
|
| 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 |
video_input = gr.Video(label="Upload video to decode")
|
| 112 |
output_file_input = gr.Textbox(label="Output file name")
|
| 113 |
decode_button = gr.Button("Decode")
|
| 114 |
+
decode_progress = gr.Textbox(label="Decoding Progress", interactive=False)
|
| 115 |
|
| 116 |
+
decode_button.click(
|
| 117 |
+
decode_interface,
|
| 118 |
+
inputs=[video_input, output_file_input],
|
| 119 |
+
outputs=decode_progress,
|
| 120 |
+
stream=True
|
| 121 |
+
)
|
| 122 |
|
| 123 |
# Launch the Gradio app
|
| 124 |
+
demo.launch()
|