From 8e762c67e06066ba2c9ac9ac61a3c6412824e3da Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Sat, 8 Sep 2012 21:37:42 +0300 Subject: [PATCH] Allow dumping subtitles as .sub file --- src/core/subtitles.cpp | 47 +++++++++++++++++++++++++++++++++++ src/platform/wxwidgets/mainwindow.cpp | 5 ++++ 2 files changed, 52 insertions(+) diff --git a/src/core/subtitles.cpp b/src/core/subtitles.cpp index 3fc67336..29a4f678 100644 --- a/src/core/subtitles.cpp +++ b/src/core/subtitles.cpp @@ -4,6 +4,7 @@ #include "core/window.hpp" #include "library/string.hpp" #include "fonts/wrapper.hpp" +#include moviefile_subtiming::moviefile_subtiming(uint64_t _frame) { @@ -58,6 +59,21 @@ uint64_t moviefile_subtiming::get_length() const { return length; } namespace { + std::string s_subescape(std::string x) + { + std::string y; + for(size_t i = 0; i < x.length(); i++) { + char ch = x[i]; + if(ch == '\n') + y += "|"; + else if(ch == '|') + y += "⎢"; + else + y.append(1, ch); + } + return y; + } + struct render_object_subtitle : public render_object { render_object_subtitle(int32_t _x, int32_t _y, const std::string& _text) throw() @@ -103,6 +119,37 @@ namespace << s_escape(i->second) << std::endl; } }); + + function_ptr_command save_s("save-subtitle", "Save subtitles in .sub format", + "Syntax: save-subtitle \nSaves subtitles in .sub format to \n", + [](arg_filename args) throw(std::bad_alloc, std::runtime_error) { + if(our_movie.subtitles.empty()) + return; + auto i = our_movie.subtitles.begin(); + uint64_t lastframe = i->first.get_frame() + i->first.get_length(); + std::ofstream y(std::string(args).c_str()); + if(!y) + throw std::runtime_error("Can't open output file"); + std::string lasttxt = ""; + uint64_t since = 0; + for(uint64_t i = 1; i < lastframe; i++) { + moviefile_subtiming posmarker(i); + auto j = our_movie.subtitles.upper_bound(posmarker); + if(j == our_movie.subtitles.end() || !j->first.inrange(i)) + continue; + if(lasttxt != j->second) { + if(lasttxt != "") + y << "{" << since << "}{" << i - 1 << "}" << s_subescape(lasttxt) + << std::endl; + since = i; + lasttxt = j->second; + } + } + if(lasttxt != "") + y << "{" << since << "}{" << lastframe - 1 << "}" << s_subescape(lasttxt) + << std::endl; + messages << "Saved subtitles to " << std::string(args) << std::endl; + }); } std::string s_escape(std::string x) diff --git a/src/platform/wxwidgets/mainwindow.cpp b/src/platform/wxwidgets/mainwindow.cpp index 84d1b259..82c08ca9 100644 --- a/src/platform/wxwidgets/mainwindow.cpp +++ b/src/platform/wxwidgets/mainwindow.cpp @@ -52,6 +52,7 @@ enum wxID_AUDIODEV_LAST = wxID_AUDIODEV_FIRST + 255, wxID_SAVE_STATE, wxID_SAVE_MOVIE, + wxID_SAVE_SUBTITLES, wxID_LOAD_STATE, wxID_LOAD_STATE_RO, wxID_LOAD_STATE_RW, @@ -719,6 +720,7 @@ wxwin_mainwindow::wxwin_mainwindow() menu_entry(wxID_SAVE_STATE, wxT("State...")); menu_entry(wxID_SAVE_MOVIE, wxT("Movie...")); menu_entry(wxID_SAVE_SCREENSHOT, wxT("Screenshot...")); + menu_entry(wxID_SAVE_SUBTITLES, wxT("Subtitles...")); menu_entry(wxID_CANCEL_SAVES, wxT("Cancel pending saves")); menu_end_sub(); menu_separator(); @@ -916,6 +918,9 @@ void wxwin_mainwindow::handle_menu_click_cancelable(wxCommandEvent& e) case wxID_SAVE_MOVIE: platform::queue("save-movie " + pick_file(this, "Save Movie", movie_path())); return; + case wxID_SAVE_SUBTITLES: + platform::queue("save-subtitle " + pick_file(this, "Save Subtitle (.sub)", movie_path())); + return; case wxID_SAVE_STATE: platform::queue("save-state " + pick_file(this, "Save State", movie_path())); return; -- 2.11.4.GIT