0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

大量の動画をまとめて連結させる!

Posted at

やりたいこと

ディレクトリの中の動画を全て連結して1つの動画にする

実行方法

$ 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. 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):
    # 入力する動画を指定(文字列でソート)
    videos_list = glob.glob('{}/*'.format(directory))
    videos_list.sort()

    # 形式はMP4Vを指定
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')

    # 動画情報の取得
    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))

    # 出力先のファイルを開く
    out = cv2.VideoWriter(result, int(fourcc), fps, (int(width), int(height)))

    # 動画の読み込み
    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)


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?