badapple_ANSI download

音频转字符视频

ANSI_Video.h 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include<opencv2/opencv.hpp>
#include<Windows.h>

constexpr char str[] = "MN@?#*b1oc, ";

namespace ANSI_Video
{
//帧转化为字符串
std::unique_ptr<std::vector<std::vector<char>>> FrametoANSI(cv::Mat& frame, float size = 1);

//重置光标
void clc(HANDLE hOutput);

//参数1文件路径,参数二输出字符密度(越小密度越大,建议不要小于1,不同配置电脑性能不一样)
bool ANSI_Video_Player(const cv::String& filename, float ANSI_pix = 4);
}

ANSI_Video.cpp 文件

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
#include "ANSI_Video.h"

#pragma comment(lib,"WINMM.LIB")
namespace ANSI_Video
{
std::unique_ptr<std::vector<std::vector<char>>> ANSI_Video::FrametoANSI(cv::Mat& frame, float size)
{
cv::Mat output;
cv::resize(frame, output, cv::Size(frame.cols / size, frame.rows / 2 / size));

std::unique_ptr<std::vector<std::vector<char>>> map_(new std::vector<std::vector<char>>);

map_->resize(output.rows);
for (auto& a : *(map_))
{
a.reserve(output.cols + 1);
}

for (int i = 0; i != output.rows; i++)
{
uchar* row = output.ptr(i);
for (int j = 0; j != output.cols; j++)
{
(*map_)[i].emplace_back(str[row[j] * 3 / 64]);
}
(*map_)[i].emplace_back('\0');
}

return map_;
}

inline void clc(HANDLE hOutput)
{
COORD coord = { 0,0 };
SetConsoleCursorPosition(hOutput, coord);
}

bool ANSI_Video::ANSI_Video_Player(const cv::String& filename, float pix)
{
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
cv::VideoCapture cap;
cap.open(filename);

if (!cap.isOpened())
{
std::cout << "不能打开视频文件" << std::endl;
return false;
}
//获取每秒帧率
double fps = cap.get(cv::CAP_PROP_FPS);

//获取时间
LARGE_INTEGER nFreq, t1, t2;
QueryPerformanceFrequency(&nFreq);
double dt;


std::cout << "请调整好大小后回车使用" << std::endl;
system("pause");
clc(hOutput);
QueryPerformanceCounter(&t1);
int flushtime = 1000 / fps * 1000;
PlaySoundW(L"BA.wav", NULL, SND_FILENAME | SND_ASYNC);
while (true)
{
cv::Mat frame, frame_;
bool rSucess = cap.read(frame);
if (!rSucess)
{
std::cout << "不能从视频文件中读取帧" << std::endl;
break;
}
else
{
//黑白处理
cv::cvtColor(frame, frame_, cv::COLOR_BGR2GRAY);
//转化为字符串
auto a = FrametoANSI(frame_, pix);

//打印
for (auto& n : (*a))
{
std::cout << n.data() << std::endl;
}
do {
QueryPerformanceCounter(&t2);
} while ((t2.QuadPart - t1.QuadPart) / float(nFreq.QuadPart) * 1000 * 1000 < flushtime);
t1 = t2;
clc(hOutput);
}
}
return true;
}
}