[PYTHON] Connect a large number of videos together!

Thing you want to do

Concatenate all videos in the directory into one video

Execution method

$ python3 combine_videos.py -i sample_videos/ -o output.mp4 
29.0 1280 720
Filename:  video01.mp4
Filename:  video02.mp4
Filename:  video03.mp4
Done.

combine_videos.py


#coding=utf-8
import os
import cv2
import glob
import argparse    # 1.Import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_dir', help='input video directory')
parser.add_argument('-o', '--output_video', help='output video filename')

args = parser.parse_args()

def combine_videos(directory, result):
    #Specify the video to input(Sort by string)
    videos_list = glob.glob('{}/*'.format(directory))
    videos_list.sort()

    #Specify MP4V as the format
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')

    #Acquisition of video information
    movie = cv2.VideoCapture(videos_list[0])
    fps    = movie.get(cv2.CAP_PROP_FPS)
    height = movie.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width  = movie.get(cv2.CAP_PROP_FRAME_WIDTH)
    print(fps, int(width), int(height))

    #Open the output file
    out = cv2.VideoWriter(result, int(fourcc), fps, (int(width), int(height)))

    #Loading video
    for video in videos_list:
        movie = cv2.VideoCapture(video)
        print('Filename: ', video.split('/')[-1])
        while True:
            ret, frame = movie.read()
            out.write(frame)
            if not ret:
                break
    print('Done.')

if __name__ == '__main__':
    combine_videos(args.input_dir, args.output_video)


Recommended Posts

Connect a large number of videos together!
Upload a large number of images to Wordpress
Organize a large number of files into folders
Accelerate a large number of simple queries with MySQL
[Python] Randomly generate a large number of English names
Paste a large number of image files into PowerPoint [python-pptx]
Scrapy-Redis is recommended for crawling a large number of domains
Executing a large number of Python3 Executor.submit may consume a lot of memory
TensorFlow To learn from a large number of images ... ~ (almost) solution ~
Convert a large number of PDF files to text files using pdfminer
Maximum average number of daily visitors (large)
Connect a lot of Python or and and
Import serial number videos together with Aviutl
TensorFlow To learn from a large number of images ... (Unsolved problem) → 12/18 Solved
One-liner to create a large number of test files at once on Linux
Find the number of days in a month
Consolidate a large number of CSV files in folders with python (data without header)
A tool to follow posters with a large number of likes on instagram [25 minutes to 1 second]
Use API to mark a large number of unread emails in Gmail as read
[Python] A program that counts the number of valleys
Read a large amount of securities reports using COTOHA
Lambda + Python is good at restricting access with a large number of IP address lists
Run a limited number of image presentation programs on PsychoPy
Make a given number of seconds into hours, minutes and seconds
Get the number of specific elements in a python list
How to connect the contents of a list into a string
[Django] What to do if the model you want to create has a large number of fields