[C.C++] C#实现文字视频生成器的示例代码

2044 0
黑夜隐士 2022-11-5 07:47:16 | 显示全部楼层 |阅读模式
目录

    前言实现功能开发环境实现代码实现效果


前言

简单的描述下写这个软件的背景吧。之前短视频平台很火的时候,相信很多人都想进去分一杯羹,俺当然也不能免俗,但是人丑家穷又没才艺,咋办呢?看到别人有只发文字啥的一些视频加点背景音乐也能看,想着,Wo Cao?,这我也行啊, I Can I Up。但是让我天天去找素材剪辑视频啥的,那肯定干不来,毕竟程序员是需要加班的,所以,这个粗糙的程序就诞生了,当然我也没怎么用,发了两篇觉得不好玩。就没再玩了。
后来通过种种途径吧,才知道短视频背后的产业相当复杂,一个视频能不能火基本不在于视频本身。


这个软件主要是基于录屏功能来实现的,不过是一键式的罢了,当然实现录屏我们用了第三方的插件:AForge。项目需要的DLL如下图:



实现功能

利用录屏功能录制语句的生成过程,并保存成视频格式

开发环境

开发工具: Visual Studio 2013
.NET Framework版本:4.5

实现代码
  1.   public class RecordingUtil
  2.     {
  3.         VideoFileWriter vfWriter = new VideoFileWriter();
  4.         ScreenCaptureStream scStream = null;
  5.         readonly Rectangle Rect;
  6.         public RecordingUtil(Rectangle rect, int interval = 40)
  7.         {
  8.             Rect = rect;
  9.             scStream = new ScreenCaptureStream(rect, interval);
  10.             scStream.NewFrame += (s, e1) =>
  11.             {
  12.                 vfWriter.WriteVideoFrame(e1.Frame);
  13.             };
  14.         }
  15.         public void Start(string savePath, int Rate = 4000 * 1024)
  16.         {
  17.             vfWriter.Open(savePath, Rect.Width, Rect.Height, 25, VideoCodec.MPEG4, 4000 * 1024);
  18.             scStream.Start();
  19.         }
  20.         public void Stop()
  21.         {
  22.             if (scStream != null && scStream.IsRunning)
  23.             {
  24.                 scStream.Stop();
  25.             }
  26.             if (vfWriter.IsOpen)
  27.             {
  28.                 vfWriter.Close();
  29.             }
  30.         }
  31.     }
复制代码
  1. private void btnCreate_Click(object sender, EventArgs e)
  2.         {
  3.             checkNull();
  4.             btnCreate.Text = "正在生成";
  5.             btnCreate.Enabled = false;
  6.             SaveFileDialog sfd = new SaveFileDialog();
  7.             sfd.Filter = "视频文件|*.MP4";
  8.             if (sfd.ShowDialog() == DialogResult.OK)
  9.             {
  10.                 Point point = new Point(this.Location.X + 5, this.Location.Y + 25);
  11.                 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);
  12.                 Rectangle rect = new Rectangle(point, size);
  13.                 RecordingUtil Recording = new RecordingUtil(rect);
  14.                 Recording.Start(sfd.FileName);
  15.                 createText(txtWord.Text);
  16.                 Recording.Stop();
  17.             }
  18.             btnCreate.Text = "生 成";
  19.             btnCreate.Enabled = true;
  20.         }
  21.         private void btnPreview_Click(object sender, EventArgs e)
  22.         {
  23.             checkNull();
  24.             btnPreview.Text = "正在预览";
  25.             btnPreview.Enabled = false;
  26.             createText(txtWord.Text);
  27.             btnPreview.Text = "预 览";
  28.             btnPreview.Enabled = true;
  29.         }
  30.         private void checkNull()
  31.         {
  32.             if (string.IsNullOrWhiteSpace(txtWord.Text))
  33.             {
  34.                 toolTip1.Hide(txtWord);
  35.                 toolTip1.Show("不可为空!", txtWord, 5, -60, 2000);
  36.                 return;
  37.             }
  38.         }
  39.         private void createText(string text)
  40.         {
  41.             Graphics g = splitContainer1.Panel1.CreateGraphics();
  42.             g.Clear(splitContainer1.Panel1.BackColor);
  43.             Font font = new Font("华文行楷", 25);
  44.             // Brush whiteBrush = new SolidBrush(Color.FromArgb(0, 192, 0));
  45.             Brush whiteBrush = new SolidBrush(Color.Black);
  46.             int x = 0, y = 0;
  47.             string[] arr = txtWord.Text.Split('\n');
  48.             for (int i = 0; i < arr.Length; i++)
  49.             {
  50.                 x = 40 * i + 15;
  51.                 for (int j = 0; j < arr[i].Length; j++)
  52.                 {
  53.                     y = 40 * j + 15;
  54.                     g.DrawString(arr[i][j].ToString(), font, whiteBrush, x, y);
  55.                     Delay(300);
  56.                 }
  57.             }
  58.         }
  59.         private void Delay(double mm)
  60.         {
  61.             DateTime now = DateTime.Now;
  62.             while (DateTime.Now.AddMilliseconds(-mm) <= now)
  63.             {
  64.                 Application.DoEvents();
  65.             }
  66.         }
复制代码
实现效果



以上就是C#实现文字视频生成器的示例代码的详细内容,更多关于C#文字视频生成器的资料请关注中国红客联盟其它相关文章!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行