Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.42k stars 1.55k forks source link

Issue with concat videos #1988

Open jorgesisco opened 1 year ago

jorgesisco commented 1 year ago

I have an intro and outro that I need to concatinate in several clips.

Expected Behavior

The clip should be the same as the original along with the intro and outro.

Actual Behavior

The clip that is now with an intro and outro looks all distorted.

Screenshot 2023-06-12 at 20 09 43

Steps to Reproduce the Problem

from moviepy.editor import *
import os

def get_files_in_directory(path: str) -> list:
    """
        This function retrieves all the files from a specified directory.

        Parameters:
        -----------
        path: str
            The path of the directory from which to retrieve the files.

        Returns:
        --------
        files: list
            A list of file paths in the specified directory.
    """

    # Initialize an empty list to store the files.
    files = []

    # Iterate over each file in the specified directory.
    for file in os.listdir(path):

        # Check if the current path is a file.
        if os.path.isfile(os.path.join(path, file)):
            # If it is a file, append it to the files list.
            files.append(f"{path}/{file}")

    # Return the list of files.
    return files

def add_loops(videos: list,
              intro_path: str,
              outro_path: str,
              sound_track_path: str,
              output_path: str) -> None:
    """
        This function loops each video in the input list five times, adds an intro and an outro clip,
        and overlays a soundtrack.

        Parameters:
        -----------
        videos: list
            A list of video file paths to be processed.
        intro_path: str
            The file path of the intro clip.
        outro_path: str
            The file path of the outro clip.
        sound_track_path: str
            The file path of the soundtrack.
        output_path: str
            The directory path where the processed videos will be saved.

        Returns:
        --------
        None
    """

    # Iterate over each video in the input list.
    for video in videos:
        # Load the current video file.
        clip = VideoFileClip(video)

        clip_size = clip.size
        # Repeat the current clip five times.
        clips = [clip] * 5

        # Concatenate the repeated clips into one continuous clip.
        clip = concatenate_videoclips(clips)

        # Load the intro and outro clips.
        intro_clip = VideoFileClip(intro_path)
        outro_clip = VideoFileClip(outro_path)

        # Concatenate the intro clip, the repeated video, and the outro clip.
        final_clip = concatenate_videoclips([intro_clip, clip, outro_clip])

        # Load the soundtrack and trim it to match the duration of the final clip.
        # soundtrack = AudioFileClip(sound_track_path).subclip(0, final_clip.duration)

        # Set the audio of the final clip to the soundtrack.
        # final_clip = final_clip.set_audio(soundtrack)

        # Write the final clip to a file in the specified output directory.
        final_clip.write_videofile(f"{output_path}/{video[-9:]}")
        break
    # The function does not return anything.
    return None

if __name__ == "__main__":
    # Get a list of all video files in a specified directory.
    video_list = get_files_in_directory(path="../videos/vg_vids")

    # Loop each video in the list, add an intro and an outro, and overlay a soundtrack.
    add_loops(videos=video_list,
              intro_path="../videos/others/intro.mp4",
              outro_path="../videos/others/outro.mp4",
              sound_track_path="../videos/sound_track/soundtrack.mp3",
              output_path="../videos/vg_vids_looped")

Specifications

RapahelS commented 1 year ago

I encountered a similar issue. The workaround I discovered was to ensure that every video clip has the same size.

pioneerHitesh commented 11 months ago

@keikoro @jorgesisco I would like to pick this one up , can it be assigned to me?