Skip to content

Instantly share code, notes, and snippets.

@bingal
Created June 30, 2021 07:11
Show Gist options
  • Save bingal/a20f709cccbcd59dd14f6b8d544e5c1d to your computer and use it in GitHub Desktop.
Save bingal/a20f709cccbcd59dd14f6b8d544e5c1d to your computer and use it in GitHub Desktop.
生成指定尺寸/帧率的测试视频,生成的视频文件非常小
#!/usr/bin/python
# encoding: utf-8
# author: bingal
# descryption: 生成指定尺寸/帧率的测试视频,生成的视频文件非常小
# usage: python make_video.py -w 1920 -H 1080 -f 25 -s 5 -p .
import cv2
import numpy as np
import os
import optparse
def make_video(width, height, fps, seconds, path=None):
if not path:
path = os.path.split(os.path.realpath(__file__))[0]
filename = 'video-{}x{}-fps{}-{}s.mp4'.format(
width, height, fps, seconds)
fp = os.path.join(path, filename)
videoWriter = cv2.VideoWriter(
fp, cv2.VideoWriter_fourcc(*'x264'), fps, (width, height))
for i in range(1, seconds+1):
img = np.zeros((height, width, 3), np.uint8)
img.fill(200)
txt1 = str(i)
txt2 = '{} x {}'.format(width, height)
res = cv2.putText(img, txt1, (int(width/2-120*len(txt1)), int(height/2)),
cv2.FONT_ITALIC, 10, (20, 20, 20), 10)
res = cv2.putText(img, txt2, (int(width/2-len(txt2)*20), int(height/2+100)),
cv2.FONT_ITALIC, 1.8, (20, 20, 20), 3)
# 如下让每张图显示1秒,具体与fps相等
for i in range(fps):
videoWriter.write(res)
videoWriter.release()
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option("-w", "--width", dest="width", help="视频宽度", type="int")
parser.add_option("-H", "--height", dest="height", help="视频高度", type="int")
parser.add_option("-f", "--fps", dest="fps", help="视频每秒帧数", type="int")
parser.add_option("-s", "--second", dest="second",
help="视频时间长度,单位秒", type="int")
parser.add_option("-p", "--path", dest="path", help="视频保存目录,不包括文件名", default=None)
(options, args) = parser.parse_args()
make_video(options.width, options.height,
options.fps, options.second, options.path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment