KlaskyCsupoRoboSplaat87 commited on
Commit
5abf3ff
1 Parent(s): d2e23d4

Create app1.py

Browse files

This is the another app.

Files changed (1) hide show
  1. app1.py +162 -0
app1.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ from moviepy.editor import VideoFileClip, AudioFileClip
6
+ import librosa
7
+ import librosa.display
8
+ import soundfile as sf
9
+ import gradio as gr
10
+ import tempfile
11
+
12
+ # Function for displaying progress
13
+ def display_progress(percent, message, progress=gr.Progress()):
14
+ progress(percent, desc=message)
15
+
16
+ # Function for extracting audio from video
17
+ def extract_audio(video_path, progress):
18
+ display_progress(0.1, "Extracting audio from video", progress)
19
+ try:
20
+ video = VideoFileClip(video_path)
21
+ if video.audio is None:
22
+ raise ValueError("No audio found in the video")
23
+ audio_path = "extracted_audio.wav"
24
+ video.audio.write_audiofile(audio_path)
25
+ display_progress(0.2, "Audio extracted", progress)
26
+ return audio_path
27
+ except Exception as e:
28
+ display_progress(0.2, f"Failed to extract audio: {e}", progress)
29
+ return None
30
+
31
+ # Function for dividing video into frames
32
+ def extract_frames(video_path, progress):
33
+ display_progress(0.3, "Extracting frames from video", progress)
34
+ try:
35
+ video = cv2.VideoCapture(video_path)
36
+ frames = []
37
+ success, frame = video.read()
38
+ while success:
39
+ frames.append(frame)
40
+ success, frame = video.read()
41
+ video.release()
42
+ display_progress(0.4, "Frames extracted", progress)
43
+ return frames
44
+ except Exception as e:
45
+ display_progress(0.4, f"Failed to extract frames: {e}", progress)
46
+ return None
47
+
48
+ # Convert frame to spectrogram
49
+ def frame_to_spectrogram(frame, sr=22050):
50
+ gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
51
+ S = np.flipud(gray_frame.astype(np.float32) / 255.0 * 100.0)
52
+ y = librosa.griffinlim(S)
53
+ return y
54
+
55
+ # Saving audio
56
+ def save_audio(y, sr=22050):
57
+ audio_path = 'output_frame_audio.wav'
58
+ sf.write(audio_path, y, sr)
59
+ return audio_path
60
+
61
+ # Saving frame spectrogram
62
+ def save_spectrogram_image(S, frame_number, temp_dir):
63
+ plt.figure(figsize=(10, 4))
64
+ librosa.display.specshow(S)
65
+ plt.tight_layout()
66
+ image_path = os.path.join(temp_dir, f'spectrogram_frame_{frame_number}.png')
67
+ plt.savefig(image_path)
68
+ plt.close()
69
+ return image_path
70
+
71
+ # Processing all video frames
72
+ def process_video_frames(frames, sr=22050, temp_dir=None, progress=gr.Progress()):
73
+ processed_frames = []
74
+ total_frames = len(frames)
75
+ for i, frame in enumerate(frames):
76
+ y = frame_to_spectrogram(frame, sr)
77
+ S = librosa.feature.melspectrogram(y=y, sr=sr)
78
+ image_path = save_spectrogram_image(S, i, temp_dir)
79
+ processed_frame = cv2.imread(image_path)
80
+ processed_frames.append(processed_frame)
81
+ display_progress(0.5 + int((i + 1) / total_frames * 0.7), f"Frame processing {i + 1}/{total_frames}", progress)
82
+ display_progress(0.8, "All frames processed", progress)
83
+ return processed_frames
84
+
85
+ # Saving video from frames
86
+ def save_video_from_frames(frames, output_path, fps=30):
87
+ height, width, layers = frames[0].shape
88
+ video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
89
+ for frame in frames:
90
+ video.write(frame)
91
+ video.release()
92
+
93
+ # Adding audio back to video
94
+ def add_audio_to_video(video_path, audio_path, output_path, progress):
95
+ display_progress(0.9, "Adding audio back to video", progress)
96
+ try:
97
+ video = VideoFileClip(video_path)
98
+ audio = AudioFileClip(audio_path)
99
+ final_video = video.set_audio(audio)
100
+ final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
101
+ display_progress(1, "Video's ready", progress)
102
+ except Exception as e:
103
+ display_progress(1, f"Failed to add audio to video: {e}", progress)
104
+
105
+ # Gradio interface
106
+ def process_video(video_path, progress=gr.Progress()):
107
+ try:
108
+ video = VideoFileClip(video_path)
109
+ if video.duration > 10:
110
+ video = video.subclip(0, 10)
111
+ temp_trimmed_video_path = "trimmed_video.mp4"
112
+ video.write_videofile(temp_trimmed_video_path, codec='libx264')
113
+ video_path = temp_trimmed_video_path
114
+ except Exception as e:
115
+ return f"Failed to load video: {e}"
116
+
117
+ audio_path = extract_audio(video_path, progress)
118
+ if audio_path is None:
119
+ return "Failed to extract audio from video."
120
+ frames = extract_frames(video_path, progress)
121
+ if frames is None:
122
+ return "Failed to extract frames from video."
123
+
124
+ # Creating a temporary folder for saving frames
125
+ with tempfile.TemporaryDirectory() as temp_dir:
126
+ processed_frames = process_video_frames(frames, temp_dir=temp_dir, progress=progress)
127
+ temp_video_path = os.path.join(temp_dir, 'processed_video.mp4')
128
+ save_video_from_frames(processed_frames, temp_video_path)
129
+ output_video_path = 'output_video_with_audio.mp4'
130
+ add_audio_to_video(temp_video_path, audio_path, output_video_path, progress)
131
+ return output_video_path
132
+
133
+ with gr.Blocks(title='Video from Spectrogram', theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as iface:
134
+
135
+ with gr.Group():
136
+ with gr.Row(variant='panel'):
137
+ with gr.Column():
138
+ gr.HTML("<center><h2><a href='https://t.me/pol1trees'>Telegram Channel</a></h2></center>")
139
+ with gr.Column():
140
+ gr.HTML("<center><h2><a href='https://t.me/+GMTP7hZqY0E4OGRi'>Telegram Chat</a></h2></center>")
141
+ with gr.Column():
142
+ gr.HTML("<center><h2><a href='https://www.youtube.com/channel/UCHb3fZEVxUisnqLqCrEM8ZA'>YouTube</a></h2></center>")
143
+ with gr.Column():
144
+ gr.HTML("<center><h2><a href='https://github.com/Bebra777228/Audio-Steganography'>GitHub</a></h2></center>")
145
+
146
+ with gr.Column(variant='panel'):
147
+ video_input = gr.Video(label="Upload video")
148
+ with gr.Column(variant='panel'):
149
+ generate_button = gr.Button("Generate")
150
+ with gr.Column(variant='panel'):
151
+ video_output = gr.Video(label="VideoSpectrogram")
152
+
153
+ def gradio_video_process_fn(video_input, progress=gr.Progress()):
154
+ return process_video(video_input, progress)
155
+
156
+ generate_button.click(
157
+ gradio_video_process_fn,
158
+ inputs=[video_input],
159
+ outputs=[video_output]
160
+ )
161
+
162
+ iface.launch(share=True)