conver audio to video , so we can upload in youtube
# === STEP 1: Mount Google Drive (optional but recommended) ===
from google.colab import drive
drive.mount('/content/drive')
# If your files are in Drive, set this path:
# BASE_PATH = "/content/drive/MyDrive/your_folder"
# If you upload files directly to Colab, use this:
BASE_PATH = "/content"
# === STEP 2: Install required package ===
!pip install moviepy --quiet
# === STEP 3: MAIN CODE - Create video from audio + title image ===
from moviepy.editor import AudioFileClip, ImageClip, concatenate_videoclips
from PIL import Image, ImageDraw, ImageFont
import os
# ────── CONFIGURATION ──────
audio_file = f"{BASE_PATH}/aud.mp3" # Input audio
output_video = f"{BASE_PATH}/vid1.mp4" # Output video
title_text = "త్రైత సిద్ధాంత భగవద్గీత, దైవాసురసంపద్విభాగ యోగము" # ← CHANGE THIS
# Video settings (small & YouTube-friendly)
width, height = 640, 360 # Minimum safe resolution
background_color = (0, 0, 0) # Black background
text_color = (255, 255, 255) # White text
font_size = 48
duration = None # Will be set automatically from audio length
# ────── Create image with title ──────
def create_title_image(text, width, height):
img = Image.new('RGB', (width, height), color=background_color)
draw = ImageDraw.Draw(img)
# Try to use a nice font, fallback to default
try:
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", font_size)
except:
font = ImageFont.load_default()
# Auto-wrap long text
max_width = width - 80
lines = []
words = text.split()
line = ""
for word in words:
test_line = line + word + " "
if draw.textbbox((0,0), test_line, font=font)[2] <= max_width:
line = test_line
else:
lines.append(line)
line = word + " "
lines.append(line)
# Calculate total height and center vertically
line_height = font_size + 10
total_text_height = len(lines) * line_height
y = (height - total_text_height) // 2
for line in lines:
bbox = draw.textbbox((0, 0), line, font=font)
text_width = bbox[2] - bbox[0]
x = (width - text_width) // 2
draw.text((x, y), line.strip(), fill=text_color, font=font)
y += line_height
img.save("/tmp/title_image.jpg")
return "/tmp/title_image.jpg"
# ────── Generate video ──────
print("Creating title image...")
img_path = create_title_image(title_text, width, height)
print("Loading audio and creating video...")
audio = AudioFileClip(audio_file)
duration = audio.duration
# Create video clip from static image
video = ImageClip(img_path, duration=duration).set_audio(audio)
# Write the final video (small size, YouTube compatible)
print("Exporting vid1.mp4 (this may take 10-60 seconds)...")
video.write_videofile(
output_video,
fps=24,
codec="libx264",
audio_codec="aac",
bitrate="500k", # Keeps file very small
preset="ultrafast" # Fast encoding
)
print(f"Done! Your video is ready: {output_video}")
# Optional: Download directly from Colab
from google.colab import files
files.download(output_video)