File: generate_video_from_frames.py

package info (click to toggle)
freerdp2 2.3.0%2Bdfsg1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 27,060 kB
  • sloc: ansic: 330,648; xml: 1,676; cpp: 821; sh: 748; python: 638; perl: 231; lisp: 120; makefile: 86
file content (47 lines) | stat: -rw-r--r-- 1,336 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import argparse
import time
import cv2
from os.path import join, getmtime

def get_image_size(path):
    img = cv2.imread(path)
    height, width, _ = img.shape
    return (width, height)

def generate_video(output, frames, size, fps):
    out = cv2.VideoWriter(
        args.output, cv2.VideoWriter_fourcc(*'DIVX'), args.fps, size)

    for frame in frames:
        img = cv2.imread(frame)
        out.write(img)

    out.release()

def main(args):
    # Load input frames, sorted by creation time.
    files = [join(args.input, f) for f in os.listdir(
        args.input) if os.path.isfile(join(args.input, f))]
    files.sort(key=lambda x: getmtime(x))

    print('Generating video...')
    print(f'Frame count: {len(files)}')

    start = time.time()
    generate_video(args.output, files, get_image_size(files[0]), args.fps)

    print(
        f'Output file {args.output} generated in {time.time() - start} seconds.')


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-i", "--input", help="path to a directory containing all frames")
    parser.add_argument(
        "-o", "--output", help="avi output file path", default="video.avi")
    parser.add_argument("-f", "--fps", type=int, help="frames per second", default=8)
    args = parser.parse_args()

    main(args)