Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Shapaper authored Aug 11, 2021
1 parent a1bd787 commit b748018
Show file tree
Hide file tree
Showing 3 changed files with 400 additions and 0 deletions.
219 changes: 219 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
#-*-coding:utf-8-*-
import pygame
import os
#win.fill((255,255,255))
#text = u"H" #将文本以unicode编码格式存储
#font = pygame.font.Font("F:\\SIMSUN.TTC", 60) #设置字体
#ftext = font.render(text, True, (0,0,0)) #渲染字体
#win.blit(ftext,(0,0))
#pygame.display.update()
#pygame.image.save(win, os.path.join("1.png"))
##################
import cv2
import time
import subprocess
import imageio
import os
import re
from PIL import Image
from alive_progress import alive_bar
show_heigth = 64
show_width = 240
print("请将视频放入《.video》文件夹内的自定义文件夹,视频名称改为main.mp4(必须是MP4)")
shipingpathh=input("视频名 如【文件 ./.video/1_sa_ri_lang/main.mp4 】为【1_sa_ri_lang】[目前支持中文了!]——那么你的视频是:")
shipingpath="./.video/"+str(shipingpathh)+"/main.mp4"
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
#生成一个ascii字符列表
char_len = len(ascii_char)

vc = cv2.VideoCapture(shipingpath) #加载一个视频
from moviepy.editor import *

print("总计"+str(vc.get(7))+"帧,预计使用"+str(0.3*vc.get(7)*(1+3+3))+"MB硬盘空间用作 缓存 !")
woyebuzhidao=input("请保证有足够的【硬盘】空间用作【缓存】!按回车键继续")

print("进行步骤1/6 提取视频声音")
video = VideoFileClip(shipingpath)
audio = video.audio
audio.write_audiofile('./huancun/ptest.mp3')

frames_nFPS=vc.get(5)#获取视频帧速率
if vc.isOpened(): #判断是否正常打开
rval , frame = vc.read()
else:
rval = False

frame_count = 0
outputList = [] #初始化输出列表
allzhengshu=int(vc.get(7))
print("进行步骤2/6 视频转为字符")
with alive_bar(int(allzhengshu)) as bar:
while rval: #循环读取视频帧
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #使用opencv转化成灰度图
gray = cv2.resize(gray,(show_width,show_heigth))#resize灰度图
text = ""
for pixel_line in gray:
for pixel in pixel_line: #字符串拼接
text += ascii_char[int(pixel / 256 * char_len )]
text += "\n"
outputList.append(text)
frame_count = frame_count + 1
rval, frame = vc.read()
bar()
###
print("进行步骤3/6 字符转为图片")
with alive_bar(int(allzhengshu)) as bar:
zhengshu=0

pygame.init() #需要初始化
win = pygame.display.set_mode((1920,1080))
font = pygame.font.SysFont("simsunnsimsun", 16) # 使用系统字体
for frame in outputList:
#os.system("cls") #清屏
all_frame=frame.split('\n')
win.fill((255,255,255))#清屏

for i, v in enumerate(all_frame):
#print(i, v)
ftext = font.render(v, True, (0,0,0)) #渲染字体
win.blit(ftext,(0,i*20))
pygame.display.update()
####
pygame.image.save(win, os.path.join("./huancun/picture/"+str(zhengshu)+".png"))

zhengshu=zhengshu+1
bar()
#print(frame)
#print()
#print()
import numpy as np
import cv2
#读取一张图片
size = (1920,1080)
#print(size," ",frames_nFPS)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
#完成写入对象的创建,第一个参数是合成之后的视频的名称,第二个参数是可以使用的编码器,第三个参数是帧率即每秒钟展示多少张图片,第四个参数是图片大小信息
videowrite = cv2.VideoWriter(r'./huancun/pvideo/ptest.avi',fourcc,frames_nFPS,size)#20是帧数,size是图片尺寸
cishua=0
print("进行步骤4/6 图片转为视频")
with alive_bar(int(allzhengshu)) as bar:
for filename in [r'./huancun/picture/{0}.png'.format(i) for i in range(allzhengshu)]:
img = cv2.imread(filename)
videowrite.write(img)
cishua=cishua+1
if img is None:
print(filename + " is error!")
continue
bar()
videowrite.release()
#print('end!')

