Spaces:
Running
Running
File size: 6,060 Bytes
83aa67f 3c4bc36 83aa67f d3f6dad 83aa67f 3c4bc36 83aa67f 3c4bc36 83aa67f 3c4bc36 83aa67f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import os
import gradio as gr
from cerebras.cloud.sdk import Cerebras
from gtts import gTTS
from moviepy import VideoFileClip, concatenate_videoclips, AudioFileClip
import requests
# Initialize Cerebras client
Cerekey = os.getenv("CKey")
client = Cerebras(api_key= Cerekey)
# Pexels API key
pexkey = os.getenv("Pkey")
PEXELS_API_KEY = pexkey
# Modify the system prompt to include the estimated word count based on video duration
def generate_script(prompt, max_duration):
system_message = f"You are an expert video content creator and narration writer who is proficient in generating narration from user prompts and crafting a concise and poetic narration that aligns with the prompt. Craft a concise, poetic narration for the prompt. Go straight to the narration, don't write a foreward or a description of your action. The narration should be suitable for a video that can be read in less than {max_duration} seconds."
stream = client.chat.completions.create(
messages=[{"role": "system", "content": system_message}, {"role": "user", "content": prompt}],
model="llama-3.3-70b",
stream=False,
max_completion_tokens=1024,
temperature=0.7,
top_p=1
)
return stream.choices[0].message.content
def search_and_download_videos(query, max_duration, aspect_ratio, download_folder, max_results=6):
url = "https://api.pexels.com/videos/search"
headers = {"Authorization": PEXELS_API_KEY}
params = {"query": query, "per_page": max_results}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
videos = response.json().get("videos", [])
if not os.path.exists(download_folder):
os.makedirs(download_folder)
downloaded_files = []
for video in videos:
duration = video.get("duration")
width = video.get("width")
height = video.get("height")
if width and height:
video_aspect_ratio = "landscape" if width > height else "portrait" if height > width else "square"
if duration <= max_duration and video_aspect_ratio == aspect_ratio:
video_url = video["video_files"][0]["link"]
video_id = video["id"]
video_filename = os.path.join(download_folder, f"{video_id}.mp4")
video_response = requests.get(video_url, stream=True)
with open(video_filename, "wb") as file:
for chunk in video_response.iter_content(chunk_size=1024):
file.write(chunk)
downloaded_files.append(video_filename)
return downloaded_files
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return []
def generate_narration(script, output_file="narration.mp3"):
tts = gTTS(script, lang="en")
tts.save(output_file)
return output_file
def load_videos_from_folder(folder_path):
if not os.path.exists(folder_path):
print(f"Error: The folder '{folder_path}' does not exist.")
return []
video_files = [
os.path.join(folder_path, file)
for file in os.listdir(folder_path)
if file.endswith(('.mp4', '.mov', '.avi', '.mkv'))
]
return video_files
def aggregate_videos(clips):
if not clips:
return None
return concatenate_videoclips(clips, method="compose")
def trim_video_to_audio_length(final_video, audio_length):
if final_video.duration > audio_length:
# Use subclipped method for CompositeVideoClip
final_video = final_video.subclipped(0, audio_length)
return final_video
# Function to add narration to the final video
def add_narration_to_video(final_video, narration_path):
if os.path.exists(narration_path):
narration_audio = AudioFileClip(narration_path)
narration_audio = narration_audio.with_duration(final_video.duration) # Adjust duration to match video
final_video = final_video.with_audio(narration_audio) # Use with_audio instead of set_audio
return final_video
def save_final_video(final_video, output_path):
final_video.write_videofile(output_path, codec="libx264", audio_codec="aac", preset="ultrafast")
def generate_video(prompt, max_duration, aspect_ratio, download_folder="downloaded_videos", max_results=6):
script = generate_script(prompt, max_duration)
videos = search_and_download_videos(prompt, max_duration, aspect_ratio, download_folder, max_results)
if not videos:
return "No videos were downloaded.", None, script
video_clips = [VideoFileClip(video) for video in videos]
final_video = aggregate_videos(video_clips)
if final_video:
narration_file = generate_narration(script)
final_video = trim_video_to_audio_length(final_video, AudioFileClip(narration_file).duration)
final_video = add_narration_to_video(final_video, narration_file)
output_video_path = "final_video_with_narration.mp4"
save_final_video(final_video, output_video_path)
return narration_file, output_video_path, script
return "Error generating video.", None, script
iface = gr.Interface(
fn=generate_video,
inputs=[
gr.Textbox(label="Enter Text Prompt", placeholder="Enter the text to generate the video script."),
gr.Slider(minimum=1, maximum=30, step=1, label="Video Length (seconds)", value=10),
gr.Radio(choices=["portrait", "landscape", "square"], label="Select Aspect Ratio", value="landscape"),
],
outputs=[
gr.Audio(label="Narration Audio"),
gr.Video(label="Generated Video"),
gr.Textbox(label="Generated Script", interactive=False)
],
title="Sepia Text-to-Video Generator",
description="Enter a text prompt, specify the length of the video (maximum 30 seconds), select the aspect ratio, and click 'Submit' to get the narrated audio, the video and the script.",
live=False
)
iface.launch(debug=True) |