基于C#实现乱码视频效果
作者:Csharp小记 时间:2022-01-25 14:29:56
文章描述
这个效果可能很多人都在抖音看到过,即把一个短视频,转成数字、字母等乱码组成的形式进行播放。
开发环境
.NET Framework版本:4.5
开发工具
Visual Studio 2013
实现代码
private void btn_convert_Click(object sender, EventArgs e)
{
string framePath = Application.StartupPath + "\\temp";
if (Directory.Exists(framePath))
{
Directory.Delete(framePath, true);
}
Directory.CreateDirectory(framePath);
//string newFramePath = Application.StartupPath + "\\newTemp";
//if (Directory.Exists(newFramePath))
//{
// Directory.Delete(newFramePath, true);
//}
//Directory.CreateDirectory(newFramePath);
FFmpegUtil.Split(axWindowsMediaPlayer1.URL, framePath);
List<char> listChar = new List<char>();
for (char i = 'a'; i <= 'z'; i++)
{
listChar.Add(i);
}
for (char i = 'A'; i <= 'Z'; i++)
{
listChar.Add(i);
}
for (char i = '0'; i <= '9'; i++)
{
listChar.Add(i);
}
string[] imgUrls = Directory.GetFiles(framePath);
Bitmap img = null;
for (int u = 0; u < imgUrls.Length; u++)
{
img = (Bitmap)Image.FromFile(imgUrls[u]);
double threshold = ImageUtil.GetAverageThreshold(img);
ImageUtil.Threshold(img, threshold);
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int y = 0; y < img.Height; y += 16)
{
for (int x = 0; x < img.Width; x += 8)
{
if (ImageUtil.GetGrayValue(img.GetPixel(x, y)) < threshold)
{
builder.Append(listChar[random.Next(62)]);
}
else
{
builder.Append(" ");
}
}
builder.Append("\r\n");
}
File.WriteAllText(@"C:\Users\Zero\Desktop\a\b\" + u + ".txt", builder.ToString());
}
MessageBox.Show("转换完成");
}
private void btn_show_Click(object sender, EventArgs e)
{
new FormShow(@"C:\Users\Zero\Desktop\a\b\").Show();
}
private void FormShow_Load(object sender, EventArgs e)
{
List<string> list = Directory.GetFiles(_path, "*.txt").ToList();
if (list.Count == 0)
{
return;
}
list.Sort((a, b) =>
{
string reg = "[0-9]+";
int a_index = int.Parse(Regex.Match(a, reg).Value);
int b_index = int.Parse(Regex.Match(b, reg).Value);
return a_index.CompareTo(b_index);
});
richTextBox1.Text = File.ReadAllText(list[0]);
Width = richTextBox1.Lines.Max(s => s).Length * 5;
Height = richTextBox1.Lines.Length * 10;
Location = new Point(Location.X, 10);
new Task(async () =>
{
foreach (string str in list)
{
if (!this.IsDisposed)
{
this.BeginInvoke(new Action(() =>
{
richTextBox1.Text = File.ReadAllText(str);
}));
}
await Task.Delay(260);
}
}).Start();
}
实现效果
代码解析:实现原理是首先利用ffmpeg将视频分割成一帧帧的图片,然后将每张图片进行一个二值化处理,最后判断灰度值,大于阈值的情况下随便输入一个字符,否则就输入空格。这里是使用了字符串来记录了各个图片的生成情况,最后保存到一个个的记事本中去。
在展示页面中,只需要将文件夹中的记事本文档按照顺序排列并逐个读取显示即可。
来源:https://mp.weixin.qq.com/s/9i7sXVbunXE2y8omNJhs2w
标签:C#,乱码,视频
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot2.X整合prometheus监控的实例讲解
2021-07-04 13:49:59
![](https://img.aspxhome.com/file/2023/2/82442_0s.jpg)
Unity中C#和Java的相互调用实例代码
2022-02-28 13:40:53
SpringBoot自动配置原理详解
2023-08-19 09:25:55
![](https://img.aspxhome.com/file/2023/6/58096_0s.jpg)
C#获取U盘序列号的方法
2023-09-15 02:09:54
关于EntityWrapper的in用法
2023-11-29 09:02:11
![](https://img.aspxhome.com/file/2023/3/60353_0s.png)
C#中using语句的用法
2023-07-02 05:33:24
![](https://img.aspxhome.com/file/2023/3/76393_0s.jpg)
SpringBoot中的Aop用法示例详解
2023-03-31 14:12:00
![](https://img.aspxhome.com/file/2023/6/62986_0s.png)
详解C语言实现猜数字游戏
2023-11-03 04:58:51
![](https://img.aspxhome.com/file/2023/7/105177_0s.jpg)
java同步之如何写一个锁Lock
2023-04-23 08:10:54
![](https://img.aspxhome.com/file/2023/3/63843_0s.png)
使用AccessibilityService实现自动遍历点赞功能
2023-10-18 16:22:53
![](https://img.aspxhome.com/file/2023/9/88089_0s.gif)
Android 自定义ProgressDialog进度条对话框用法详解
2022-02-14 19:31:04
![](https://img.aspxhome.com/file/2023/2/95002_0s.png)
浅谈利用Session防止表单重复提交
2022-02-08 00:45:42
java 多态与抽象类详解总结
2022-04-19 10:59:18
![](https://img.aspxhome.com/file/2023/5/67135_0s.jpg)
Android二维码的生成与扫码-zxing示例代码
2022-10-17 11:33:49
![](https://img.aspxhome.com/file/2023/3/89553_0s.gif)
JAVA面试题 从源码角度分析StringBuffer和StringBuilder的区别
2023-07-24 07:29:07
![](https://img.aspxhome.com/file/2023/3/95463_0s.png)
Java并发编程之线程安全性
2021-12-22 07:43:41
C#远程发送和接收数据流生成图片的方法
2021-08-31 00:30:10
29个要点帮你完成java代码优化
2022-11-06 05:16:26
SpringMVC解析JSON请求数据问题解析
2023-06-02 21:41:32
![](https://img.aspxhome.com/file/2023/8/60578_0s.png)
springboot动态定时任务的实现方法示例
2023-04-20 15:59:58