ar08 commited on
Commit
75c8ca3
·
verified ·
1 Parent(s): d171de2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -54
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, progress=gr.Progress()):
11
  """
12
  Encodes a file by splitting it into chunks, rendering frames, and creating a video from the frames.
 
13
  """
14
- with progress.tqdm(total=5) as pbar:
15
- pbar.set_description("Splitting file into chunks...")
16
- split_file(input_file=file_path)
17
- pbar.update(1)
18
-
19
- pbar.set_description("Rendering frames from chunks...")
20
- render_frame()
21
- pbar.update(1)
22
-
23
- pbar.set_description("Creating video from frames...")
24
- create_video_from_images()
25
- pbar.update(1)
26
-
27
- pbar.set_description("Cleaning up temporary files...")
28
- shutil.rmtree("./chunks", ignore_errors=True)
29
- shutil.rmtree("./images", ignore_errors=True)
30
- shutil.rmtree("./extracted_frames", ignore_errors=True)
31
- shutil.rmtree("./recovered", ignore_errors=True)
32
- pbar.update(1)
33
-
34
- pbar.set_description("Encoding completed!")
35
- pbar.update(1)
 
 
 
36
 
37
- return "encoded_video.mp4"
 
38
 
39
- def decode(output_file, progress=gr.Progress()):
40
  """
41
  Decodes a video by extracting frames, recovering binary files, and merging the chunks back into the original file.
 
42
  """
43
- with progress.tqdm(total=5) as pbar:
44
- pbar.set_description("Extracting frames from video...")
45
- extract_frames_from_video()
46
- pbar.update(1)
47
-
48
- pbar.set_description("Recovering binary files from frames...")
49
- recover_binary_files()
50
- pbar.update(1)
51
-
52
- pbar.set_description("Merging chunks to reconstruct the original file...")
53
- merge_cunks("./recovered", output_file)
54
- pbar.update(1)
55
-
56
- pbar.set_description("Cleaning up temporary files...")
57
- shutil.rmtree("./extracted_frames", ignore_errors=True)
58
- shutil.rmtree("./chunks", ignore_errors=True)
59
- shutil.rmtree("./recovered", ignore_errors=True)
60
- shutil.rmtree("./images", ignore_errors=True)
61
- pbar.update(1)
62
-
63
- pbar.set_description("Decoding completed!")
64
- pbar.update(1)
 
 
 
65
 
66
- return f"Decoding completed! Output file: {output_file}"
 
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
- progress_bar = gr.Progress()
82
  video_output = gr.Video(label="Encoded Video")
83
  download_button = gr.Button("Download Encoded Video")
84
 
85
- encode_button.click(encode_interface, inputs=file_input, outputs=[video_output], progress=progress_bar)
86
- download_button.click(lambda: "encoded_video.mp4", inputs=None, outputs=gr.File(label="Download Encoded Video"))
 
 
 
 
 
 
 
 
 
 
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(decode_interface, inputs=[video_input, output_file_input], outputs=gr.Textbox(label="Status"), progress=decode_progress)
 
 
 
 
 
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()