21
14

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.

OpenCVを使って複数動画を一発で連結する!

Last updated at Posted at 2020-02-05

強化学習結果などの沢山の動画を一発で連結したかったので作ってみた.

シチュエーション

複数の動画が入ったディレクトリあり,その中の動画を全て連結した一つの動画を作成するプログラム.

ソース

import cv2
import glob

def comb_movie(movie_files,out_path):
    
    # 形式はmp4
    fourcc = cv2.VideoWriter_fourcc('m','p','4','v')

    # 動画情報の取得
    movie = cv2.VideoCapture(movie_files[0])
    fps = movie.get(cv2.CAP_PROP_FPS)
    height = movie.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width = movie.get(cv2.CAP_PROP_FRAME_WIDTH)


    # 出力先のファイルを開く
    out = cv2.VideoWriter(out_path, int(fourcc), fps, (int(width), int(height)))
        
        
    for movies in (movie_files):
        print(movies)
        # 動画ファイルの読み込み,引数はビデオファイルのパス
        movie = cv2.VideoCapture(movies)

        # 正常に動画ファイルを読み込めたか確認
        if movie.isOpened() == True: 
            # read():1コマ分のキャプチャ画像データを読み込む
            ret, frame = movie.read() 
        else:
            ret = False
        
        while ret:
            # 読み込んだフレームを書き込み
            out.write(frame)
            # 次のフレーム読み込み
            ret, frame = movie.read()

# ディレクトリ内の動画をリストで取り出す
files = sorted(glob.glob("./movie_dir/*.mp4"))

# 出力ファイル名
out_path = "movie_out1.mp4"

comb_movie(files,out_path)


21
14
2

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
21
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?