Commit 35985076 authored by Sashmita Raghav's avatar Sashmita Raghav

Add srt parser to model

parent a0db1142
#include "subtitlemodel.hpp"
#include "core.h"
#include "project/projectmanager.h"
SubtitleModel::SubtitleModel(std::weak_ptr<DocUndoStack> undo_stack, QObject *parent)
: QAbstractListModel(parent)
{
}
std::shared_ptr<SubtitleModel> SubtitleModel::getModel()
{
return pCore->projectManager()->getSubtitleModel();
}
void SubtitleModel::parseSubtitle()
{
//QModelIndex index;
//int paramName = m_model->data(index, AssetParameterModel::NameRole).toInt();
//QString filePath(m_asset->get(paramName.toUtf8().constData()));
QString filePath= "path_to_srt_file.srt";
QString start,end,comment;
GenTime startPos, endPos;
QString timeLine;
int index =0, turn = 0,r =0;
/*
* turn = 0 -> Add subtitle number
* turn = 1 -> Add string to timeLine
* turn > 1 -> Add string to completeLine
*/
if (filePath.isEmpty())
return;
if (filePath.contains(".srt"))
qDebug()<< "srt File";
QFile srtFile(filePath);
if (!srtFile.exists() && !srtFile.open(QIODevice::ReadOnly)){
qDebug() << " File not found " << filePath;
return;
}
{ //parsing srt file
QTextStream stream(&srtFile);
QString line;
while (stream.readLineInto(&line)) {
line = line.simplified();
if (line.compare(""))
{
if(!turn)
{
index=atoi(line.toStdString().c_str());
turn++;
continue;
}
if (line.contains("-->"))
{
timeLine += line;
QStringList srtTime;
srtTime = timeLine.split(' ');
start = srtTime[0];
startPos= stringtoTime(start);
end = srtTime[2];
startPos= stringtoTime(start);
}
else
{
r++;
if (comment != "")
comment += " ";
if(r == 1) // only one line
comment += line;
else
comment = comment + "\r" +line;
}
turn++;
}
else
{
addSubtitle(index,startPos,endPos,comment);
//reinitialize
comment = timeLine = "";
turn = 0; r = 0;
}
}
}
}
GenTime SubtitleModel::stringtoTime(QString str)
{
QStringList total,secs;
int hours=0, mins=0, seconds=0, ms=0;
double total_sec=0;
total = str.split(':');
hours = atoi(total[0].toStdString().c_str());
mins = atoi(total[1].toStdString().c_str());
secs = total[2].split(',');
seconds = atoi(secs[0].toStdString().c_str());
ms = atoi(secs[1].toStdString().c_str());
total_sec= hours *3600 + mins *60 + seconds + ms * 0.001 ;
GenTime pos= *new GenTime(total_sec);
return pos;
}
\ No newline at end of file
#ifndef SUBTITLEMODEL_HPP
#define SUBTITLEMODEL_HPP
#include "definitions.h"
#include "gentime.h"
#include "undohelper.hpp"
#include <QAbstractListModel>
#include <QReadWriteLock>
#include <array>
#include <map>
#include <memory>
#include <mlt++/MltProperties.h>
class DocUndoStack;
class AssetParameterModel;
/* @brief This class is the model for a list of subtitles.
*/
class SubtitleModel:public QAbstractListModel
{
Q_OBJECT
public:
/* @brief Construct a subtitle list bound to the timeline */
SubtitleModel(std::weak_ptr<DocUndoStack> undo_stack, QObject *parent = nullptr);
/** @brief Function that parses through a subtitle file */
void parseSubtitle();
void addSubtitle(int ix,GenTime start,GenTime end, QString str);
GenTime stringtoTime(QString str);
private:
std::weak_ptr<DocUndoStack> m_undoStack;
std::map<GenTime, std::pair<QString, GenTime>> m_subtitleList;
//To get subtitle file from effects parameter:
//std::unique_ptr<Mlt::Properties> m_asset;
//std::shared_ptr<AssetParameterModel> m_model;
};
Q_DECLARE_METATYPE(SubtitleModel *)
#endif // SUBTITLEMODEL_HPP
\ No newline at end of file
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