Prthameshh commited on
Commit
ae01c95
·
verified ·
1 Parent(s): ab1abbd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import yt_dlp
4
+ import os
5
+ import numpy as np
6
+
7
+ def download_video(url, download_path):
8
+ """
9
+ Download a video from YouTube using yt-dlp.
10
+
11
+ Parameters:
12
+ url (str): The URL of the video to download.
13
+ download_path (str): Directory to save the downloaded video.
14
+
15
+ Returns:
16
+ str: Path to the downloaded video
17
+ """
18
+ # Create download directory if it doesn't exist
19
+ os.makedirs(download_path, exist_ok=True)
20
+
21
+ ydl_opts = {
22
+ 'format': 'best',
23
+ 'outtmpl': f'{download_path}/%(title)s.%(ext)s',
24
+ 'noplaylist': True,
25
+ 'quiet': False,
26
+ }
27
+
28
+ try:
29
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
30
+ info_dict = ydl.extract_info(url, download=True)
31
+ video_filename = ydl.prepare_filename(info_dict)
32
+ return video_filename
33
+ except Exception as e:
34
+ raise gr.Error(f"Video download failed: {e}")
35
+
36
+ def process_video(input_path, brightness_threshold=100, slow_factor=2):
37
+ """
38
+ Process the input video with brightness analysis and slow motion.
39
+
40
+ Parameters:
41
+ input_path (str): Path to the input video
42
+ brightness_threshold (int): Threshold for brightness classification
43
+ slow_factor (int): Factor to slow down the video
44
+
45
+ Returns:
46
+ str: Path to the processed video
47
+ """
48
+ # Ensure output directory exists
49
+ output_dir = 'processed_videos'
50
+ os.makedirs(output_dir, exist_ok=True)
51
+
52
+ # Generate output filename
53
+ output_path = os.path.join(output_dir, 'processed_video.mp4')
54
+
55
+ # Open the input video
56
+ video = cv2.VideoCapture(input_path)
57
+
58
+ # Get video properties
59
+ fps = int(video.get(cv2.CAP_PROP_FPS))
60
+ width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
61
+ height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
62
+
63
+ # Adjust FPS for slower playback
64
+ new_fps = fps // slow_factor
65
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
66
+ writer = cv2.VideoWriter(output_path, fourcc, new_fps, (width, height))
67
+
68
+ def calculate_brightness(frame):
69
+ """Calculate the brightness of a frame using the V channel in HSV color space."""
70
+ hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
71
+ return np.mean(hsv[:, :, 2])
72
+
73
+ # Process each frame
74
+ while True:
75
+ ret, frame = video.read()
76
+ if not ret:
77
+ break
78
+
79
+ # Calculate brightness and classify
80
+ brightness = calculate_brightness(frame)
81
+ if brightness > brightness_threshold:
82
+ text = f"Morning: {brightness:.2f}%"
83
+ text_color = (0, 255, 0) # Green for morning
84
+ else:
85
+ text = f"Night: {brightness:.2f}%"
86
+ text_color = (255, 0, 0) # Red for night
87
+
88
+ # Overlay text on the frame
89
+ cv2.putText(frame, text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
90
+
91
+ # Write the frame to the output video
92
+ writer.write(frame)
93
+
94
+ # Release resources
95
+ video.release()
96
+ writer.release()
97
+
98
+ return output_path
99
+
100
+ def process_youtube_video(youtube_url, brightness_threshold, slow_factor):
101
+ """
102
+ Combine video download and processing with error handling.
103
+
104
+ Parameters:
105
+ youtube_url (str): YouTube video URL
106
+ brightness_threshold (int): Threshold for brightness classification
107
+ slow_factor (int): Factor to slow down the video
108
+
109
+ Returns:
110
+ str: Path to the processed video
111
+ """
112
+ try:
113
+ # Download video
114
+ downloaded_video_path = download_video(youtube_url, 'downloads')
115
+
116
+ # Process video
117
+ processed_video_path = process_video(
118
+ downloaded_video_path,
119
+ brightness_threshold,
120
+ slow_factor
121
+ )
122
+
123
+ return processed_video_path
124
+ except Exception as e:
125
+ raise gr.Error(f"Error processing video: {e}")
126
+
127
+ # Create Gradio Interface
128
+ demo = gr.Interface(
129
+ fn=process_youtube_video,
130
+ inputs=[
131
+ gr.Textbox(label="YouTube Video URL"),
132
+ gr.Slider(minimum=0, maximum=255, value=100, label="Brightness Threshold"),
133
+ gr.Slider(minimum=1, maximum=10, value=2, step=1, label="Slow Motion Factor")
134
+ ],
135
+ outputs=gr.Video(label="Processed Video"),
136
+ title="YouTube Video Brightness Analyzer",
137
+ description="Upload a YouTube video link and analyze its brightness with slow-motion effect."
138
+ )
139
+
140
+ # Launch the interface
141
+ if __name__ == "__main__":
142
+ demo.launch()