您好,登錄后才能下訂單哦!
這篇文章主要介紹了利用Python3怎么將視頻轉換成字符動畫,此處通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考價值,需要的朋友可以參考下:
Python是一種跨平臺的、具有解釋性、編譯性、互動性和面向對象的腳本語言,其最初的設計是用于編寫自動化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發獨立的項目和大型項目。
具體代碼如下所示:
# -*- 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 += '<span >'+ ch + '</span>' // html += '<span >'+ ch + '</span>' } html += "<br>" } document.getElementsByClassName("video-panel")[0].innerHTML = html }, 1000/fps); document.getElementsByTagName("audio")[0].play(); ''' class VideoToHtml: # 像素形狀,因為顏色已經用rgb控制了,這里的pixels其實可以隨意排 pixels = "$#@&%ZYXWVUTSRQPONMLKJIHGFEDCBA098765432?][}{/)(><zyxwvutsrqponmlkjihgfedcba*+1-." def __init__(self, video_path, fps_for_html=8, time_interval=None): """ :param video_path: 字符串, 視頻文件的路徑 :param fps_for_html: 生成的html的幀率 :param time_interval: 用于截取視頻(開始時間,結束時間)單位秒 """ self.video_path = Path(video_path) # 從指定文件創建一個VideoCapture對象 self.cap = cv2.VideoCapture(video_path) self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH) self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT) self.frames_count_all = self.cap.get(cv2.CAP_PROP_FRAME_COUNT) self.fps = self.cap.get(cv2.CAP_PROP_FPS) self.resize_width = None self.resize_height = None self.frames_count = 0 self.fps_for_html = fps_for_html self.time_interval = time_interval def video2mp3(self): """#調用ffmpeg獲取mp3音頻文件""" mp3_path = self.video_path.with_suffix('.mp3') subprocess.call('ffmpeg -i ' + str(self.video_path) + ' -f mp3 ' + str(mp3_path), shell=True) return mp3_path def set_width(self, width): """只能縮小,而且始終保持長寬比""" if width >= 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轉換成需要的大小 原則:只縮小,不放大。 """ # 沒指定就不需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): """獲取到指定位置的幀""" # 把指針移動到指定幀的位置 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("讀取失敗,跳出循環") break yield self.resize(frame) # 惰性求值 # 結束時要釋放空間 self.cap.release() def get_char(self, gray): percent = gray / 255 # 轉換到 0-1 之間 index = int(percent * (len(self.pixels) - 1)) # 拿到index return self.pixels[index] def get_json_pic(self, img): """測試階段,不實用""" json_pic = [] # 寬高剛好和size相反,要注意。(這是numpy的特性。。) height, width, channel = img.shape # 轉換成灰度圖,用來選擇合適的字符 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): """測試階段,不實用""" mp3_path = self.video2mp3() time_start = time() with open(file_name, 'w') as html: # 要記得設置monospace等寬字體,不然沒法玩 html.write('<!DOCTYPE html>' '<html>' '<body >' '<div class="video-panel"></div>' f'<audio src="{mp3_path.name}" autoplay controls></audio>' '</body>' '<script>' 'var frames=[\n') try: i = 0 for img in self.get_imgs(): json_pic = self.get_json_pic(img) html.write(f"{json_pic},") if i % 20: print(f"進度:{i/self.frames_count * 100:.2f}%, 已用時:{time() - time_start:.2f}") i += 1 finally: html.write('\n];\n' f'let fps={self.fps_for_html};\n' f'{play_chars_js}' '</script>\n' '</html>') 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()
到此這篇關于利用Python3怎么將視頻轉換成字符動畫的文章就介紹到這了,更多相關利用Python3怎么將視頻轉換成字符動畫的內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。