Created
January 21, 2026 18:39
-
-
Save me-suzy/20c2c9d80d8b1805fbd93d70fb43ae49 to your computer and use it in GitHub Desktop.
FINAL - Convert poze si video cu melodie.py
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 os | |
| from moviepy.editor import ImageClip, VideoFileClip, concatenate_videoclips, AudioFileClip, ColorClip, CompositeVideoClip | |
| # Configurații | |
| media_directory = r"g:\De pus pe FTP 2\masina de spalat" | |
| audio_path = r"g:\Muzica\Hamidshax - Yesterday (Original Mix).mp3" | |
| output_path = r"g:\De pus pe FTP 2\masina de spalat\Video_Masina_Spalat.mp4" | |
| TARGET_RES = (1920, 1080) | |
| SORT_METHOD = 'DATE' | |
| def fix_clip_size(clip, target_res): | |
| """ | |
| Redimensionează clipul astfel încât să încapă în target_res fără deformare. | |
| Adaugă margini negre (Letterboxing). | |
| """ | |
| # Calculăm scara necesară pentru a nu depăși nicio dimensiune | |
| w, h = clip.size | |
| scale = min(target_res[0]/w, target_res[1]/h) | |
| # Redimensionăm păstrând raportul de aspect | |
| new_w, new_h = int(w * scale), int(h * scale) | |
| clip_resized = clip.resize(newsize=(new_w, new_h)) | |
| # Creăm fundalul negru și suprapunem clipul pe centru | |
| bg = ColorClip(size=target_res, color=(0, 0, 0)).set_duration(clip.duration) | |
| return CompositeVideoClip([bg, clip_resized.set_position("center")]) | |
| def create_video(): | |
| print("[*] Încep procesarea cu corecție de rotație și aspect ratio...") | |
| valid_extensions = ('.png', '.jpg', '.jpeg', '.mp4', '.mov', '.avi') | |
| files = [f for f in os.listdir(media_directory) if f.lower().endswith(valid_extensions)] | |
| if SORT_METHOD == 'NAME': | |
| files.sort() | |
| else: | |
| files.sort(key=lambda x: os.path.getmtime(os.path.join(media_directory, x))) | |
| clips = [] | |
| for index, file in enumerate(files): | |
| path = os.path.join(media_directory, file) | |
| print(f" [{index + 1}/{len(files)}] Procesez: {file}") | |
| try: | |
| if file.lower().endswith(('.mp4', '.mov', '.avi')): | |
| # FOARTE IMPORTANT: rotating_log=True sau manual rotation check | |
| clip = VideoFileClip(path) | |
| # Unele versiuni de MoviePy necesită corecție manuală dacă video-ul e vertical | |
| if clip.rotation == 90: | |
| clip = clip.resize(clip.size[::-1]).rotate(-90) | |
| elif clip.rotation == 270: | |
| clip = clip.resize(clip.size[::-1]).rotate(-270) | |
| elif clip.rotation == 180: | |
| clip = clip.rotate(180) | |
| else: | |
| clip = ImageClip(path).set_duration(3).set_fps(24) | |
| # Aplicăm fixarea dimensiunii și efectele | |
| processed = fix_clip_size(clip, TARGET_RES).fadein(0.5).fadeout(0.5) | |
| clips.append(processed) | |
| except Exception as e: | |
| print(f" [!] Eroare la {file}: {e}") | |
| print("[*] Combinare clipuri...") | |
| final_video = concatenate_videoclips(clips, method="compose") | |
| print("[*] Adăugare audio...") | |
| audio = AudioFileClip(audio_path) | |
| if audio.duration > final_video.duration: | |
| audio = audio.subclip(0, final_video.duration) | |
| final_video = final_video.set_audio(audio) | |
| print("[*] Randare finală...") | |
| final_video.write_videofile( | |
| output_path, | |
| codec="libx264", | |
| audio_codec="aac", | |
| fps=24, | |
| threads=2, | |
| ffmpeg_params=["-crf", "24"] | |
| ) | |
| print(f"\n[+] Finalizat: {output_path}") | |
| if __name__ == "__main__": | |
| create_video() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment