| import gradio as gr |
| from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip |
| from PIL import Image, ImageDraw, ImageFont |
| import whisper |
| from keybert import KeyBERT |
| import numpy as np |
| import os |
|
|
| |
| whisper_model = whisper.load_model("base") |
| kw_model = KeyBERT() |
|
|
| def process_video(video_path, caption="Your Caption"): |
| |
| clip = VideoFileClip(video_path) |
|
|
| |
| frame = clip.get_frame(5) |
| image = Image.fromarray(np.uint8(frame)) |
| draw = ImageDraw.Draw(image) |
| font = ImageFont.load_default() |
| text_position = (50, image.height - 100) |
| draw.text(text_position, caption, fill="white", font=font) |
| thumbnail_path = "thumbnail.jpg" |
| image.save(thumbnail_path) |
|
|
| |
| result = whisper_model.transcribe(video_path, language="hi") |
| text = result["text"] |
|
|
| |
| keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), stop_words='english') |
| keywords_list = [kw[0] for kw in keywords] |
|
|
| |
| txt_clip = TextClip(caption, fontsize=50, color='white', font="Arial", bg_color='black').set_position(("center", "bottom")).set_duration(clip.duration) |
| final = CompositeVideoClip([clip, txt_clip]) |
|
|
| |
| output_path = "output_with_caption.mp4" |
| final.write_videofile(output_path, codec='libx264', audio_codec='aac') |
|
|
| return thumbnail_path, ", ".join(keywords_list), output_path |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# Video Thumbnail Generator with SEO Keywords") |
| video_input = gr.File(label="Upload Video", type="filepath") |
| caption_input = gr.Textbox(label="Enter Caption for Thumbnail", value="Awesome Video!") |
| generate_button = gr.Button("Generate Thumbnail & Keywords") |
| thumbnail_output = gr.Image(label="Generated Thumbnail") |
| keywords_output = gr.Textbox(label="SEO Keywords") |
| video_output = gr.File(label="Download Processed Video") |
|
|
| generate_button.click( |
| fn=process_video, |
| inputs=[video_input, caption_input], |
| outputs=[thumbnail_output, keywords_output, video_output] |
| ) |
|
|
| demo.launch() |
|
|