Politrees commited on
Commit
76ecead
·
verified ·
1 Parent(s): cf21461

Update steganography.py

Browse files
Files changed (1) hide show
  1. steganography.py +13 -25
steganography.py CHANGED
@@ -119,26 +119,19 @@ def gradio_image_to_audio_fn(upload_image):
119
  def gradio_decode_fn(upload_audio):
120
  return display_audio_spectrogram(upload_audio)
121
 
122
-
123
- def display_progress(percent, message, progress=gr.Progress()):
124
- progress(percent, desc=message)
125
-
126
- def extract_audio(video_path, progress):
127
- display_progress(0.1, "Extracting audio from video", progress)
128
  try:
129
  video = VideoFileClip(video_path)
130
  if video.audio is None:
131
  raise ValueError("No audio found in the video")
132
  audio_path = "extracted_audio.wav"
133
  video.audio.write_audiofile(audio_path)
134
- display_progress(0.2, "Audio extracted", progress)
135
  return audio_path
136
  except Exception as e:
137
- display_progress(0.2, f"Failed to extract audio: {e}", progress)
138
  return None
139
 
140
- def extract_frames(video_path, progress):
141
- display_progress(0.3, "Extracting frames from video", progress)
142
  try:
143
  video = cv2.VideoCapture(video_path)
144
  frames = []
@@ -147,10 +140,9 @@ def extract_frames(video_path, progress):
147
  frames.append(frame)
148
  success, frame = video.read()
149
  video.release()
150
- display_progress(0.4, "Frames extracted", progress)
151
  return frames
152
  except Exception as e:
153
- display_progress(0.4, f"Failed to extract frames: {e}", progress)
154
  return None
155
 
156
  def frame_to_spectrogram(frame, sr=DEFAULT_SAMPLE_RATE):
@@ -173,7 +165,7 @@ def save_spectrogram_image(S, frame_number, temp_dir):
173
  plt.close()
174
  return image_path
175
 
176
- def process_video_frames(frames, sr=DEFAULT_SAMPLE_RATE, temp_dir=None, progress=gr.Progress()):
177
  processed_frames = []
178
  total_frames = len(frames)
179
  for i, frame in enumerate(frames):
