#!/usr/bin/env python import os import os.path import time import tempfile from glob import glob ONE_HOUR = 60 * 60 files = [f for f in glob('*.MP4') if os.path.isfile(f)] files.sort(key=os.path.getmtime) current_file_index = 0 file = None def next_file(): global file if file is not None: file.flush() join_videos() file.close() file = tempfile.NamedTemporaryFile() def join_videos(): global current_file_index, file if file.tell() > 0: current_file_index += 1 os.system("ffmpeg -f concat -safe 0 -i '%s' -c copy %i.mp4" % (file.name, current_file_index)) last = 0 for filepath in files: mtime = os.path.getmtime(filepath) if (mtime - last) > ONE_HOUR/2: next_file() last = mtime file.write("file '%s'\n" % os.path.join(os.getcwd(), filepath)) next_file()