2006-02-26 23:45:34 +01:00
|
|
|
// Copyright (c) 2006, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2006-02-26 23:45:34 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file subtitle_format_srt.cpp
|
|
|
|
/// @brief Reading/writing SubRip format subtitles (.SRT)
|
|
|
|
/// @ingroup subtitle_io
|
|
|
|
///
|
2006-02-26 23:45:34 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <wx/regex.h>
|
|
|
|
#endif
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
2006-02-26 23:45:34 +01:00
|
|
|
#include "subtitle_format_srt.h"
|
|
|
|
#include "text_file_reader.h"
|
2006-02-27 22:57:10 +01:00
|
|
|
#include "text_file_writer.h"
|
2006-02-26 23:45:34 +01:00
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
DEFINE_SIMPLE_EXCEPTION(SRTParseError, SubtitleFormatParseError, "subtitle_io/parse/srt")
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
SRTSubtitleFormat::SRTSubtitleFormat()
|
|
|
|
: SubtitleFormat("SubRip")
|
|
|
|
{
|
2006-12-26 19:26:13 +01:00
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
wxArrayString SRTSubtitleFormat::GetReadWildcards() const {
|
2006-12-26 19:26:13 +01:00
|
|
|
wxArrayString formats;
|
2011-09-28 21:43:11 +02:00
|
|
|
formats.Add("srt");
|
2006-12-26 19:26:13 +01:00
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
wxArrayString SRTSubtitleFormat::GetWriteWildcards() const {
|
2006-12-26 19:26:13 +01:00
|
|
|
return GetReadWildcards();
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
void SRTSubtitleFormat::ReadFile(wxString const& filename, wxString const& encoding) {
|
2006-02-26 23:45:34 +01:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
TextFileReader file(filename,encoding);
|
|
|
|
|
2007-01-06 06:04:57 +01:00
|
|
|
LoadDefault(false);
|
2006-02-26 23:45:34 +01:00
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
// See parsing algorithm at <http://devel.aegisub.org/wiki/SubtitleFormats/SRT>
|
2007-01-15 22:35:34 +01:00
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
// "hh:mm:ss,fff --> hh:mm:ss,fff" (e.g. "00:00:04,070 --> 00:00:10,04")
|
|
|
|
/// @todo: move the full parsing of SRT timestamps here, instead of having it in AssTime
|
2011-09-28 21:43:11 +02:00
|
|
|
wxRegEx timestamp_regex("^([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}) --> ([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})");
|
2010-06-20 21:07:43 +02:00
|
|
|
if (!timestamp_regex.IsValid())
|
|
|
|
throw agi::InternalError("Parsing SRT: Failed compiling regex", 0);
|
2007-01-15 22:35:34 +01:00
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
int state = 1;
|
|
|
|
int line_num = 0;
|
|
|
|
int linebreak_debt = 0;
|
|
|
|
AssDialogue *line = 0;
|
|
|
|
while (file.HasMoreLines()) {
|
|
|
|
wxString text_line = file.ReadLineFromFile();
|
|
|
|
line_num++;
|
|
|
|
text_line.Trim(true).Trim(false);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
// start of file, no subtitles found yet
|
|
|
|
if (text_line.IsEmpty())
|
|
|
|
// ignore blank lines
|
|
|
|
break;
|
|
|
|
if (text_line.IsNumber()) {
|
|
|
|
// found the line number, throw it away and hope for timestamps
|
|
|
|
state = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (timestamp_regex.Matches(text_line)) {
|
|
|
|
goto found_timestamps;
|
|
|
|
}
|
|
|
|
char cvtbuf[16]; sprintf(cvtbuf, "%d", line_num);
|
|
|
|
throw SRTParseError(std::string("Parsing SRT: Expected subtitle index at line ") + cvtbuf, 0);
|
2007-01-15 22:35:34 +01:00
|
|
|
}
|
2010-06-20 21:07:43 +02:00
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
// want timestamps
|
|
|
|
if (timestamp_regex.Matches(text_line) == false) {
|
|
|
|
// bad format
|
|
|
|
char cvtbuf[16]; sprintf(cvtbuf, "%d", line_num);
|
|
|
|
throw SRTParseError(std::string("Parsing SRT: Expected timestamp pair at line ") + cvtbuf, 0);
|
|
|
|
}
|
|
|
|
found_timestamps:
|
|
|
|
if (line != 0) {
|
|
|
|
// finalise active line
|
|
|
|
line->ParseSRTTags();
|
|
|
|
line = 0;
|
|
|
|
}
|
|
|
|
// create new subtitle
|
|
|
|
line = new AssDialogue();
|
2011-09-28 21:43:11 +02:00
|
|
|
line->group = "[Events]";
|
|
|
|
line->Style = "Default";
|
2007-01-15 22:35:34 +01:00
|
|
|
line->Comment = false;
|
2010-06-20 21:07:43 +02:00
|
|
|
// this parsing should best be moved out of AssTime
|
|
|
|
line->Start.ParseSRT(timestamp_regex.GetMatch(text_line, 1));
|
|
|
|
line->End.ParseSRT(timestamp_regex.GetMatch(text_line, 2));
|
|
|
|
// store pointer to subtitle, we'll continue working on it
|
2007-01-15 22:35:34 +01:00
|
|
|
Line->push_back(line);
|
2010-06-20 21:07:43 +02:00
|
|
|
// next we're reading the text
|
|
|
|
state = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
// reading first line of subtitle text
|
|
|
|
if (text_line.IsEmpty()) {
|
|
|
|
// that's not very interesting... blank subtitle?
|
|
|
|
state = 5;
|
2010-06-20 21:42:13 +02:00
|
|
|
// no previous line that needs a line break after
|
|
|
|
linebreak_debt = 0;
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
line->Text.Append(text_line);
|
|
|
|
state = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
// reading following line of subtitle text
|
|
|
|
if (text_line.IsEmpty()) {
|
|
|
|
// blank line, next may begin a new subtitle
|
|
|
|
state = 5;
|
2010-06-20 21:42:13 +02:00
|
|
|
// previous line needs a line break after
|
2010-06-20 21:07:43 +02:00
|
|
|
linebreak_debt = 1;
|
|
|
|
break;
|
|
|
|
}
|
2011-09-28 21:43:11 +02:00
|
|
|
line->Text.Append("\\N").Append(text_line);
|
2010-06-20 21:07:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
// blank line in subtitle text
|
|
|
|
linebreak_debt++;
|
|
|
|
if (text_line.IsEmpty()) {
|
|
|
|
// multiple blank lines in a row, just add a line break...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (text_line.IsNumber()) {
|
|
|
|
// must be a subtitle index!
|
|
|
|
// go for timestamps next
|
|
|
|
state = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (timestamp_regex.Matches(text_line)) {
|
|
|
|
goto found_timestamps;
|
|
|
|
}
|
|
|
|
// assume it's a continuation of the subtitle text
|
|
|
|
// resolve our line break debt and append the line text
|
|
|
|
while (linebreak_debt-- > 0)
|
2011-09-28 21:43:11 +02:00
|
|
|
line->Text.Append("\\N");
|
2010-06-20 21:07:43 +02:00
|
|
|
line->Text.Append(text_line);
|
|
|
|
state = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
char cvtbuf[16]; sprintf(cvtbuf, "%d", state);
|
|
|
|
throw agi::InternalError(std::string("Parsing SRT: Reached unexpected state ") + cvtbuf, 0);
|
2007-01-15 22:35:34 +01:00
|
|
|
}
|
2006-02-26 23:45:34 +01:00
|
|
|
}
|
|
|
|
}
|
2007-01-06 06:04:57 +01:00
|
|
|
|
2010-06-20 21:07:43 +02:00
|
|
|
if (state == 1 || state == 2) {
|
2011-09-28 21:44:53 +02:00
|
|
|
throw SRTParseError("Parsing SRT: Incomplete file", 0);
|
2010-06-20 21:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (line) {
|
|
|
|
// an unfinalised line
|
|
|
|
line->ParseSRTTags();
|
2007-01-06 06:04:57 +01:00
|
|
|
}
|
2006-02-26 23:45:34 +01:00
|
|
|
}
|
2006-02-27 22:57:10 +01:00
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
void SRTSubtitleFormat::WriteFile(wxString const& filename, wxString const& encoding) {
|
|
|
|
TextFileWriter file(filename,encoding);
|
2006-02-27 22:57:10 +01:00
|
|
|
|
|
|
|
// Convert to SRT
|
|
|
|
CreateCopy();
|
2007-06-19 05:34:53 +02:00
|
|
|
SortLines();
|
2008-07-18 17:39:34 +02:00
|
|
|
StripComments();
|
2009-05-15 03:34:00 +02:00
|
|
|
// Tags must be converted in two passes
|
|
|
|
// First ASS style overrides are converted to SRT but linebreaks are kept
|
2011-09-28 21:43:11 +02:00
|
|
|
ConvertTags(2,"\\N",false);
|
2009-05-15 03:34:00 +02:00
|
|
|
// Then we can recombine overlaps, this requires ASS style linebreaks
|
2008-07-18 17:39:34 +02:00
|
|
|
RecombineOverlaps();
|
|
|
|
MergeIdentical();
|
2009-05-15 03:34:00 +02:00
|
|
|
// And finally convert linebreaks
|
2011-09-28 21:43:11 +02:00
|
|
|
ConvertTags(0,"\r\n",false);
|
2009-05-15 03:34:00 +02:00
|
|
|
// Otherwise unclosed overrides might affect lines they shouldn't, see bug #809 for example
|
2006-02-27 22:57:10 +01:00
|
|
|
|
|
|
|
// Write lines
|
|
|
|
int i=1;
|
2011-09-28 21:44:53 +02:00
|
|
|
for (std::list<AssEntry*>::iterator cur=Line->begin();cur!=Line->end();cur++) {
|
|
|
|
if (AssDialogue *current = dynamic_cast<AssDialogue*>(*cur)) {
|
|
|
|
file.WriteLineToFile(wxString::Format("%i", i++));
|
2011-09-28 21:43:11 +02:00
|
|
|
file.WriteLineToFile(current->Start.GetSRTFormated() + " --> " + current->End.GetSRTFormated());
|
2006-02-27 22:57:10 +01:00
|
|
|
file.WriteLineToFile(current->Text);
|
2011-09-28 21:43:11 +02:00
|
|
|
file.WriteLineToFile("");
|
2006-02-27 22:57:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-19 05:34:53 +02:00
|
|
|
ClearCopy();
|
2006-02-27 22:57:10 +01:00
|
|
|
}
|