YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
import os import gradio as gr from yt_dlp import YoutubeDL import whisper from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
model = whisper.load_model("base")
def download_video(url): ydl_opts = {'format': 'bestvideo+bestaudio/best', 'outtmpl': 'temp_input.%(ext)s', 'quiet': True} with YoutubeDL(ydl_opts) as ydl: ydl.download([url]) return "temp_input.mp4"
def generate_shorts(video_url, clip_duration): try: video_path = download_video(video_url) result = model.transcribe(video_path) segments = result["segments"]
best_start = 0
max_words = 0
for seg in segments:
start_time = seg["start"]
word_count = sum(len(s["text"].split()) for s in segments if start_time <= s["start"] <= start_time + float(clip_duration))
if word_count > max_words:
max_words = word_count
best_start = start_time
video = VideoFileClip(video_path)
clip = video.subclip(best_start, best_start + float(clip_duration))
w, h = clip.size
target_w = int(h * 9 / 16)
crop_clip = clip.crop(x1=(w - target_w) // 2, y1=0, width=target_w, height=h)
annotated_clips = []
for seg in segments:
if best_start <= seg["start"] <= best_start + float(clip_duration):
txt = TextClip(seg["text"].strip(), fontsize=24, color='yellow', font='Arial-Bold', size=(target_w - 40, None), method='caption')
txt = txt.set_start(seg["start"] - best_start).set_end(min(seg["end"] - best_start, float(clip_duration))).set_position(('center', 'bottom'))
annotated_clips.append(txt)
final_clip = CompositeVideoClip([crop_clip] + annotated_clips)
output_path = "generated_short.mp4"
final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=24)
return output_path, f"Short successfully isolated starting at timestamp: {int(best_start)}s"
except Exception as e:
return None, str(e)
interface = gr.Interface( fn=generate_shorts, inputs=[gr.Textbox(label="Video URL Link"), gr.Slider(15, 60, step=5, value=30, label="Clip Length (Seconds)")], outputs=[gr.Video(label="Generated AI Short"), gr.Textbox(label="System Logs")], title="AI Auto-Shorts & Caption Generator" )
if name == "main": interface.launch()