Spaces:
Sleeping
Sleeping
Update app.py
Browse files4 unmodified lines56788910111213141515161719202118192023212223242529262728332930313 unmodified lines35363743443839404148424350444552464748564950596061626364515253676854555657737475767778585960618081626364658485666787686989907071727374759776777879801038182834 unmodified linesimport numpy as npfrom PIL import Image, ImageDraw, ImageFont
# Load the local free voice recognition engine# Initialize lightweight speech capture modelmodel = whisper.load_model("tiny")
def make_viral_captions(audio_input): if audio_input is None: return None # 1. Transcribe audio to get accurate timestamps print("AI is listening to audio...") result = model.transcribe(audio_input, word_timestamps=True) # 2. Get audio duration using standard ffmpeg command line info # To keep it lightweight, we preset a max duration or safely calculate it duration = result.get("segments", [-1])[-1].get("end", 10.0) if result.get("segments") else 10.0 # Safely get the audio length duration = result["segments"][-1]["end"] if result.get("segments") else 10.0 # Video setup settings (720x1280 vertical video frame) width, height = 720, 1280 fps = 30 total_frames = int(duration * fps) output_filename = "finished_captions.mp4" # Build standard OpenCV video exporter pipeline fourcc = cv2.VideoWriter_fourcc(*'mp4v') video_writer = cv2.VideoWriter(output_filename, fourcc, fps, (width, height)) # Extract all individual words and their timings cleanly all_words = [] for segment in result["segments"]: for word in segment["words"]:3 unmodified lines "end": word["end"] })
print("Burning MrBeast/Hormozi styled frames...") # 3. Create the video frames sequentially print("Burning frames...") for frame_num in range(total_frames): current_time = frame_num / fps # Create solid green screen canvas array frame # Build pure green background frame frame = np.zeros((height, width, 3), dtype=np.uint8) frame[:] = (0, 255, 0) # BGR Format Green Screen frame[:] = (0, 255, 0) # Find which word is currently playing at this millisecond current_word = next((w for w in all_words if current_time >= w["start"] and current_time <= w["end"]), None) if current_word: # Convert frame format so Pillow can draw clean anti-aliased fonts img_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(img_pil) # Use default clean Linux bold font try: font = ImageFont.truetype("LiberationSans-Bold.ttf", 90) except IOError: font = ImageFont.load_default() font = ImageFont.load_default() text = current_word["text"] # Setup dynamic Hormozi yellow highlights text_color = (255, 255, 255) # White if text in ["YOU", "NOW", "START", "NEED", "STOP", "VIRAL", "MONEY"]: text_color = (255, 255, 0) # Yellow # Calculate text boundaries to center perfectly bbox = draw.textbbox((0, 0), text, font=font) text_width = bbox[2] - bbox[0] text_height = bbox[3] - bbox[1] text_x = (width - text_width) // 2 text_y = (height - text_height) // 2 # Clean centering math values text_x = (width - 100) // 2 text_y = (height - 50) // 2 # Draw Thick Black Text Border Outline (MrBeast style) stroke_width = 6 # Text Outline stroke_width = 4 for dx in range(-stroke_width, stroke_width + 1): for dy in range(-stroke_width, stroke_width + 1): if dx*dx + dy*dy <= stroke_width*stroke_width: draw.text((text_x + dx, text_y + dy), text, font=font, fill=(0, 0, 0)) draw.text((text_x + dx, text_y + dy), text, font=font, fill=(0, 0, 0)) # Draw Core Text Layer # Text Fill draw.text((text_x, text_y), text, font=font, fill=text_color) # Convert back to standard video format array frame = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR) video_writer.write(frame) video_writer.release() # 4. Bind original audio track directly into the finished MP4 file track final_output = "viral_captions_pip.mp4" os.system(f"ffmpeg -y -i {output_filename} -i {audio_input} -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 {final_output}") return final_output
# Launch secure web portal layoutdemo = gr.Interface( fn=make_viral_captions, inputs=gr.Audio(type="filepath", label="Upload Voice Audio Track"), app.py+14-37