【BadApple】badApple但壞掉的我并且是800年前畫質(zhì),新人爛活

#代碼極其簡(jiǎn)單:
import CV2 as cv
import os
import time
def PlayVideo(fileName):
? ? # 替換字符列表
? ? ascii_char = list(r" █")
? ? char_len = len(ascii_char)
? ? # 加載視頻
? ? cap = cv.VideoCapture(fileName)
? ? termSize = os.get_terminal_size()
? ? texts = []
? ? while True:
? ? ? ? # 讀取視頻每一幀 跳幀
? ? ? ? for i in range(3):
? ? ? ? ? ? hasFrame, frame = cap.read()
? ? ? ? if not hasFrame:
? ? ? ? ? ? break
? ? ? ? # 視頻長(zhǎng)寬
? ? ? ? width = frame.shape[0]
? ? ? ? height = frame.shape[1]
? ? ? ? # 轉(zhuǎn)灰度圖
? ? ? ? img_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
? ? ? ? # 縮小圖片并調(diào)整長(zhǎng)寬比
? ? ? ? img_resize = cv.resize(img_gray, (termSize.columns, termSize.lines - 1))
? ? ? ? text = ''
? ? ? ? # 遍歷圖片中的像素
? ? ? ? for row in img_resize:
? ? ? ? ? ? for pixel in row:
? ? ? ? ? ? ? ? # 根據(jù)像素值,選取對(duì)應(yīng)的字符
? ? ? ? ? ? ? ? text += ascii_char[int(pixel / 256 * char_len)]
? ? ? ? ? ? text += '\n'
? ? ? ? texts.append(text)
? ? ? ?
? ? for text in texts:
? ? ? ? # 清屏
? ? ? ? os.system('clear')
? ? ? ? print(text)
? ? ? ? time.sleep(0.005)
# PlayVideo END
filename = input("視頻文件名:")
PlayVideo(filename)