• [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
    

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

  • 相关阅读:
    MySQL对于数据库应该如何如何配置安全问题了
    对于改善 MySQL 数据装载操作有效率的方法是怎样
    MySQL与SQL比较有那些区别呢
    Centos6.5和Centos7 php环境搭建如何实现呢
    php单例模式是怎么实现的呢
    PHP编写的图片验证码类文件分享方法
    PHP中header函数的用法及其注意重点是什么呢
    java正则表达式四种常用的处理方式是怎么样呢《匹配、分割、代替、获取》
    PHP弱类型安全问题的写法和步骤
    vs2010 使用IIS EXPRESS出错.
  • 原文地址:https://www.cnblogs.com/xiaff/p/4856506.html
Copyright © 2020-2023  润新知