1. 程式人生 > >視頻加字幕

視頻加字幕

end 動態 time 總數 .com tail print frame val

import os

os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[
    -1]

f_mp3 = ‘{}{}{}‘.format(this_file_dirname, os_sep, ‘auido.mp3‘)
from playsound import playsound

import time
import math

this_time = time.time()

# playsound(f_mp3)

# t_spend = time.time() - this_time
t_spend = 58.777058839797974
# 音頻的秒數
t_spend = math.ceil(t_spend)
import cv2
import glob



‘‘‘
python+opencv視頻圖像相互轉換 - CSDN博客 https://blog.csdn.net/m0_37733057/article/details/79023693
鏈接:https://www.zhihu.com/question/49558804/answer/343058915

OpenCV: Drawing Functions in OpenCV https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html

‘‘‘
# 每秒傳輸幀數(Frames Per Second)
fps = 100  # 保存視頻的FPS,可以適當調整 FPS是圖像領域中的定義,是指畫面每秒傳輸幀數,通俗來講就是指動畫或視頻的畫面數。FPS是測量用於保存、顯示動態視頻的信息數量。每秒鐘幀數愈多,所顯示的動作就會愈流暢。通常,要避免動作不流暢的最低是30。某些計算機視頻格式,每秒只能提供15幀。
fps = 15
fps = 5

fourcc = cv2.VideoWriter_fourcc(‘M‘, ‘J‘, ‘P‘, ‘G‘)  # opencv3.0

f_v = ‘{}{}‘.format(int(time.time()), ‘saveVideo.avi‘)
f_img_d = ‘{}{}{}{}{}‘.format(this_file_dirname, os_sep, ‘mypng‘, os_sep, ‘*.jpg‘)
imgs = glob.glob(f_img_d)

img = cv2.imread(imgs[0])
img_size = (img.shape[1], img.shape[0])
videoWriter = cv2.VideoWriter(f_v, fourcc, fps, img_size)

"""
用圖片總數均分音頻時間
"""
os_delay_factor=0.14
os_delay_factor=0.11
myinterval = t_spend / len(imgs)*os_delay_factor

for imgname in imgs:
    this_time = time.time()
    while time.time() - this_time < myinterval:
        print(imgname)
        img = cv2.imread(imgname)
        mytxt = ‘{}{}‘.format(‘OpenCV-text-xl-‘, time.time())
        text, font = mytxt, cv2.FONT_HERSHEY_SIMPLEX
        org_step=10
        org=(int(img.shape[1]/2-org_step), int(img.shape[0]/2-org_step))
        cv2.putText(img, text, org, font, 1, (0, 0, 0), 2, cv2.LINE_AA)
        videoWriter.write(img)
        videoWriter.write(img)

videoWriter.release()

  

視頻加字幕