Hello I have written a program to create video from audio and two images in the moment. The video with audio can be created, but I don't manage to display the next image as specified in array
photos_start_time = ["00:00", "3:00"] # Times of the image projection start
ramdisk_path set to your local drive in ~/Videos where you have your videos
To test
Specify audio file:
By default the dirs are set to:
Specify fps for your videos here three videos will be generated:
The code:
photos_start_time = ["00:00", "3:00"] # Times of the image projection start
MM:SS
.ramdisk_path set to your local drive in ~/Videos where you have your videos
To test
Specify audio file:
`audio_file = "Groove.mp3"`Answer if you want to start in current directory (.)
`user_choice = input("...")`Set the images you want to have in a video.
By default the dirs are set to:
`photos_dir = os.path.join(home_dir, "Stažené")` `audio_dir = os.path.join(home_dir, "Hudba")`output_dir = os.path.join(home_dir, "Videa")
Specify fps for your videos here three videos will be generated:
fps_values = [3, 5, 6]So my question is how to fix it to see every image, right now I see only one image the first one, for all the video duration.
The code:
``` import os from PIL import Image # Required for checking image dimensions from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclips, ImageClip from datetime import datetime audio_file = "Finance Groove.mp3" # Getting the user's choice user_choice = input("Do you want to use the current folder (.) or the default folder? Enter '.' for the current folder or 'default' for the default folder: ") # Setting the folder path based on the user's choice if user_choice == '.': input_dir = os.path.abspath('.') audio_dir = input_dir else: home_dir = os.path.expanduser("~") photos_dir = os.path.join(home_dir, "Downloads") audio_dir = os.path.join(home_dir, "Music") output_dir = os.path.join(home_dir, "Videos") # Convert times from "MM:SS" format to seconds def time_to_seconds(time_str): m, s = map(int, time_str.split(':')) return m * 60 + s def find_duration(photo_times, duration, i): if i + 1 < len(photo_times): end_time = photo_times[i + 1] else: end_time = duration img_duration = end_time - photo_times[i] return img_duration # File paths audio_path = os.path.join(audio_dir, audio_file) # FPS for different versions of the video fps_values = [3, 5, 6] # Generate three different videos based on the number of frames photos = [ os.path.join(photos_dir, "_527b2337-6f60-4b3e-ae72-a4449dea17e3.jpeg"), os.path.join(photos_dir, "dandelions.jpeg") ] audio_clip = AudioFileClip(audio_path) duration = audio_clip.duration ramdisk_size = int(estimate_ramdisk_size(duration, photos, fps_values) / (1024 * 1024)) # MB audio_clip.close() create_ramdisk(ramdisk_path, ramdisk_size) # RAM disk size in MB photos_start_time = ["00:00", "3:00"] # Start times in `MM:SS` # Check if audio file exists if not os.path.isfile(audio_path): print(f"Error: Audio file '{audio_path}' does not exist.") os.sys.exit(1) # Check if images exist for photo_path in photos: if not os.path.isfile(photo_path): print(f"Error: Image '{photo_path}' does not exist.") os.sys.exit(1) # Get times in seconds photo_times = [time_to_seconds(t) for t in photos_start_time] # Load audio file audio_clip = AudioFileClip(audio_path) # Create video clips from photos and different fps for fps in fps_values: print(f"Creating video with FPS = {fps}") # Create a list of video clips from photos video_clips = [] for i, photo_path in enumerate(photos): dur = find_duration(photo_times, duration, i) img_clip = ImageClip(photo_path).set_duration(dur) # Photo for the entire length of audio img_clip = img_clip.set_start(photo_times[i]) # Set photo start video_clips.append(img_clip) # Concatenate all clips into one video final_clip = concatenate_videoclips(video_clips, method="compose") final_clip = final_clip.set_audio(audio_clip) # Set FPS final_clip = final_clip.set_duration(duration).fx(lambda clip: clip.set_fps(fps)) # Temporarily save the final video to RAM disk temp_video_path = os.path.join(ramdisk_path, f"temp_video_{fps}.mp4") # final_clip.write_videofile(temp_video_path, codec="libx264", audio_codec="aac") final_clip.write_videofile(temp_video_path, codec="libx264", audio_codec="aac", threads=2, preset='ultrafast') # Close all clips audio_clip.close() final_clip.close() print(f"Video was successfully created and saved to RAM disk at: {ramdisk_path}") # Concatenate all clips into one video final_clip = concatenate_videoclips(video_clips, method="compose") final_clip = final_clip.set_audio(audio_clip) # Temporarily save the final video to RAM disk temp_video_path = os.path.join(ramdisk_path, "temp_video.mp4") final_clip.write_videofile(temp_video_path, codec="libx264", crf="28", audio_codec="aac") # Close all clips audio_clip.close() final_clip.close() print(f"Video was successfully created and saved to RAM disk at: {ramdisk_path}") ```