print("进行步骤5/6 音轨视频混合")
cmd = f"ffmpeg -y -i ./huancun/pvideo/ptest.avi -i ./huancun/ptest.mp3 -acodec copy -vcodec copy ./huancun/pvideo/ptest.mp4"
process=subprocess.Popen(cmd,stderr=subprocess.PIPE,bufsize=0,encoding="utf-8",universal_newlines=True,shell=True)

duration = None
cishua=0
with alive_bar(int(allzhengshu)) as bar:
while process.poll() is None:
line = process.stderr.readline().strip()
if line:
duration_res = re.search(r'Duration: (?P<duration>\S+)', line)
if duration_res is not None:
duration = duration_res.groupdict()['duration']
duration = re.sub(r',', '', duration)

frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame=(?P<frame>\S+)', line)
if (frame is not None) and (frame!=''):
wcframe=str(frame.group())
for wcc in range(cishua,int(wcframe[wcframe.find('frame=')+6:len(wcframe)])):
bar()
cishua=int(wcframe[wcframe.find('frame=')+6:len(wcframe)])
#print(wcframe[wcframe.find('frame=')+6:wcframe.find("'>")])
#print(frame[frame.find('frame=')+5:frame.find("'>")])
for wcc in range(cishua,int(allzhengshu)):
bar()
print("进行步骤6/6 压缩比特率")
cmd = f"ffmpeg -y -i ./huancun/pvideo/ptest.mp4 -b:v 20000k -bufsize 20000k ./.video/"+str(shipingpathh)+"/output.mp4"
process=subprocess.Popen(cmd,stderr=subprocess.PIPE,bufsize=0,encoding="utf-8",universal_newlines=True,shell=True)

duration = None
cishua=0
with alive_bar(int(allzhengshu)) as bar:
while process.poll() is None:
line = process.stderr.readline().strip()
if line:
duration_res = re.search(r'Duration: (?P<duration>\S+)', line)
if duration_res is not None:
duration = duration_res.groupdict()['duration']
duration = re.sub(r',', '', duration)

frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame= (?P<frame>\S+)', line)
if frame is None:
frame = re.search(r'frame=(?P<frame>\S+)', line)
if frame is not None:
wcframe=str(frame.group())
for wcc in range(cishua,int(wcframe[wcframe.find('frame=')+6:len(wcframe)])):
bar()
cishua=int(wcframe[wcframe.find('frame=')+6:len(wcframe)])
#print(wcframe[wcframe.find('frame=')+6:wcframe.find("'>")])
#print(frame[frame.find('frame=')+5:frame.find("'>")])
for wcc in range(cishua,int(allzhengshu)):
bar()

print("删除临时图片籍中(时间较长,请稍后)")
cmd = f"rmdir /s/q .\huancun\picture"
print(cmd)
subprocess.call(cmd,shell=True)
cmd = f"md .\huancun\picture"
print(cmd)
subprocess.call(cmd,shell=True)

print("删除临时视频1/2中")
cmd = f"del /q .\huancun\pvideo\ptest.avi"
print(cmd)
subprocess.call(cmd,shell=True)

print("删除临时视频2/2中")
cmd = f"del /q .\huancun\pvideo\ptest.mp4"
print(cmd)
subprocess.call(cmd,shell=True)

