简体   繁体   中英

Python concatenate_videoclips, strange audio and video glitches

I concatenated video clips with:

clip1=VideoFileClip('cutclip35.mp4')
clip2=VideoFileClip('cutclip165.mp4')
clip3=VideoFileClip('cutclip24.mp4')
final_clip = concatenate_videoclips([clip1,clip2,clip3],method='compose')
final_clip.write_videofile("my_concatenation.mp4",fps=20)

and get very weird glitches and I don't know what I did wrong. Any help is appreciated!

Here are the videos: https://www.dropbox.com/sh/hbd4cwooy9xjf19/AACLcv4Rqtmj7YmGzbpmCTtsa?dl=0

using python 3.8.10

I too faced the same isue. Concatenating .mp4 files directly causes glitches due to encoding issues.

The best way to avoid gltiches while concatenating is to convert the .mp4 videos to .ts vidoes, concat the .ts videos and convert the final .ts video to .mp4

Using MoviePy to do all this process can be time consuming, hence, we can use ffmpeg directly to do all of it.

def ConcatVideos(input_video_path_list:List[str], output_video_path:str, temporary_process_folder:str):    
    concat_file_list:List[str] = []
    for idx, input_video_path in enumerate(input_video_path_list, start=1):

        # convering individual file to .ts #
        temp_file_ts = f'{temporary_process_folder}/concat_{idx}.ts'
        os.system(f'''ffmpeg -y -loglevel error -i {input_video_path} -c copy -bsf:v h264_mp4toannexb -f mpegts {temp_file_ts} ''')
        concat_file_list.append(temp_file_ts)

    # concatenating .ts files and saving as .mp4 file #
    # ffmpeg will take care of encoding .ts to .mp4 #
    concat_string = '|'.join(concat_file_list)
    os.system(f'''ffmpeg -y -loglevel error -i \
        "concat:{concat_string}" -c copy {output_video_path}''')
    
    return output_video_path

final_video_path = ConcatVideos(['input1.mp4','input2.mp4'], 'final_video.mp4', 'Temp')
# make sure '/Temp` folder exists or create it before-hand.

I have faced similar issue. what i did is concatenate the audios and videos separately and combine them later.

video_clip = concatenate_videoclips(clips,method='compose')
audioConc = concatenate_audioclips(audio_clips)
video_clip=video_clip.set_audio(audioConc)
video_clip.write_videofile("video-output.mp4",fps=24)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM