This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCmdPlayer.py
146 lines (121 loc) · 3.45 KB
/
CmdPlayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import cv2
import sys
import multiprocessing
import time
import os
import win32api
import win32console
import pydub
from pydub.playback import play
import ctypes
#载入调用c函数的动态链接库
Drawdll=ctypes.cdll.LoadLibrary("./CmdDraw.dll")
#字符集
pixels = " .,-'`:!1+*abcdefghijklmnopqrstuvwxyz<>()\/{}[]?234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ%&@#$"
#反向字符集
#pixels = "$#@&%ZYXWVUTSRQPONMLKJIHGFEDCBA098765432?][}{/\)(><zyxwvutsrqponmlkjihgfedcba*+1!:`'-,. "
#获取字符集长度
length=len(pixels)
#字符帧存储队列 进程共享用
q=multiprocessing.Queue()
#视频帧转换到字符帧
def image2chars(image):
ret=[]
height,width=image.shape
for i in range(height):
line=""
for j in range(width):
percent=image[i][j]/255
index=int(percent*(length-1))
line+=pixels[index]
ret.append(line)
return ret
#转换进程函数
def video2chars(q,video_name,size):
cap=cv2.VideoCapture(video_name)
while cap.isOpened():
ret,frame=cap.read()
if ret:
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
image=cv2.resize(gray,size,interpolation=cv2.INTER_AREA)
q.put(image2chars(image))
else:
print("Error when reading frame")
break
cap.release()
#字符帧打印进程函数
def draw(q,fps,width,height):
Drawdll.console_init(height,width)
runtime=0
delay=0.98/fps
#delay=1/(fps+(width*height/1000))
while True:
start=time.time()
for value in q.get():
print(value)
print("fps=%f runtime=%f"%(fps,runtime))
end=time.time()
runtime=start-end
if (delay+runtime>0):
time.sleep(delay+runtime)
Drawdll.gotoxy(0,0)
#获取视频音轨
def Getbacksound(video_name):
temp=video_name.split('.',1)
file_name=temp[0]+".mp3"
#print(file_name)
if os.path.isfile(file_name):
return file_name
cmd="ffmpeg -i "+video_name+" -f mp3 -vn "+file_name
os.system(cmd)
return file_name
#音轨播放
def musicplay(music_name):
#print(music_name)
sound=pydub.AudioSegment.from_file(str(music_name),format="mp3")
play(sound)
#获取视频fps
def Getfps(video_name):
cap=cv2.VideoCapture(video_name)
if not(cap.isOpened()):
print("Can't find the mv!")
sys.exit(0)
ret=cap.get(5)
cap.release()
return ret
#主程序
#name=""
if __name__ == "__main__":
try:
video_name=str(sys.argv[1])
if video_name=="0":
video_name=0
height=int(sys.argv[2])
width=int(sys.argv[3])
music_boot=str(sys.argv[4])
size=(height,width)
except:
print("Usage: CmdPlayer.py <video_name> <width> <height> <music_setting on|off>")
print("Tips:size 128:64 is recommend")
sys.exit(0)
fps=Getfps(video_name)
music_name=""
if music_boot=="on" :
music_name=Getbacksound(video_name)
#print(music_name)
#多进程
p1=multiprocessing.Process(target=video2chars,args=(q,video_name,size))
p2=multiprocessing.Process(target=draw,args=(q,fps,width,height))
if music_boot=="on":
p3=multiprocessing.Process(target=musicplay,args=(music_name,))
#进程启动
p1.start()
time.sleep(1)
p2.start()
if music_boot=="on":
p3.start()
#进程join
p1.join()
p2.join()
if music_boot=="on":
p3.join()