Created
June 5, 2025 16:18
-
-
Save A-L-V/630e37271bdbd630d8f29768d448b375 to your computer and use it in GitHub Desktop.
Mini video music python y yt_Dlp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yt_dlp | |
import imageio_ffmpeg | |
from moviepy import ImageClip, AudioFileClip | |
import ffmpeg | |
url = "https://www.youtube.com/watch?v=j-RpvIuazmc" | |
start_time = 30 # start at 30 seconds | |
duration = 10 # duration of 60 seconds | |
options = {} | |
dataMusic = "" | |
image_path = './ShareVideoMusic/' # Updated image path | |
output_path = 'output_video.mp4' | |
audio_path = "./ShareVideoMusic/audio.mp3" | |
ffmpeg_path = "C:\\Users\\USER\\StudioProjects\\ShareVideoMusic\\ffmpeg.exe" | |
download_path = './ShareVideoMusic/' # Path for downloaded files | |
def donwload_audio(url, start_time, duration): | |
options = { | |
'format': 'bestaudio/best', | |
'extract-audio': True, # only keep the audio | |
'audio-quality': 1, # the best audio quality | |
'ffmpeg_location': ffmpeg_path, # Set the path to ffmpeg here | |
'postprocessors': [{ | |
'key': 'FFmpegExtractAudio', #ExtractAudio | |
'preferredcodec': 'mp3', | |
'preferredquality': '192', | |
}], | |
'postprocessor_args': [ | |
'-ss', str(start_time), | |
'-t', str(duration), | |
], | |
'quite': True, | |
'outtmpl': download_path + 'audio.%(ext)s', | |
'noplaylist': True, # Evita procesar listas de reproducción | |
'logger': None, # Desactiva logs | |
} | |
try: | |
with yt_dlp.YoutubeDL(options) as ydl: | |
ydl.download([url]) # Download the audio | |
return ydl.prepare_filename({'ext': 'mp3'}) | |
except Exception as e: | |
print(f"Error: {e}") | |
return None | |
def crear_video(imagen_path, audio_path, video_output_path, duracion_video=10): | |
# Cargar la imagen | |
imagen = ImageClip(imagen_path) | |
# Cargar el audio | |
print(audio_path) | |
audio = AudioFileClip(audio_path) | |
# Si no se especifica la duración del video, usamos la duración del audio | |
if duracion_video is None: | |
duracion_video = audio.duration | |
# Ajustar la duración de la imagen al audio | |
imagen = imagen.with_duration(duracion_video) | |
# Establecer la resolución del video (opcional) | |
imagen = imagen.resized(height=720) # Ajusta el tamaño si es necesario | |
# Asignar el audio al video | |
video = imagen.with_audio(audio) | |
# Escribir el archivo de salida | |
video.write_videofile(video_output_path, fps=24) | |
def download_thumbnail(url): | |
ydl_opts = { | |
'skip_download': True, # We only want to download the thumbnail | |
'writethumbnail': True, # Write the thumbnail file | |
'outtmpl': download_path + 'thumbnail.%(ext)s', # Output template for the thumbnail | |
} | |
try: | |
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
ydl.download([url]) | |
print("Thumbnail downloaded successfully.") | |
return ydl.prepare_filename({'ext': 'webp'}) # Assuming the thumbnail is in jpg format | |
except Exception as e: | |
print(f"Error downloading thumbnail: {e}") | |
return None | |
image_path = download_thumbnail(url) | |
audio_path = donwload_audio(url, start_time, duration) | |
crear_video(image_path, audio_path, output_path, duration) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment