swami audio to video , youtube private mode.

 !pip install yt-dlp moviepy ffmpeg-python


import yt_dlp

from moviepy.editor import ImageClip, AudioFileClip

import glob, os


# -------- USER INPUTS --------

image_file = "/content/ima1.png"     # your image in /content

use_uploaded_audio = True            # True = use input.mp3, False = download from YouTube


# If using YouTube audio

audio_url = "https://www.youtube.com/watch?v=me8uai5nV9c"  


# If using uploaded audio

uploaded_audio_file = "/content/input.mp3"


# Trim times (now in hh:mm:ss format)

audio_start = "00:00:05"   # hh:mm:ss

audio_end   = "00:00:25"   # hh:mm:ss


output_path = "/content/output.mp4"

# -----------------------------


def download_from_youtube(url, filename):

    ydl_opts = {

        'format': 'bestaudio/best',

        'outtmpl': filename + ".%(ext)s",

        'quiet': False,

    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:

        ydl.download([url])


def time_to_seconds(t):

    parts = t.split(":")

    parts = [int(p) for p in parts]

    if len(parts) == 2:  # mm:ss

        mm, ss = parts

        return mm * 60 + ss

    elif len(parts) == 3:  # hh:mm:ss

        hh, mm, ss = parts

        return hh * 3600 + mm * 60 + ss

    else:

        raise ValueError("Invalid time format! Use mm:ss or hh:mm:ss")


# Pick audio source

if use_uploaded_audio and os.path.exists(uploaded_audio_file):

    audio_file = uploaded_audio_file

    print(f"🎵 Using uploaded audio: {audio_file}")

else:

    print("📥 Downloading audio from YouTube...")

    download_from_youtube(audio_url, "/content/audio")

    audio_files = glob.glob("/content/audio.*")

    audio_file = audio_files[0]


# Load audio

full_audio = AudioFileClip(audio_file)

print(f"🎵 Audio duration: {full_audio.duration:.2f} sec ({full_audio.duration/60:.1f} min)")


# Convert input times

a_start = time_to_seconds(audio_start)

a_end   = time_to_seconds(audio_end)


# Safety check

if a_start >= full_audio.duration:

    print("⚠️ Start time is beyond audio length. Resetting to 0.")

    a_start = 0

if a_end > full_audio.duration:

    print("⚠️ End time is beyond audio length. Resetting to end of audio.")

    a_end = full_audio.duration


# Trim audio

audio_clip = full_audio.subclip(a_start, a_end)


# Make static image into a video

image_clip = ImageClip(image_file, duration=audio_clip.duration)


# Attach audio

final_clip = image_clip.set_audio(audio_clip)


# Export

final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=24)


print(f"✅ Final video saved as {output_path}")


Popular posts from this blog

SAP CPI : camle expression in sap cpi , cm, router, filter and groovy script. format

SAP CPI camel conditions and xpath conditions

oauth call to cpi integraiton suite from sap apim