Politrees commited on
Commit
cc891cd
1 Parent(s): 62f9fc5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ video = VideoFileClip(video_path)
20
+ audio_path = "extracted_audio.wav"
21
+ video.audio.write_audiofile(audio_path)
22
+ display_progress(0.2, "Audio extracted", progress)
23
+ return audio_path
24
+
25
+ # Function for dividing video into frames
26
+ def extract_frames(video_path, progress):
27
+ display_progress(0.3, "Extract frames from video", progress)
28
+ video = cv2.VideoCapture(video_path)
29
+ frames = []
30
+ success, frame = video.read()
31
+ while success:
32
+ frames.append(frame)
33
+ success, frame = video.read()
34
+ video.release()
35
+ display_progress(0.4, "Frames extracted", progress)
36
+ return frames
37
+
38
+ # Convert frame to spectrogram
39
+ def frame_to_spectrogram(frame, sr=22050):
40
+ gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
41
+ S = np.flipud(gray_frame.astype(np.float32) / 255.0 * 100.0)
42
+ y = librosa.griffinlim(S)
43
+ return y
44
+
45
+ # Saving audio
46
+ def save_audio(y, sr=22050):
47
+ audio_path = 'output_frame_audio.wav'
48
+ sf.write(audio_path, y, sr)
49
+ return audio_path
50
+
51
+ # Saving frame spectrogram
52
+ def save_spectrogram_image(S, frame_number, temp_dir):
53
+ plt.figure(figsize=(10, 4))
54
+ librosa.display.specshow(S)
55
+ plt.tight_layout()
56
+ image_path = os.path.join(temp_dir, f'spectrogram_frame_{frame_number}.png')
57
+ plt.savefig(image_path)
58
+ plt.close()
59
+ return image_path
60
+
61
+ # Processing all video frames
62
+ def process_video_frames(frames, sr=22050, temp_dir=None, progress=gr.Progress()):
63
+ processed_frames = []
64
+ total_frames = len(frames)
65
+ for i, frame in enumerate(frames):
66
+ y = frame_to_spectrogram(frame, sr)
67
+ S = librosa.feature.melspectrogram(y=y, sr=sr)
68
+ image_path = save_spectrogram_image(S, i, temp_dir)
69
+ processed_frame = cv2.imread(image_path)
70
+ processed_frames.append(processed_frame)
71
+ display_progress(0.5 + int((i + 1) / total_frames * 0.7), f"Frame processing {i + 1}/{total_frames}", progress)
72
+ display_progress(0.8, "All frames processed", progress)
73
+ return processed_frames
74
+
75
+ # Saving video from frames
76
+ def save_video_from_frames(frames, output_path, fps=30):
77
+ height, width, layers = frames[0].shape
78
+ video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
79
+ for frame in frames:
80
+ video.write(frame)
81
+ video.release()
82
+
83
+ # Adding audio back to video
84
+ def add_audio_to_video(video_path, audio_path, output_path, progress):
85
+ display_progress(0.9, "Adding audio back to video", progress)
86
+ video = VideoFileClip(video_path)
87
+ audio = AudioFileClip(audio_path)
88
+ final_video = video.set_audio(audio)
89
+ final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
90
+ display_progress(1, "Video's ready", progress)
91
+
92
+ # Gradio interface
93
+ def process_video(video_path, progress=gr.Progress()):
94
+ audio_path = extract_audio(video_path, progress)
95
+ frames = extract_frames(video_path, progress)
96
+
97
+ # Creating a temporary folder for saving frames
98
+ with tempfile.TemporaryDirectory() as temp_dir:
99
+ processed_frames = process_video_frames(frames, temp_dir=temp_dir, progress=progress)
100
+ temp_video_path = os.path.join(temp_dir, 'processed_video.mp4')
101
+ save_video_from_frames(processed_frames, temp_video_path)
102
+ output_video_path = 'output_video_with_audio.mp4'
103
+ add_audio_to_video(temp_video_path, audio_path, output_video_path, progress)
104
+ return output_video_path
105
+
106
+ 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:
107
+
108
+ with gr.Group():
109
+ with gr.Row(variant='panel'):
110
+ with gr.Column():
111
+ gr.HTML("<center><h2><a href='https://t.me/pol1trees'>Telegram Channel</a></h2></center>")
112
+ with gr.Column():
113
+ gr.HTML("<center><h2><a href='https://t.me/+GMTP7hZqY0E4OGRi'>Telegram Chat</a></h2></center>")
114
+ with gr.Column():
115
+ gr.HTML("<center><h2><a href='https://www.youtube.com/channel/UCHb3fZEVxUisnqLqCrEM8ZA'>YouTube</a></h2></center>")
116
+ with gr.Column():
117
+ gr.HTML("<center><h2><a href='https://github.com/Bebra777228/Audio-Steganography'>GitHub</a></h2></center>")
118
+
119
+ with gr.Column(variant='panel'):
120
+ video_input = gr.Video(label="Upload video")
121
+ with gr.Column(variant='panel'):
122
+ generate_button = gr.Button("Generate")
123
+ with gr.Column(variant='panel'):
124
+ video_output = gr.Video(label="VideoSpectrogram")
125
+
126
+ def gradio_video_process_fn(video_input, progress=gr.Progress()):
127
+ return process_video(video_input, progress)
128
+
129
+ generate_button.click(
130
+ gradio_video_process_fn,
131
+ inputs=[video_input],
132
+ outputs=[video_output]
133
+ )
134
+
135
+ iface.launch(share=True)