欢迎光临散文网 会员登陆 & 注册

Termux 字符画转视频

2023-08-08 12:25 作者:真雷神托儿  | 我要投稿


import CV2,sys,os
import numpy as np
from tqdm import tqdm
from PIL import Image,ImageDraw,ImageFont
from multiprocessing import Process,Queue,Manager

img_str = Manager().dict()
cap_list = Queue(100)
IMG2STR_CPU_COUNT = 4
END = Queue(1)
FPS = Queue(2)
PP = 0.15

pix =''' .'`^",:;Il!i><~+_-?][}{1)(|\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@'''
pixgray2str= lambda p: pix[(255 - p) * 68 //255]

def cap_while(cap_list,mv_path,END,FPS) :
    cap = CV2.VideoCapture(mv_path) 
    if not FPS.full() :
        FPS.put(cap.get(CV2.CAP_PROP_FPS))
        FPS.put(int(cap.get(CV2.CAP_PROP_FRAME_COUNT)))
    b = 0
    while True :
        a = cap.read()
        if a[0] == True:
            cap_list.put((b,a[1]))
            b += 1
        else :
            for _ in range(IMG2STR_CPU_COUNT) :
                cap_list.put(False)
            END.put(b)
            cap.release()
            break 

def img2str(cap_list,img_str) :
    while True :
        if len(img_str) >= 500 :
            continue
        A = cap_list.get()
        if A :
            img = A[1]
            gray_img = CV2.cvtColor(img,CV2.COLOR_RGB2GRAY)
            Y,X = gray_img.shape
            s_img = CV2.resize(gray_img,(int(X*PP),int(Y*PP)))
            y,x = s_img.shape
            #print(y,x)
            txt = ''
            for i in s_img :
                for p in i :
                    txt += pixgray2str(p)*2
                txt += '\n'
            font = ImageFont.load_default()
            bg_color = 'black'
            #bg_color = 'white'
            im_out = Image.new('RGB',(x*10,y*10), bg_color)
            draw = ImageDraw.Draw(im_out)
            draw.text((0,0),txt,fill=(255,255,255),font=font)
            img_new_str = CV2.cvtColor(np.asarray(im_out), CV2.COLOR_RGB2BGR)
            img_str[A[0]] = img_new_str
        else :
            break
        
def videowriter() :
    os.system('clear')
    fps = FPS.get()
    frames = FPS.get()
    pbar = tqdm(total=frames)
    while True :
        if 0 in img_str :
            WH = img_str[0].shape
            break
    width_height = (WH[1],WH[0])
    fourcc = CV2.VideoWriter_fourcc(*'mp4v')
    video = CV2.VideoWriter(sys.path[0]+'/out.mp4', fourcc,fps,width_height,isColor=True)
    K = 0
    end = None
    while True :
        if END.full() :
            end = END.get()
        if K == end :
            video.release()
            print('文件保存在 :',sys.path[0]+'/out.mp4')
            break
        if K in img_str :
            video.write(img_str[K])
            del img_str[K]
            K+=1
            pbar.update(1)

if __name__ == '__main__' :
    mv_path = input('要处理的视频 :')
    #获取每一帧
    P=Process(target=cap_while,daemon=True,args=(cap_list,mv_path,END,FPS))
    P.start()
    #处理每一帧
    for _ in range(IMG2STR_CPU_COUNT) :
        Process(target=img2str,daemon=True,args=(cap_list,img_str)).start()
    #合成
    videowriter()    
            


Termux 字符画转视频的评论 (共 条)

分享到微博请遵守国家法律