ar08 commited on
Commit
76feb60
Β·
verified Β·
1 Parent(s): 75c8ca3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -97
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
- 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
87
- with gr.Blocks() as demo:
88
- gr.Markdown("# File Encoder/Decoder using Video Frames")
89
-
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()
 
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()