Commit e6843558 authored by Sashmita Raghav's avatar Sashmita Raghav

Add SSA Parser

parent 35985076
...@@ -17,81 +17,143 @@ void SubtitleModel::parseSubtitle() ...@@ -17,81 +17,143 @@ void SubtitleModel::parseSubtitle()
{ {
//QModelIndex index; //QModelIndex index;
//int paramName = m_model->data(index, AssetParameterModel::NameRole).toInt(); //int paramName = m_model->data(index, AssetParameterModel::NameRole).toInt();
//QString filename(m_asset->get(paramName.toUtf8().constData()));
//QString filePath(m_asset->get(paramName.toUtf8().constData())); QString filePath= "path_to_subtitle_file.srt";
QString filePath= "path_to_srt_file.srt";
QString start,end,comment; QString start,end,comment;
GenTime startPos, endPos; GenTime startPos, endPos;
QString timeLine; QString timeLine;
int index =0, turn = 0,r =0; int index = 0, turn = 0,r = 0;
/* /*
* turn = 0 -> Add subtitle number * turn = 0 -> Parse next subtitle line [srt] (or) Parse next section [ssa]
* turn = 1 -> Add string to timeLine * turn = 1 -> Add string to timeLine
* turn > 1 -> Add string to completeLine * turn > 1 -> Add string to completeLine
*/ */
if (filePath.isEmpty()) if (filePath.isEmpty())
return; return;
if (filePath.contains(".srt"))
qDebug()<< "srt File";
QFile srtFile(filePath);
if (!srtFile.exists() && !srtFile.open(QIODevice::ReadOnly)){ if (filePath.contains(".srt")) {
QFile srtFile(filePath);
if (!srtFile.exists() && !srtFile.open(QIODevice::ReadOnly)) {
qDebug() << " File not found " << filePath; qDebug() << " File not found " << filePath;
return; return;
} }
{ //parsing srt file qDebug()<< "srt File";
//parsing srt file
QTextStream stream(&srtFile); QTextStream stream(&srtFile);
QString line; QString line;
while (stream.readLineInto(&line)) { while (stream.readLineInto(&line)) {
line = line.simplified(); line = line.simplified();
if (line.compare("")) {
if (!turn) {
if (line.compare(""))
{
if(!turn)
{
index=atoi(line.toStdString().c_str()); index=atoi(line.toStdString().c_str());
turn++; turn++;
continue; continue;
} }
if (line.contains("-->")) {
if (line.contains("-->"))
{
timeLine += line; timeLine += line;
QStringList srtTime; QStringList srtTime;
srtTime = timeLine.split(' '); srtTime = timeLine.split(' ');
start = srtTime[0]; start = srtTime[0];
startPos= stringtoTime(start); startPos= stringtoTime(start);
end = srtTime[2]; end = srtTime[2];
startPos= stringtoTime(start); startPos= stringtoTime(start);
} else {
}
else
{
r++; r++;
if (comment != "") if (comment != "")
comment += " "; comment += " ";
if(r == 1) // only one line if (r == 1)
comment += line; comment += line;
else else
comment = comment + "\r" +line; comment = comment + "\r" +line;
} }
turn++; turn++;
} } else {
else addSubtitle(startPos,endPos,comment);
{
addSubtitle(index,startPos,endPos,comment);
//reinitialize //reinitialize
comment = timeLine = ""; comment = timeLine = "";
turn = 0; r = 0; turn = 0; r = 0;
} }
} }
} else if (filePath.contains(".ass")) {
qDebug()<< "ass File";
QString startTime,endTime,text;
QString EventFormat, section;
turn = 0;
int maxSplit =0;
QFile assFile(filePath);
if (!assFile.exists() || !assFile.open(QIODevice::ReadOnly)) {
qDebug() << " Failed attempt on opening " << filePath;
return;
}
QTextStream stream(&assFile);
QString line;
qDebug() << " correct ass file " << filePath;
while (stream.readLineInto(&line)) {
line = line.simplified();
if (line.compare("")) {
if (!turn) {
qDebug() << " turn = 0 " << line;
//check if it is script info, event,or v4+ style
if (line.replace(" ","").contains("ScriptInfo")) {
qDebug()<< "Script Info";
section = "Script Info";
turn++;
//qDebug()<< "turn" << turn;
continue;
} else if (line.contains("Styles")) {
qDebug()<< "V4 Styles";
section = "V4 Styles";
turn++;
//qDebug()<< "turn" << turn;
continue;
} else {
turn++;
section = "Events";
//qDebug()<< "turn" << turn;
continue;
}
}
qDebug() << "\n turn != 0 " << turn<< line;
if (section.contains("Events")) {
//if it is event
QStringList format;
if (line.contains("Format:")) {
EventFormat += line;
format = (EventFormat.split(": ")[1].replace(" ","")).split(',');
//qDebug() << format << format.count();
maxSplit = format.count();
//TIME
startTime = format[1];
endTime = format[2];
// Text
text = format[9];
//qDebug()<< startTime << endTime << text;
} else {
QString EventDialogue;
QStringList dialogue;
start ="";end ="";comment="";
EventDialogue += line;
dialogue = EventDialogue.split(": ")[1].split(',');
QString remainingStr = "," + EventDialogue.split(": ")[1].section(',', maxSplit);
//qDebug()<< dialogue;
//TIME
start = dialogue[1];
startPos= stringtoTime(start);
end = dialogue[2];
endPos= stringtoTime(end);
// Text
comment = dialogue[9]+ remainingStr;
//qDebug()<<"Start: "<< start << "End: "<<end << comment;
addSubtitle(startPos,endPos,comment);
}
}
turn++;
} else {
turn = 0;
text = startTime = endTime = "";
}
}
} }
} }
GenTime SubtitleModel::stringtoTime(QString str) GenTime SubtitleModel::stringtoTime(QString str)
...@@ -102,11 +164,13 @@ GenTime SubtitleModel::stringtoTime(QString str) ...@@ -102,11 +164,13 @@ GenTime SubtitleModel::stringtoTime(QString str)
total = str.split(':'); total = str.split(':');
hours = atoi(total[0].toStdString().c_str()); hours = atoi(total[0].toStdString().c_str());
mins = atoi(total[1].toStdString().c_str()); mins = atoi(total[1].toStdString().c_str());
if(total[2].contains('.'))
secs = total[2].split(','); secs = total[2].split('.'); //ssa file
else
secs = total[2].split(','); //srt file
seconds = atoi(secs[0].toStdString().c_str()); seconds = atoi(secs[0].toStdString().c_str());
ms = atoi(secs[1].toStdString().c_str()); ms = atoi(secs[1].toStdString().c_str());
total_sec= hours *3600 + mins *60 + seconds + ms * 0.001 ; total_sec= hours *3600 + mins *60 + seconds + ms * 0.001 ;
GenTime pos= *new GenTime(total_sec); GenTime pos= GenTime(total_sec);
return pos; return pos;
} }
\ No newline at end of file
...@@ -29,7 +29,7 @@ public: ...@@ -29,7 +29,7 @@ public:
/** @brief Function that parses through a subtitle file */ /** @brief Function that parses through a subtitle file */
void parseSubtitle(); void parseSubtitle();
void addSubtitle(int ix,GenTime start,GenTime end, QString str); void addSubtitle(GenTime start,GenTime end, QString str);
GenTime stringtoTime(QString str); GenTime stringtoTime(QString str);
private: private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment