Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Multimedia
Kdenlive
Commits
e6843558
Commit
e6843558
authored
Jun 14, 2020
by
Sashmita Raghav
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add SSA Parser
parent
35985076
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
124 additions
and
60 deletions
+124
-60
src/bin/model/subtitlemodel.cpp
src/bin/model/subtitlemodel.cpp
+123
-59
src/bin/model/subtitlemodel.hpp
src/bin/model/subtitlemodel.hpp
+1
-1
No files found.
src/bin/model/subtitlemodel.cpp
View file @
e6843558
...
...
@@ -17,81 +17,143 @@ 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 filename(m_asset->get(paramName.toUtf8().constData()));
QString
filePath
=
"path_to_subtitle_file.srt"
;
QString
start
,
end
,
comment
;
GenTime
startPos
,
endPos
;
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 completeLine
*/
if
(
filePath
.
isEmpty
())
return
;
if
(
filePath
.
contains
(
".srt"
))
if
(
filePath
.
contains
(
".srt"
))
{
QFile
srtFile
(
filePath
);
if
(
!
srtFile
.
exists
()
&&
!
srtFile
.
open
(
QIODevice
::
ReadOnly
))
{
qDebug
()
<<
" File not found "
<<
filePath
;
return
;
}
qDebug
()
<<
"srt File"
;
QFile
srtFile
(
filePath
);
if
(
!
srtFile
.
exists
()
&&
!
srtFile
.
open
(
QIODevice
::
ReadOnly
)){
qDebug
()
<<
" File not found "
<<
filePath
;
return
;
}
{
//parsing srt file
//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
);
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
)
comment
+=
line
;
else
comment
=
comment
+
"
\r
"
+
line
;
}
turn
++
;
}
else
{
addSubtitle
(
startPos
,
endPos
,
comment
);
//reinitialize
comment
=
timeLine
=
""
;
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
)
...
...
@@ -102,11 +164,13 @@ GenTime SubtitleModel::stringtoTime(QString str)
total
=
str
.
split
(
':'
);
hours
=
atoi
(
total
[
0
].
toStdString
().
c_str
());
mins
=
atoi
(
total
[
1
].
toStdString
().
c_str
());
secs
=
total
[
2
].
split
(
','
);
if
(
total
[
2
].
contains
(
'.'
))
secs
=
total
[
2
].
split
(
'.'
);
//ssa file
else
secs
=
total
[
2
].
split
(
','
);
//srt file
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
);
GenTime
pos
=
GenTime
(
total_sec
);
return
pos
;
}
\ No newline at end of file
src/bin/model/subtitlemodel.hpp
View file @
e6843558
...
...
@@ -29,7 +29,7 @@ public:
/** @brief Function that parses through a subtitle file */
void
parseSubtitle
();
void
addSubtitle
(
int
ix
,
GenTime
start
,
GenTime
end
,
QString
str
);
void
addSubtitle
(
GenTime
start
,
GenTime
end
,
QString
str
);
GenTime
stringtoTime
(
QString
str
);
private:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment