Spaces:
Sleeping
Sleeping
Update chatgpt-ad-maker.py
Browse files- chatgpt-ad-maker.py +40 -2
chatgpt-ad-maker.py
CHANGED
|
@@ -49,9 +49,10 @@ def process_video(video_path, dot_size=10, spacing=2, invert=False, fps=30):
|
|
| 49 |
orig_fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 50 |
orig_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 51 |
orig_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
output_fps = fps
|
| 55 |
|
| 56 |
max_height = 720
|
| 57 |
if orig_height > max_height:
|
|
@@ -71,11 +72,28 @@ def process_video(video_path, dot_size=10, spacing=2, invert=False, fps=30):
|
|
| 71 |
out = cv2.VideoWriter(output_path, fourcc, output_fps, (frame_width, frame_height), True)
|
| 72 |
|
| 73 |
try:
|
|
|
|
| 74 |
while cap.isOpened():
|
| 75 |
ret, frame = cap.read()
|
| 76 |
if not ret:
|
| 77 |
break
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if frame.shape[0] != frame_height:
|
| 80 |
frame = cv2.resize(frame, (frame_width, frame_height))
|
| 81 |
|
|
@@ -83,6 +101,8 @@ def process_video(video_path, dot_size=10, spacing=2, invert=False, fps=30):
|
|
| 83 |
dotted_frame = create_dot_effect(frame_rgb, dot_size, spacing, invert)
|
| 84 |
dotted_frame_bgr = cv2.cvtColor(dotted_frame, cv2.COLOR_GRAY2BGR)
|
| 85 |
out.write(dotted_frame_bgr)
|
|
|
|
|
|
|
| 86 |
|
| 87 |
finally:
|
| 88 |
cap.release()
|
|
@@ -90,6 +110,14 @@ def process_video(video_path, dot_size=10, spacing=2, invert=False, fps=30):
|
|
| 90 |
|
| 91 |
return output_path
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
with gr.Blocks(title="Dot Effect Generator") as iface:
|
| 94 |
gr.Markdown("# 🎨 Dot Effect Generator")
|
| 95 |
gr.Markdown("Transform media into stylized dot patterns")
|
|
@@ -115,6 +143,16 @@ with gr.Blocks(title="Dot Effect Generator") as iface:
|
|
| 115 |
with gr.Row():
|
| 116 |
vid_fps = gr.Slider(1, 60, 30, step=1, label="Output FPS")
|
| 117 |
vid_btn = gr.Button("Process Video", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
vid_btn.click(process_video, [vid_input, vid_dot_size, vid_spacing, vid_invert, vid_fps], vid_output)
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
|
|
|
| 49 |
orig_fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 50 |
orig_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 51 |
orig_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 52 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 53 |
|
| 54 |
+
# Ensure output FPS does not exceed input FPS
|
| 55 |
+
output_fps = min(fps, orig_fps)
|
| 56 |
|
| 57 |
max_height = 720
|
| 58 |
if orig_height > max_height:
|
|
|
|
| 72 |
out = cv2.VideoWriter(output_path, fourcc, output_fps, (frame_width, frame_height), True)
|
| 73 |
|
| 74 |
try:
|
| 75 |
+
frame_counter = 0
|
| 76 |
while cap.isOpened():
|
| 77 |
ret, frame = cap.read()
|
| 78 |
if not ret:
|
| 79 |
break
|
| 80 |
|
| 81 |
+
# Skip or duplicate frames based on FPS ratio
|
| 82 |
+
if output_fps < orig_fps:
|
| 83 |
+
# Skip frames to match the output FPS
|
| 84 |
+
if frame_counter % (orig_fps // output_fps) != 0:
|
| 85 |
+
frame_counter += 1
|
| 86 |
+
continue
|
| 87 |
+
elif output_fps > orig_fps:
|
| 88 |
+
# Duplicate frames to match the output FPS
|
| 89 |
+
for _ in range(output_fps // orig_fps):
|
| 90 |
+
if frame.shape[0] != frame_height:
|
| 91 |
+
frame = cv2.resize(frame, (frame_width, frame_height))
|
| 92 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 93 |
+
dotted_frame = create_dot_effect(frame_rgb, dot_size, spacing, invert)
|
| 94 |
+
dotted_frame_bgr = cv2.cvtColor(dotted_frame, cv2.COLOR_GRAY2BGR)
|
| 95 |
+
out.write(dotted_frame_bgr)
|
| 96 |
+
|
| 97 |
if frame.shape[0] != frame_height:
|
| 98 |
frame = cv2.resize(frame, (frame_width, frame_height))
|
| 99 |
|
|
|
|
| 101 |
dotted_frame = create_dot_effect(frame_rgb, dot_size, spacing, invert)
|
| 102 |
dotted_frame_bgr = cv2.cvtColor(dotted_frame, cv2.COLOR_GRAY2BGR)
|
| 103 |
out.write(dotted_frame_bgr)
|
| 104 |
+
|
| 105 |
+
frame_counter += 1
|
| 106 |
|
| 107 |
finally:
|
| 108 |
cap.release()
|
|
|
|
| 110 |
|
| 111 |
return output_path
|
| 112 |
|
| 113 |
+
def get_video_fps(video_path):
|
| 114 |
+
cap = cv2.VideoCapture(video_path)
|
| 115 |
+
if not cap.isOpened():
|
| 116 |
+
return 30 # Default FPS if video cannot be opened
|
| 117 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 118 |
+
cap.release()
|
| 119 |
+
return fps
|
| 120 |
+
|
| 121 |
with gr.Blocks(title="Dot Effect Generator") as iface:
|
| 122 |
gr.Markdown("# 🎨 Dot Effect Generator")
|
| 123 |
gr.Markdown("Transform media into stylized dot patterns")
|
|
|
|
| 143 |
with gr.Row():
|
| 144 |
vid_fps = gr.Slider(1, 60, 30, step=1, label="Output FPS")
|
| 145 |
vid_btn = gr.Button("Process Video", variant="primary")
|
| 146 |
+
|
| 147 |
+
# Update FPS slider max value based on input video
|
| 148 |
+
def update_fps_slider(video_path):
|
| 149 |
+
if video_path is None:
|
| 150 |
+
return gr.Slider.update(maximum=60, value=30) # Default if no video
|
| 151 |
+
fps = get_video_fps(video_path)
|
| 152 |
+
return gr.Slider.update(maximum=fps, value=min(fps, 30))
|
| 153 |
+
|
| 154 |
+
vid_input.change(update_fps_slider, vid_input, vid_fps)
|
| 155 |
+
|
| 156 |
vid_btn.click(process_video, [vid_input, vid_dot_size, vid_spacing, vid_invert, vid_fps], vid_output)
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|