C++实现转换Srt字幕文件为Txt纯文本(Srt转txt,方便打印)

Q:为什么会有这个需求呢?

A:crying为了学英语啊!!!正所谓少壮不努力,老大练听力!由于一直喜欢英式发音,所以到网上下载The IT Crowd的音频mp3到手机去听。找了SRT转LRC的软件,可以将SRT字幕转换成LRC歌词。但手机屏幕毕竟太小,看着相当不爽,于是乎就想打印出来看,但是发同无论是SRT还是LRC文件都有许多时间轴之类的对阅读里面文字毫无意义的东西,如果将这些东西也打印出来岂不是太浪费纸张了,本着自己动手,丰衣足食的态度,写了下来这个too simple的SRT class,其实真正有用的就是一个Convert函数;那为什么还要写成类呢,这…这不是流行OO(Object Oriented)嘛!

……其实可能是因为写习惯了!

好了,背景交代完毕,上代码,由于代码量小,就整在一个CPP中了:

#include 
#include 
#include 
#include 

/*
Class brief : Convert .Srt to plain printable text(.txt)!
author       : Andy Guo
website     : www.xycabin.com
Date          : 2013-07-20
Refer         : Srt file specification :https://en.wikipedia.org/wiki/SubRip#Specifications
*/
using namespace std;

class SRT
{
public:
    SRT();
    ~SRT();
    SRT(string OriginalSrtFile, string DstFile);
    bool Convert();
    //处理一行文本
    //bool ProcessLine(string LineStr, string& Outstr);
private:
    //SRT文件全路径
    string m_OriginalSrtFile;
    //目标Txt文件全路径
    string m_DstTxtFile;
};

SRT::SRT()
{
}

SRT::~SRT()
{
}

SRT::SRT(string OriginalSrtFile, string DstFile):m_OriginalSrtFile(OriginalSrtFile), m_DstTxtFile(DstFile)
{
}

bool SRT::Convert()
{
    if (m_OriginalSrtFile == "" || m_DstTxtFile == "")
    {
        cout << "Error: 源Srt或目标Txt文件为空!"  << endl;
        return false;
    }

    ifstream Orgfile( m_OriginalSrtFile.c_str() );
    if (!Orgfile)
    {
        cout  << "error: unable to open srt file: "<< m_OriginalSrtFile << endl;
        return false;
    }

    ofstream DstTxt( m_DstTxtFile.c_str() );
    if (!DstTxt)
    {
        cout  << "error: unable to open destination file: "<< m_DstTxtFile << endl;
        return false;
    }

    string tempstr;
    //while(Orgfile >> tempstr)
    char ch_temp[256];
    while( Orgfile.getline(ch_temp, 256 ) )
    {
        cout << "内容:" << ch_temp << endl;
        if( strcmp(ch_temp, "")==0 )
            continue;
        string OutString;
        //determine whether LineStr is a digit (F = subtitle frame counter (as ASCII digits))
        bool IsSeqNo = true;
        for( int i = 0; i < strlen(ch_temp); i++ )
        {
            //不=0是数字
            if (isdigit(ch_temp[i]) == 0)
                IsSeqNo = false;
        }
        if (IsSeqNo)
        {
            char ch_temp_next_line[256];
            Orgfile.getline(ch_temp_next_line, 256 );
            //00:00:45,264 --> 00:00:46,798
            tempstr = string(ch_temp_next_line);
            unsigned int findpos = tempstr.find("-->");
            if ( findpos != string::npos )
            {
                //cout << "时间轴:" << ch_temp_next_line << endl;
                //往前推5个应该是"," ,推后推6个应该是":"
                if (tempstr[findpos - 5] == ',' && tempstr[findpos + 6] == ':')
                {
                    //不写入txt
                }
                else
                {
                   // if ( ProcessLine(tempstr, OutString) )
                    {
                        cout << "A line has content ,so must write to dst file!" << endl;
                        DstTxt << tempstr;
                    }
                }
            }
            else
            {
                //说明前一行的数学不是序列号,需要写入目标txt文件
                //if ( ProcessLine(string(ch_temp), OutString) )
                {
                    DstTxt << string(ch_temp);
                }
            }
        }
        else
        {
            //不是帧序号,要写文件
            //如果最后是逗号不换行
            string WriteString = string(ch_temp);
            unsigned int unPos = WriteString.rfind(',');
            if (unPos != WriteString.size() -1 )
            {
                DstTxt << ch_temp << endl;;
                DstTxt << endl; //写一个空行
            }
            else
            {
                DstTxt << ch_temp;
            }
        }
    }
    Orgfile.close();
    DstTxt.close();
    return true;
}

//bool SRT::ProcessLine(string LineStr, string& Outstr)
//{
//    if (LineStr == "")
//        return false;
//
//}

int main()
{
    SRT c_Srt("The IT Crowd - 1x01 - Yesterday's Jam.DVD.en.srt", "The IT Crowd - 1x01 - Yesterday's Jam.DVD.en.txt");
    c_Srt.Convert();
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注