print("删除临时音频中")
cmd = f"del /q .\huancun\ptest.mp3"
print(cmd)
subprocess.call(cmd,shell=True)
178 changes: 178 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
aiocache==0.11.1
aiocqhttp==1.4.0
aiofiles==0.7.0
aiohttp==3.7.4.post0
alive-progress==1.6.2
altgraph==0.16.1
anyio==3.2.1
appdirs==1.4.3
APScheduler==3.6.0
arrow==1.1.1
asgiref==3.3.1
async-timeout==3.0.1
attrs==20.2.0
beautifulsoup4==4.9.3
bilibili-api==4.1.0
binaryornot==0.4.4
blinker==1.4
certifi==2019.3.9
chardet==3.0.4
click==7.1.2
colorama==0.4.4
common==0.1.2
comtypes==1.1.7
cookiecutter==1.7.3
crypto==1.4.1
cssutils==2.2.0
cycler==0.10.0
decorator==4.4.2
Django==3.1.6
dukpy==0.2.2
echarts-china-cities-pypkg==0.0.9
echarts-china-misc-pypkg==0.0.1
echarts-china-provinces-pypkg==0.0.3
echarts-countries-pypkg==0.1.6
echarts-united-kingdom-pypkg==0.0.1
ExifRead==2.1.2
face-recognition-models==0.3.0
fastapi==0.63.0
future==0.17.1
get==2019.4.13
h11==0.9.0
h2==4.0.0
hpack==4.0.0
httpcore==0.12.3
httpx==0.16.1
Hypercorn==0.11.2
hyperframe==6.0.1
idna==2.8
image==1.5.33
imageio==2.9.0
imageio-ffmpeg==0.4.4
importlib-metadata==2.0.0
iso8601==0.1.12
itchat==1.3.10
itsdangerous==2.0.1
javascripthon==0.10
jieba==0.39
Jinja2==2.10.1
jinja2-time==0.2.0
JsonForm==0.0.2
jsonschema==3.2.0
JsonSir==0.0.2
jupyter-echarts-pypkg==0.1.2
kiwisolver==1.0.1
llvmlite==0.36.0
lml==0.0.2
loguru==0.5.3
macholib==1.11
macropy3==1.1.0b2
Mako==1.1.4
MarkupSafe==1.1.1
matplotlib==3.0.3
mcpi==1.2.0
minecraftstuff==1.0.1
moviepy==1.0.3
multidict==5.1.0
Naked==0.1.31
nb-cli==0.3.2
nonebot==1.8.3
nonebot2==2.0.0a10
numba==0.53.1
numpy==1.16.2
opencv-contrib-python==4.1.0.25
opencv-python==4.1.0.25
packaging==20.9
panda3d==1.10.7
pefile==2018.8.8
pexpect==4.8.0
Pillow==6.0.0
post==2019.4.13
poyo==0.5.0
priority==2.0.0
proglog==0.1.9
prompt-toolkit==1.0.14
psutil==5.6.2
ptyprocess==0.7.0
public==2019.4.13
PyAutoGUI==0.9.42
pycairo==1.20.0
pydantic==1.8.2
pyecharts==0.5.11
pyecharts-javascripthon==0.0.6
pyecharts-jupyter-installer==0.0.3
pyfiglet==0.8.post1
pygame==1.9.5
PyGetWindow==0.0.5
pyglet==1.5.7
Pygments==2.9.0
pygtrie==2.4.2
PyInquirer==1.0.3
PyInstaller==3.4
PyMsgBox==1.0.6
PyOpenGL==3.1.0
PyOpenGL-accelerate @ file:///C:/Users/lenovo/Downloads/PyOpenGL_accelerate-3.1.5-cp37-cp37m-win_amd64.whl
pyparsing==2.3.1
pypng==0.0.19
PyPrind==2.11.3
PyQRCode==1.2.1
PyQt5==5.15.4
PyQt5-Qt5==5.15.2
PyQt5-sip==12.9.0
PyQtWebEngine==5.15.4
PyQtWebEngine-Qt5==5.15.2
pyreadline==2.1
PyRect==0.1.4
pyrsistent==0.17.3
PyScreeze==0.1.21
python-dateutil==2.8.0
python-dotenv==0.18.0
Python-EasyConfig==0.1.7
python-for-android==0.7.0
python-resources==0.3
python-slugify==5.0.2
pytools==2021.1
PyTweening==1.0.3
pytz==2019.1
PyWavefront==1.3.3
pywifi==1.1.12
pywin32==224
pywin32-ctypes==0.2.0
PyYAML==5.1
qqbot==2.3.11
qrcode==7.2
Quart==0.14.1
query-string==2019.4.13
regex==2021.7.6
request==2019.4.13
requests==2.25.1
Resource==0.2.1
rfc3986==1.5.0
selenium==3.141.0
serial==0.0.97
sh==1.12.14
shellescape==3.4.1
Sift==5.0.1
sip==6.1.0
six==1.12.0
sniffio==1.2.0
soupsieve==2.2.1
sqlparse==0.4.1
starlette==0.13.6
text-unidecode==1.3
tk==0.1.0
toml==0.10.2
torch==1.8.1
tqdm==4.62.0
typing-extensions==3.10.0.0
tzlocal==1.5.1
urllib3==1.24.1
uvicorn==0.11.8
warp==0.0.1
wcwidth==0.2.5
websockets==8.1
Werkzeug==2.0.1
win32-setctime==1.0.3
wsproto==1.0.0
yarl==1.6.3
zipp==3.3.1
Loading

0 comments on commit b748018

Please sign in to comment.