Video compositon with moviepy, 'can't sync to MPEG frame' error

17 Views Asked by At

I am working into some class that will simply get as parameter an Image of jpg format and some mp3 file that will normally contain a song, after the composition aka merge of song and background image it should return some mp4 file. The code so far:

from mutagen.mp3 import MP3
from PIL import Image
import os
from moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips
import numpy as np

class MusicAssemble:
    def __init__(self, song_mp3_path, song_background_img_path, video_id):
        self.background_img = song_background_img_path
        self.song_mp3 = song_mp3_path
        self.output_video = fr'D:\Users\User\pythonProject\YoutubeMusicAutomation\Resources\MusicVideo\ToBePosted\{video_id}.mp4'

    def video_assemble(self):
        try:
            audio = MP3(self.song_mp3)
            audio_length = audio.info.length
        except Exception as e:
            pr

int(f"There was some error reading the audio file: {e}")
            return None

        try:
            image = Image.open(self.background_img)
            image_array = np.array(image)

            # Set the duration of the video clip to match the audio length
            video_clip = ImageClip(image_array, duration=audio_length)
            audio_clip = AudioFileClip(self.song_mp3)
            final_video = video_clip.set_audio(audio_clip)

            # Write the video file
            final_video.write_videofile(self.output_video, codec='libx264', fps=24)
        except Exception as e:
            print(f"There was some error creating the video: {e}")


if __name__ == '__main__':

    video_id = "output"
    music = r'D:\Users\User\pythonProject\YoutubeMusicAutomation\Resources\Music\lBN3syUF41U.mp3'
    background = r'D:\Users\User\pythonProject\YoutubeMusicAutomation\Resources\BackgroundImg\YTtestMusicBackground\photo-1534232495813-e969fcea64e8.jpg'
    music = MusicAssemble(music, background, 'outputSong')
    music.video_assemble()

Some mp4 file that will have as background the image thruought the whole video with the mp3 file playing in the background

0

There are 0 best solutions below