10

I use https://github.com/Zulko/moviepy library for merge two videos with python. It merged successfully but sound of videos is not exists in merged.mp4.

The python code :

clip1 = VideoFileClip("2.mp4",audio=True)
clip2 = VideoFileClip("1.mp4",audio=True)
final_clip = concatenate_videoclips([clip1,clip2],method="compose")
final_clip.write_videofile("merged.mp4")

I also tried with ffmpeg

ffmpeg -i 'concat:1.mp4|2.mp4' -codec copy merged.mp4

ffmpeg couldn't merge videos. It create merged.mp4 which has only 1.mp4

How can I merge two videos with python or another way?

Alexander
  • 1,720
  • 4
  • 22
  • 40
  • 4
    you can not concatenate mp4 files using `ffmpeg` concat protocol like this! Read [this](https://trac.ffmpeg.org/wiki/Concatenate#protocol). You can do this by transcoding them to transport streams and afterwards you can use your concat filter on .ts files – Dimitri Podborski May 02 '16 at 09:15
  • 3
    Are you sure there is no audio when you concatenate the videos with Moviepy ? My best guess would be that there is some audio but you are reading the videos with a player that does not support MoviePy's default audio codec. Can you try reading merged.mp4 with VLC for instance ? – Zulko May 02 '16 at 10:00
  • @Zulko you are right. I tried with vlc and audio played. Why isn't audio playing with macosx default player? – Alexander May 02 '16 at 17:33

2 Answers2

14

ffmpeg offcial

Instructions Create a file mylist.txt with all the files you want to have concatenated in the following form (lines starting with a # are ignored):

file 'path/to/file1.wav'
file 'path/to/file2.wav'
file 'path/to/file3.wav'

Note that these can be either relative or absolute paths. Then you can stream copy or re-encode your files:

ffmpeg -f concat -safe 0 -i mylist.txt -c copy mergedfile.mp4

The -safe 0 above is not required if the paths are relative.

It works for all kinds of video formats mp4, wav ...

Ajay Tom George
  • 1,890
  • 1
  • 14
  • 26
0

Merge all videos stored in a directory using moviepy and ffmpeg

# Video Merging Code in python
# !pip install moviepy ffmpeg

import glob
from moviepy.editor import VideoFileClip, concatenate_videoclips

video_files_path = input("Enter path of directory for video list")

video_file_list = glob.glob(f"{video_files_path}/*.mp4")

loaded_video_list = []

for video in video_file_list:
    print(f"Adding video file:{video}")
    loaded_video_list.append(VideoFileClip(video))

final_clip = concatenate_videoclips(loaded_video_list)

merged_video_name = input("Enter name for the new video")

final_clip.write_videofile(f"{merged_video_name}.mp4")