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

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

實(shí)現(xiàn)功能
利用錄屏功能錄制語句的生成過程,并保存成視頻格式
開發(fā)環(huán)境
開發(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生成器模式解決帶老婆配置電腦問題實(shí)例
- 詳解C#設(shè)計(jì)模式編程中生成器模式的使用
- 詳解C#中有趣的?SourceGenerator生成器
相關(guān)文章
C#簡(jiǎn)單實(shí)現(xiàn)顯示中文格式星期幾的方法
這篇文章主要介紹了C#簡(jiǎn)單實(shí)現(xiàn)顯示中文格式星期幾的方法,涉及C#常見的日期與時(shí)間以及字符串轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2016-07-07
C#?將數(shù)據(jù)庫SqlServer數(shù)據(jù)綁定到類中的過程詳解
本文講述的是讀取數(shù)據(jù)庫中數(shù)據(jù)的常用做法,即將數(shù)據(jù)庫中的數(shù)據(jù)綁定到創(chuàng)建的類中,再將類綁定到DataGridView的數(shù)據(jù)源中的做法,對(duì)C#將SqlServer數(shù)據(jù)綁定到類中感興趣的朋友一起看看吧2022-06-06
C#獲取ListView鼠標(biāo)下的Item實(shí)例
下面小編就為大家?guī)硪黄狢#獲取ListView鼠標(biāo)下的Item實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
C#校驗(yàn)時(shí)間格式的場(chǎng)景分析
本文通過場(chǎng)景分析給大家講解C#里如何簡(jiǎn)單的校驗(yàn)時(shí)間格式,本次的場(chǎng)景屬于比較常見的收單API,對(duì)第三方的訂單進(jìn)行簽名驗(yàn)證,然后持久化到數(shù)據(jù)庫,需要的朋友跟隨小編一起看看吧2022-08-08