@@ -182,8 +174,6 @@ def process_video_frames(frames, sr=DEFAULT_SAMPLE_RATE, temp_dir=None, progress
182
  image_path = save_spectrogram_image(S, i, temp_dir)
183
  processed_frame = cv2.imread(image_path)
184
  processed_frames.append(processed_frame)
185
- display_progress(0.5 + int((i + 1) / total_frames * 0.7), f"Frame processing {i + 1}/{total_frames}", progress)
186
- display_progress(0.8, "All frames processed", progress)
187
  return processed_frames
188
 
189
  def save_video_from_frames(frames, output_path, fps=30):
@@ -193,18 +183,16 @@ def save_video_from_frames(frames, output_path, fps=30):
193
  video.write(frame)
194
  video.release()
195
 
196
- def add_audio_to_video(video_path, audio_path, output_path, progress):
197
- display_progress(0.9, "Adding audio back to video", progress)
198
  try:
199
  video = VideoFileClip(video_path)
200
  audio = AudioFileClip(audio_path)
201
  final_video = video.set_audio(audio)
202
  final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
203
- display_progress(1, "Video's ready", progress)
204
  except Exception as e:
205
- display_progress(1, f"Failed to add audio to video: {e}", progress)
206
 
207
- def process_video(video_path, progress=gr.Progress()):
208
  try:
209
  video = VideoFileClip(video_path)
210
  if video.duration > 10:
@@ -215,19 +203,19 @@ def process_video(video_path, progress=gr.Progress()):
215
  except Exception as e:
216
  return f"Failed to load video: {e}"
217
 
218
- audio_path = extract_audio(video_path, progress)
219
  if audio_path is None:
220
  return "Failed to extract audio from video."
221
- frames = extract_frames(video_path, progress)
222
  if frames is None:
223
  return "Failed to extract frames from video."
224
 
225
  with tempfile.TemporaryDirectory() as temp_dir:
226
- processed_frames = process_video_frames(frames, temp_dir=temp_dir, progress=progress)
227
  temp_video_path = os.path.join(temp_dir, 'processed_video.mp4')
228
  save_video_from_frames(processed_frames, temp_video_path)
229
  output_video_path = 'output_video_with_audio.mp4'
230
- add_audio_to_video(temp_video_path, audio_path, output_video_path, progress)
231
  return output_video_path
232
 
233
  def create_gradio_interface():
@@ -286,4 +274,4 @@ def create_gradio_interface():
286
 
287
  if __name__ == "__main__":
288
  txt2spec = create_gradio_interface()
289
- txt2spec.launch(share=True)
 
119
  def gradio_decode_fn(upload_audio):
120
  return display_audio_spectrogram(upload_audio)
121
 
122
+ def extract_audio(video_path):
 
 
 
 
 
123
  try:
124
  video = VideoFileClip(video_path)
125
  if video.audio is None:
126
  raise ValueError("No audio found in the video")
127
  audio_path = "extracted_audio.wav"
128
  video.audio.write_audiofile(audio_path)
 
129
  return audio_path
130
  except Exception as e:
131
+ logging.error(f"Failed to extract audio: {e}")
132
  return None
133
 
134
+ def extract_frames(video_path):
 
135
  try:
136
  video = cv2.VideoCapture(video_path)
137
  frames = []
 
140
  frames.append(frame)
141
  success, frame = video.read()
142
  video.release()
 
143
  return frames
144
  except Exception as e:
145
+ logging.error(f"Failed to extract frames: {e}")
146
  return None
147
 
148
  def frame_to_spectrogram(frame, sr=DEFAULT_SAMPLE_RATE):
 
165
  plt.close()
166
  return image_path
167
 
168
+ def process_video_frames(frames, sr=DEFAULT_SAMPLE_RATE, temp_dir=None):
169
  processed_frames = []
170
  total_frames = len(frames)
171
  for i, frame in enumerate(frames):
 
174
  image_path = save_spectrogram_image(S, i, temp_dir)
175
  processed_frame = cv2.imread(image_path)
176
  processed_frames.append(processed_frame)
 
 
177
  return processed_frames
178
 
179
  def save_video_from_frames(frames, output_path, fps=30):
 
183
  video.write(frame)
184
  video.release()
185
 
186
+ def add_audio_to_video(video_path, audio_path, output_path):
 
187
  try:
188
  video = VideoFileClip(video_path)
189
  audio = AudioFileClip(audio_path)
190
  final_video = video.set_audio(audio)
191
  final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
 
192
  except Exception as e:
193
+ logging.error(f"Failed to add audio to video: {e}")
194
 
195
+ def process_video(video_path):
196
  try:
197
  video = VideoFileClip(video_path)
198
  if video.duration > 10:
 
203
  except Exception as e:
204
  return f"Failed to load video: {e}"
205
 
206
+ audio_path = extract_audio(video_path)
207
  if audio_path is None:
208
  return "Failed to extract audio from video."
209
+ frames = extract_frames(video_path)
210
  if frames is None:
211
  return "Failed to extract frames from video."
212
 
213
  with tempfile.TemporaryDirectory() as temp_dir:
214
+ processed_frames = process_video_frames(frames, temp_dir=temp_dir)
215
  temp_video_path = os.path.join(temp_dir, 'processed_video.mp4')
216
  save_video_from_frames(processed_frames, temp_video_path)
217
  output_video_path = 'output_video_with_audio.mp4'
218
+ add_audio_to_video(temp_video_path, audio_path, output_video_path)
219
  return output_video_path
220
 
221
  def create_gradio_interface():
 
274
 
275
  if __name__ == "__main__":
276
  txt2spec = create_gradio_interface()
277
+ txt2spec.launch(share=True)