基于C#實(shí)現(xiàn)亂碼視頻效果
文章描述
這個效果可能很多人都在抖音看到過,即把一個短視頻,轉(zhuǎn)成數(shù)字、字母等亂碼組成的形式進(jìn)行播放。
開發(fā)環(huán)境
.NET Framework版本:4.5
開發(fā)工具
Visual Studio 2013
實(shí)現(xiàn)代碼
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("轉(zhuǎn)換完成"); } 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(); }
實(shí)現(xiàn)效果
代碼解析:實(shí)現(xiàn)原理是首先利用ffmpeg將視頻分割成一幀幀的圖片,然后將每張圖片進(jìn)行一個二值化處理,最后判斷灰度值,大于閾值的情況下隨便輸入一個字符,否則就輸入空格。這里是使用了字符串來記錄了各個圖片的生成情況,最后保存到一個個的記事本中去。
在展示頁面中,只需要將文件夾中的記事本文檔按照順序排列并逐個讀取顯示即可。
到此這篇關(guān)于基于C#實(shí)現(xiàn)亂碼視頻效果的文章就介紹到這了,更多相關(guān)C#亂碼視頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)計(jì)算兩個坐標(biāo)點(diǎn)直接距離的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了C#中幾種常見場景下兩個坐標(biāo)點(diǎn)直接距離的計(jì)算方法,文中的示例代碼講解詳細(xì),有需要的可以參考一下2024-04-04C#實(shí)現(xiàn)根據(jù)字節(jié)數(shù)截取字符串并加上省略號的方法
這篇文章主要介紹了C#實(shí)現(xiàn)根據(jù)字節(jié)數(shù)截取字符串并加上省略號的方法,比較實(shí)用的功能,需要的朋友可以參考下2014-07-07C#環(huán)形隊(duì)列的實(shí)現(xiàn)方法詳解
這篇文章先是簡單的給大家介紹了什么是環(huán)形隊(duì)列和環(huán)形隊(duì)列的優(yōu)點(diǎn),然后通過實(shí)例代碼給大家介紹C#如何實(shí)現(xiàn)環(huán)形隊(duì)列,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09ASP.NET MVC 5使用X.PagedList.Mvc進(jìn)行分頁教程(PagedList.Mvc)
這篇文章主要介紹了ASP.NET MVC 5使用X.PagedList.Mvc進(jìn)行分頁教程(原名為PagedList.Mvc),需要的朋友可以參考下2014-10-10Unity中協(xié)程IEnumerator的使用方法介紹詳解
本文主要介紹了Unity中協(xié)程IEnumerator的使用方法介紹詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C#使用NPOI將List數(shù)據(jù)導(dǎo)出到Excel文檔
這篇文章主要為大家詳細(xì)介紹了C#使用NPOI將List數(shù)據(jù)導(dǎo)出到Excel文檔,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02C#?彈出窗口show()和showdialog()的兩種方式
本文主要介紹了C#?彈出窗口show()和showdialog()的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07