diff --git a/Makefile b/Makefile index 1f0425b..e490f97 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,9 @@ LIBWEBM := libwebm.a WEBMOBJS := mkvparser.o mkvreader.o mkvmuxer.o mkvmuxerutil.o mkvwriter.o OBJECTS1 := sample.o OBJECTS2 := sample_muxer.o +OBJECTS3 := dumpvtt.o vttreader.o webvttparser.o INCLUDES := -I. -EXES := samplemuxer sample +EXES := samplemuxer sample dumpvtt all: $(EXES) @@ -15,6 +16,9 @@ sample: sample.o $(LIBWEBM) samplemuxer: sample_muxer.o $(LIBWEBM) $(CXX) $^ -o $@ +dumpvtt: $(OBJECTS3) + $(CXX) $^ -o $@ + libwebm.a: $(WEBMOBJS) $(AR) rcs $@ $^ @@ -22,4 +26,4 @@ libwebm.a: $(WEBMOBJS) $(CXX) -c $(CXXFLAGS) $(INCLUDES) $< -o $@ clean: - $(RM) -f $(OBJECTS1) $(OBJECTS2) $(WEBMOBJS) $(LIBWEBM) $(EXES) Makefile.bak + $(RM) -f $(OBJECTS1) $(OBJECTS2) $(OBJECTS3) $(WEBMOBJS) $(LIBWEBM) $(EXES) Makefile.bak diff --git a/dumpvtt.cc b/dumpvtt.cc new file mode 100644 index 0000000..ca54f01 --- /dev/null +++ b/dumpvtt.cc @@ -0,0 +1,98 @@ +// Copyright (c) 2012 The WebM project authors. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. + +#include +#include +#include "./vttreader.h" +#include "./webvttparser.h" + +int main(int argc, const char* argv[]) { + if (argc != 2) { + fprintf(stdout, "usage: dumpvtt \n"); + return EXIT_SUCCESS; + } + + libwebvtt::VttReader reader; + const char* const filename = argv[1]; + + if (int e = reader.Open(filename)) { + (void)e; + fprintf(stderr, "open failed\n"); + return EXIT_FAILURE; + } + + libwebvtt::Parser parser(&reader); + + if (int e = parser.Init()) { + (void)e; + fprintf(stderr, "parser init failed\n"); + return EXIT_FAILURE; + } + + for (libwebvtt::Cue cue;;) { + const int e = parser.Parse(&cue); + + if (e < 0) { // error + fprintf(stderr, "error parsing cue\n"); + return EXIT_FAILURE; + } + + if (e > 0) // EOF + return EXIT_SUCCESS; + + fprintf(stdout, "cue identifier: \"%s\"\n", cue.identifier.c_str()); + + const libwebvtt::Time& st = cue.start_time; + fprintf(stdout, "cue start time: \"HH=%i MM=%i SS=%i SSS=%i\"\n", + st.hours, + st.minutes, + st.seconds, + st.milliseconds); + + const libwebvtt::Time& sp = cue.stop_time; + fprintf(stdout, "cue stop time: \"HH=%i MM=%i SS=%i SSS=%i\"\n", + sp.hours, + sp.minutes, + sp.seconds, + sp.milliseconds); + + { + typedef libwebvtt::Cue::settings_t::const_iterator iter_t; + iter_t i = cue.settings.begin(); + const iter_t j = cue.settings.end(); + + if (i == j) { + fprintf(stdout, "cue setting: \n"); + } else { + while (i != j) { + const libwebvtt::Setting& setting = *i++; + fprintf(stdout, "cue setting: name=%s value=%s\n", + setting.name.c_str(), + setting.value.c_str()); + } + } + } + + { + typedef libwebvtt::Cue::payload_t::const_iterator iter_t; + iter_t i = cue.payload.begin(); + const iter_t j = cue.payload.end(); + + int idx = 1; + while (i != j) { + const std::string& payload = *i++; + const char* const payload_str = payload.c_str(); + fprintf(stdout, "cue payload[%i]: \"%s\"\n", idx, payload_str); + ++idx; + } + } + + fprintf(stdout, "\n"); + fflush(stdout); + } +} diff --git a/vttreader.cc b/vttreader.cc new file mode 100644 index 0000000..e1b1476 --- /dev/null +++ b/vttreader.cc @@ -0,0 +1,57 @@ +// Copyright (c) 2012 The WebM project authors. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. + +#include "./vttreader.h" // NOLINT + +namespace libwebvtt { + +VttReader::VttReader() : file_(NULL) { +} + +VttReader::~VttReader() { + Close(); +} + +int VttReader::Open(const char* filename) { + if (filename == NULL || file_ != NULL) + return -1; + + file_ = fopen(filename, "rb"); + if (file_ == NULL) + return -1; + + return 0; // success +} + +void VttReader::Close() { + if (file_) { + fclose(file_); + file_ = NULL; + } +} + +int VttReader::GetChar(char* c) { + if (c == NULL || file_ == NULL) + return -1; + + const int result = fgetc(file_); + if (result != EOF) { + *c = static_cast(result); + return 0; // success + } + + if (ferror(file_)) + return -1; // error + + if (feof(file_)) + return 1; // EOF + + return -1; // weird +} + +} // namespace libwebvtt diff --git a/vttreader.h b/vttreader.h new file mode 100644 index 0000000..6d6c7e6 --- /dev/null +++ b/vttreader.h @@ -0,0 +1,44 @@ +// Copyright (c) 2012 The WebM project authors. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. + +#ifndef VTTREADER_H_ // NOLINT +#define VTTREADER_H_ + +#include +#include "./webvttparser.h" + +namespace libwebvtt { + +class VttReader : public libwebvtt::Reader { + public: + VttReader(); + virtual ~VttReader(); + + // Open the file identified by |filename| in read-only mode, as a + // binary stream of bytes. Returns 0 on success, negative if error. + int Open(const char* filename); + + // Closes the file stream. Note that the stream is automatically + // closed when the VttReader object is destroyed. + void Close(); + + // Reads the next character in the file stream, as per the semantics + // of Reader::GetChar. Returns negative if error, 0 on success, and + // positive if end-of-stream has been reached. + virtual int GetChar(char* c); + + private: + FILE* file_; + + VttReader(const VttReader&); + VttReader& operator=(const VttReader&); +}; + +} // namespace libwebvtt + +#endif // VTTREADER_H_ // NOLINT