C#實(shí)現(xiàn)文字視頻生成器的示例代碼
前言
簡(jiǎn)單的描述下寫(xiě)這個(gè)軟件的背景吧。之前短視頻平臺(tái)很火的時(shí)候,相信很多人都想進(jìn)去分一杯羹,俺當(dāng)然也不能免俗,但是人丑家窮又沒(méi)才藝,咋辦呢?看到別人有只發(fā)文字啥的一些視頻加點(diǎn)背景音樂(lè)也能看,想著,Wo Cao?,這我也行啊, I Can I Up。但是讓我天天去找素材剪輯視頻啥的,那肯定干不來(lái),畢竟程序員是需要加班的,所以,這個(gè)粗糙的程序就誕生了,當(dāng)然我也沒(méi)怎么用,發(fā)了兩篇覺(jué)得不好玩。就沒(méi)再玩了。
后來(lái)通過(guò)種種途徑吧,才知道短視頻背后的產(chǎn)業(yè)相當(dāng)復(fù)雜,一個(gè)視頻能不能火基本不在于視頻本身。

這個(gè)軟件主要是基于錄屏功能來(lái)實(shí)現(xiàn)的,不過(guò)是一鍵式的罷了,當(dāng)然實(shí)現(xiàn)錄屏我們用了第三方的插件:AForge。項(xiàng)目需要的DLL如下圖:

實(shí)現(xiàn)功能
利用錄屏功能錄制語(yǔ)句的生成過(guò)程,并保存成視頻格式
開(kāi)發(fā)環(huán)境
開(kāi)發(fā)工具: Visual Studio 2013
.NET Framework版本:4.5
實(shí)現(xiàn)代碼
public class RecordingUtil
{
VideoFileWriter vfWriter = new VideoFileWriter();
ScreenCaptureStream scStream = null;
readonly Rectangle Rect;
public RecordingUtil(Rectangle rect, int interval = 40)
{
Rect = rect;
scStream = new ScreenCaptureStream(rect, interval);
scStream.NewFrame += (s, e1) =>
{
vfWriter.WriteVideoFrame(e1.Frame);
};
}
public void Start(string savePath, int Rate = 4000 * 1024)
{
vfWriter.Open(savePath, Rect.Width, Rect.Height, 25, VideoCodec.MPEG4, 4000 * 1024);
scStream.Start();
}
public void Stop()
{
if (scStream != null && scStream.IsRunning)
{
scStream.Stop();
}
if (vfWriter.IsOpen)
{
vfWriter.Close();
}
}
}
private void btnCreate_Click(object sender, EventArgs e)
{
checkNull();
btnCreate.Text = "正在生成";
btnCreate.Enabled = false;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "視頻文件|*.MP4";
if (sfd.ShowDialog() == DialogResult.OK)
{
Point point = new Point(this.Location.X + 5, this.Location.Y + 25);
Size size = new Size(splitContainer1.Panel1.Width % 2 == 1 ? splitContainer1.Panel1.Width - 1 : splitContainer1.Panel1.Width, splitContainer1.Panel1.Height % 2 == 1 ? splitContainer1.Panel1.Height - 1 : splitContainer1.Panel1.Height);
Rectangle rect = new Rectangle(point, size);
RecordingUtil Recording = new RecordingUtil(rect);
Recording.Start(sfd.FileName);
createText(txtWord.Text);
Recording.Stop();
}
btnCreate.Text = "生 成";
btnCreate.Enabled = true;
}
private void btnPreview_Click(object sender, EventArgs e)
{
checkNull();
btnPreview.Text = "正在預(yù)覽";
btnPreview.Enabled = false;
createText(txtWord.Text);
btnPreview.Text = "預(yù) 覽";
btnPreview.Enabled = true;
}
private void checkNull()
{
if (string.IsNullOrWhiteSpace(txtWord.Text))
{
toolTip1.Hide(txtWord);
toolTip1.Show("不可為空!", txtWord, 5, -60, 2000);
return;
}
}
private void createText(string text)
{
Graphics g = splitContainer1.Panel1.CreateGraphics();
g.Clear(splitContainer1.Panel1.BackColor);
Font font = new Font("華文行楷", 25);
// Brush whiteBrush = new SolidBrush(Color.FromArgb(0, 192, 0));
Brush whiteBrush = new SolidBrush(Color.Black);
int x = 0, y = 0;
string[] arr = txtWord.Text.Split('\n');
for (int i = 0; i < arr.Length; i++)
{
x = 40 * i + 15;
for (int j = 0; j < arr[i].Length; j++)
{
y = 40 * j + 15;
g.DrawString(arr[i][j].ToString(), font, whiteBrush, x, y);
Delay(300);
}
}
}
private void Delay(double mm)
{
DateTime now = DateTime.Now;
while (DateTime.Now.AddMilliseconds(-mm) <= now)
{
Application.DoEvents();
}
}
實(shí)現(xiàn)效果

以上就是C#實(shí)現(xiàn)文字視頻生成器的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#文字視頻生成器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C#實(shí)現(xiàn)封面圖片生成器的示例代碼
- C#設(shè)計(jì)模式之建造者模式生成器模式示例詳解
- Python實(shí)現(xiàn)C#代碼生成器應(yīng)用服務(wù)于Unity示例解析
- C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式
- C# Guid長(zhǎng)度雪花簡(jiǎn)單生成器的示例代碼
- c# 如何實(shí)現(xiàn)代碼生成器
- C#設(shè)計(jì)模式之Builder生成器模式解決帶老婆配置電腦問(wèn)題實(shí)例
- 詳解C#設(shè)計(jì)模式編程中生成器模式的使用
- 詳解C#中有趣的?SourceGenerator生成器
相關(guān)文章
C#中DataTable 轉(zhuǎn)換為 Json的方法匯總(三種方法)
JavaScript Object Notation (Json)是一種輕量級(jí)的數(shù)據(jù)交換格式,下面小編給大家介紹三種方法實(shí)現(xiàn)DataTable轉(zhuǎn)換成 Json 對(duì)象,感興趣的朋友一起看看吧2016-11-11
C# 中的 is 真的是越來(lái)越強(qiáng)大越來(lái)越語(yǔ)義化(推薦)
這篇文章主要介紹了C# 中的 is 真的是越來(lái)越強(qiáng)大越來(lái)越語(yǔ)義化,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
C# TabControl控件中TabPage選項(xiàng)卡切換時(shí)的觸發(fā)事件問(wèn)題
這篇文章主要介紹了C# TabControl控件中TabPage選項(xiàng)卡切換時(shí)的觸發(fā)事件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
C# 利用Selenium實(shí)現(xiàn)瀏覽器自動(dòng)化操作的示例代碼
這篇文章主要介紹了C# 利用Selenium實(shí)現(xiàn)瀏覽器自動(dòng)化操作,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-09-09
C# 基于udp廣播收集局域網(wǎng)類(lèi)所有設(shè)備信息
這篇文章主要介紹了C# 基于udp廣播收集局域網(wǎng)類(lèi)所有設(shè)備信息的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12
基于C#設(shè)計(jì)一個(gè)帶導(dǎo)航菜單的主界面
這篇文章主要為大家詳細(xì)介紹了如何基于C#設(shè)計(jì)一個(gè)帶導(dǎo)航菜單的主界面,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
在c#中使用servicestackredis操作redis的實(shí)例代碼
本篇文章主要介紹了在c#中使用servicestackredis操作redis的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

