Join all Mp4 with Python and Ffmpeg

With the following code you can join all the mp4 in a directory, withou caring of have to input anythong like name of the files or select or other things that takes time. No program needed, just ffmpeg (go to the site and install it, it’s free).

def concatenate():
	stringa = "ffmpeg -i \"concat:"
	elenco_video = glob.glob("*.mp4")
	elenco_file_temp = []
	for f in elenco_video:
		file = "temp" + str(elenco_video.index(f) + 1) + ".ts"
		os.system("ffmpeg -i " + f + " -c copy -bsf:v h264_mp4toannexb -f mpegts " + file)
		elenco_file_temp.append(file)
	print(elenco_file_temp)
	for f in elenco_file_temp:
		stringa += f
		if elenco_file_temp.index(f) != len(elenco_file_temp)-1:
			stringa += "|"
		else:
			stringa += "\" -c copy  -bsf:a aac_adtstoasc output.mp4"
	print(stringa)
	os.system(stringa)
 
concatenate()

 

A little script to trim the video

You can make this a little more useful (or you can just run the code in the cmd). Later I will make something a little more complex out of this ffmpeg command.

import os

os.system("ffmpeg -i joinmp4.mp4 -ss 00:00:03 -to 00:10:50 -c:v copy -c:a copy joinmp4_b.mp4")