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

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

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

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

1
00:00:19,572 --> 00:00:21,069
很高兴能够在这里见到大家

2
00:00:21,645 --> 00:00:24,991
首先恭喜大家通过初试来到今天小组讨论的阶段

3
00: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();
        }
    }
}

最后完成的效果如下:

1
00:00:19,572 --> 00:00:21,069
很高兴能够在这里见到大家
Glad to be here to see you.

2
00:00:21,645 --> 00:00:24,991
首先恭喜大家通过初试来到今天小组讨论的阶段
Congratulations everyone came through the first test phase of the panel discussion today

3
00:00:25,527 --> 00:00:28,532
能够厮杀到这个环节说明大家都是非常优秀的
To be able to fight this link shows that we are very good

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-04-05 20:10  夏方方  阅读(731)  评论(0编辑  收藏  举报