[C#]中英文字幕合并的小程序

来源:互联网 发布:golang syscall mac 编辑:程序博客网 时间:2024/05/17 08:27

今天班里小组合作录了一个视频,我给它做了字幕的时间轴。为了让这个视频假装很高端的样子,我想再加上英文的字幕。中文的纯字幕文本先搞成一句一行,然后放到Google翻译上,复制英文保存在Eng.txt。

我用人人影视的字幕软件给中文先做好了时间轴,想要在加上英文的时候发现好像这个软件不那么容易实现。于是我就想自己写点代码自己实现这个需求。

代码其实是非常简单的,就是最简单的文件操作。做好时间轴的中文字幕保存在Chs.srt中,其格式为:

100:00:19,572 --> 00:00:21,069很高兴能够在这里见到大家200:00:21,645 --> 00:00:24,991首先恭喜大家通过初试来到今天小组讨论的阶段300:00:25,527 --> 00:00:28,532能够厮杀到这个环节说明大家都是非常优秀的

每一句字幕是4行,我只要在第三行插入对应的那行英文就可以了。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace BilingualSrt{    class Program    {        static void Main(string[] args)        {            StreamReader chsStream = new StreamReader("Chs.srt");            StreamReader engText = new StreamReader("Eng.txt");            StreamWriter outer = new StreamWriter("chs_eng.srt");            string cline = "";            int counter = 0;//用来给行数计数            while(true){                cline=chsStream.ReadLine();                if (cline == null)                    break;                Console.WriteLine(cline);                switch (counter)                {                    //第三行是字幕文本                    case 2:                        outer.WriteLine(cline);                                         string engLine = engText.ReadLine();                        outer.WriteLine(engLine);                        break;                    //第一、二行是序号和时间                    //第四行是空行                    default:                        outer.WriteLine(cline);                        break;                }                counter++;                counter%= 4;            }            chsStream.Close();            engText.Close();            outer.Close();        }    }}

最后完成的效果如下:

100:00:19,572 --> 00:00:21,069很高兴能够在这里见到大家Glad to be here to see you.200:00:21,645 --> 00:00:24,991首先恭喜大家通过初试来到今天小组讨论的阶段Congratulations everyone came through the first test phase of the panel discussion today300:00:25,527 --> 00:00:28,532能够厮杀到这个环节说明大家都是非常优秀的To be able to fight this link shows that we are very good
0 0
原创粉丝点击