Python3視頻轉(zhuǎn)字符動(dòng)畫,具體代碼如下所示:
# -*- coding:utf-8 -*- import json import os import subprocess from pathlib import Path from cv2 import cv2 import numpy as np from time import time import webbrowser play_chars_js = ''' let i = 0; window.setInterval(function(){ let img = frames[i++]; let html = "" for(let line of img){ for(let char of line){ let [[r,g,b], ch] = char; html += ' '+ ch + ' ' // html += ' '+ ch + ' ' } html += "
" } document.getElementsByClassName("video-panel")[0].innerHTML = html }, 1000/fps); document.getElementsByTagName("audio")[0].play(); ''' class VideoToHtml: # 像素形狀,因?yàn)轭伾呀?jīng)用rgb控制了,這里的pixels其實(shí)可以隨意排 pixels = "$#@&%ZYXWVUTSRQPONMLKJIHGFEDCBA098765432?][}{/)(>= self.width: return False else: self.resize_width = width self.resize_height = int(self.height * (width / self.width)) return True def set_height(self, height): """只能縮小,而且始終保持長寬比""" if height >= self.height: return False else: self.resize_height = height self.resize_width = int(self.width * (height / self.height)) return True def resize(self, img): """ 將img轉(zhuǎn)換成需要的大小 原則:只縮小,不放大。 """ # 沒指定就不需resize了 if not self.resize_width or not self.resize_height: return img else: size = (self.resize_width, self.resize_height) return cv2.resize(img, size, interpolation=cv2.INTER_CUBIC) def get_img_by_pos(self, pos): """獲取到指定位置的幀""" # 把指針移動(dòng)到指定幀的位置 self.cap.set(cv2.CAP_PROP_POS_FRAMES, pos) # cap.read() 返回值介紹: # ret 布爾值,表示是否讀取到圖像 # frame 為圖像矩陣,類型為 numpy.ndarray. ret, frame = self.cap.read() return ret, frame def get_frame_pos(self): """生成需要獲取的幀的位置,使用了惰性求值""" step = self.fps / self.fps_for_html # 如果未指定 if not self.time_interval: self.frames_count = int(self.frames_count_all / step) # 更新count return (int(step * i) for i in range(self.frames_count)) # 如果指定了 start, end = self.time_interval pos_start = int(self.fps * start) pos_end = int(self.fps * end) self.frames_count = int((pos_end - pos_start) / step) # 更新count return (pos_start + int(step * i) for i in range(self.frames_count)) def get_imgs(self): assert self.cap.isOpened() for i in self.get_frame_pos(): ret, frame = self.get_img_by_pos(i) if not ret: print("讀取失敗,跳出循環(huán)") break yield self.resize(frame) # 惰性求值 # 結(jié)束時(shí)要釋放空間 self.cap.release() def get_char(self, gray): percent = gray / 255 # 轉(zhuǎn)換到 0-1 之間 index = int(percent * (len(self.pixels) - 1)) # 拿到index return self.pixels[index] def get_json_pic(self, img): """測試階段,不實(shí)用""" json_pic = [] # 寬高剛好和size相反,要注意。(這是numpy的特性。。) height, width, channel = img.shape # 轉(zhuǎn)換成灰度圖,用來選擇合適的字符 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for y in range(height): line = [] for x in range(width): r, g, b = img[y][x] gray = img_gray[y][x] char = self.get_char(gray) line.append([[int(r), int(g), int(b)], char]) json_pic.append(line) return json.dumps(json_pic) def write_html_with_json(self, file_name): """測試階段,不實(shí)用""" mp3_path = self.video2mp3() time_start = time() with open(file_name, 'w') as html: # 要記得設(shè)置monospace等寬字體,不然沒法玩 html.write('' '' '' ' ' f' ' '' ' \n' '') def main(): # 視頻路徑,換成你自己的 video_path = "ceshi.mp4" video2html = VideoToHtml(video_path, fps_for_html=8) video2html.set_width(120) html_name = Path(video_path).with_suffix(".html").name video2html.write_html_with_json(html_name) if __name__ == "__main__": main()
總結(jié)
以上所述是小編給大家介紹的Python3視頻轉(zhuǎn)字符動(dòng)畫的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
