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.