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 5 years have passed since last update.

OpenCVの動画保存ライブラリについて

Posted at

目的

画像を集めて動画を作成する機会があったので、備忘録がてらに記載

参考

OpenCV Documentation

ソースコード

メイン処理

fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
video = cv2.VideoWriter('out.mp4', fourcc, 29.97, (width, height))

fourccとは、動画のコーデックを指定するための4バイトのコード。
videoは動画保存のためのVideoWriter型のオブジェクト。

video.write(img)
video.release()

write()は動画に画像を追加する処理、imgは画像ファイル
最後にrelease()でout.mp4を出力。

一連処理

import cv2

def main():
    dir_path = "./movie"
    fourcc = cv2.VideoWriter_fourcc('m','p','4','v') 
    i = 1
    for _file in os.listdir(dir_path):
        frame_path = os.path.join(dir_path, _file)
        print(frame_path)
        
        img = cv2.imread(frame_path)
        height, width, _ = img.shape
        print(height, width)
        img = cv2.resize(img, (height,width))
        if i == 1:
            video = cv2.VideoWriter('out.mp4', fourcc, 29.97, (width, height))

        video.write(img)
        sys.stdout.write('\rNow frame {} / {}'. format(i, len(os.listdir(dir_path))))
        i = i+1

    video.release()


if __name__ == "__main__":
    main()

以下のgifは、ニューラルネットワークの中間データを画像に保存して、それを動画に変換したものです。
ezgif.com-video-to-gif.gif

ちなみに、動画から画像に変換する際は、こちらの記事とかわかりやすかったです!!

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?