From a573b0897b9fdb37d09e10adca49b098b25c412a Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 19 May 2010 00:44:37 +0000 Subject: [PATCH] Add "Open Subtitles From Video" menu option. Closes #253. Originally committed to SVN as r4306. --- aegisub/src/frame_main.cpp | 1 + aegisub/src/frame_main.h | 2 ++ aegisub/src/frame_main_events.cpp | 8 ++++++ aegisub/src/mkv_wrap.cpp | 22 +++++++++++++++ aegisub/src/mkv_wrap.h | 1 + aegisub/src/video_context.cpp | 47 +++++++++++++++++-------------- aegisub/src/video_context.h | 5 ++++ 7 files changed, 65 insertions(+), 21 deletions(-) diff --git a/aegisub/src/frame_main.cpp b/aegisub/src/frame_main.cpp index aa22ada6c..ecbfb26e3 100644 --- a/aegisub/src/frame_main.cpp +++ b/aegisub/src/frame_main.cpp @@ -357,6 +357,7 @@ void FrameMain::InitMenu() { AppendBitmapMenuItem(fileMenu,Menu_File_New_Subtitles, MakeHotkeyText(_("&New Subtitles"), _T("New Subtitles")), _("New subtitles"),GETIMAGE(new_toolbutton_16)); AppendBitmapMenuItem(fileMenu,Menu_File_Open_Subtitles, MakeHotkeyText(_("&Open Subtitles..."), _T("Open Subtitles")), _("Opens a subtitles file"),GETIMAGE(open_toolbutton_16)); AppendBitmapMenuItem(fileMenu,Menu_File_Open_Subtitles_Charset, _("&Open Subtitles with Charset..."), _("Opens a subtitles file with a specific charset"),GETIMAGE(open_with_toolbutton_16)); + fileMenu->Append(Menu_File_Open_Subtitles_From_Video, _("Open Subtitles from &Video"), _("Opens the subtitles from the current video file")); AppendBitmapMenuItem(fileMenu,Menu_File_Save_Subtitles, MakeHotkeyText(_("&Save Subtitles"), _T("Save Subtitles")), _("Saves subtitles"),GETIMAGE(save_toolbutton_16)); AppendBitmapMenuItem(fileMenu,Menu_File_Save_Subtitles_As, _("Save Subtitles as..."), _("Saves subtitles with another name"), GETIMAGE(save_as_toolbutton_16)); AppendBitmapMenuItem(fileMenu,Menu_File_Export_Subtitles, _("Export Subtitles..."), _("Saves a copy of subtitles with processing applied to it."), GETIMAGE(export_menu_16)); diff --git a/aegisub/src/frame_main.h b/aegisub/src/frame_main.h index 9d85422c4..23f4899cc 100644 --- a/aegisub/src/frame_main.h +++ b/aegisub/src/frame_main.h @@ -214,6 +214,7 @@ private: void OnNewSubtitles (wxCommandEvent &event); void OnOpenSubtitles (wxCommandEvent &event); void OnOpenSubtitlesCharset (wxCommandEvent &event); + void OnOpenSubtitlesVideo (wxCommandEvent &event); void OnSaveSubtitles (wxCommandEvent &event); void OnSaveSubtitlesAs (wxCommandEvent &event); void OnSaveSubtitlesCharset (wxCommandEvent &event); @@ -412,6 +413,7 @@ enum { /// DOCME Menu_File_Open_Subtitles_Charset, + Menu_File_Open_Subtitles_From_Video, /// DOCME Menu_File_New_Subtitles, diff --git a/aegisub/src/frame_main_events.cpp b/aegisub/src/frame_main_events.cpp index 11e3accce..f0073b8fd 100644 --- a/aegisub/src/frame_main_events.cpp +++ b/aegisub/src/frame_main_events.cpp @@ -124,6 +124,7 @@ BEGIN_EVENT_TABLE(FrameMain, wxFrame) EVT_MENU(Menu_File_Close_Video, FrameMain::OnCloseVideo) EVT_MENU(Menu_File_Open_Subtitles, FrameMain::OnOpenSubtitles) EVT_MENU(Menu_File_Open_Subtitles_Charset, FrameMain::OnOpenSubtitlesCharset) + EVT_MENU(Menu_File_Open_Subtitles_From_Video, FrameMain::OnOpenSubtitlesVideo) EVT_MENU(Menu_File_New_Subtitles, FrameMain::OnNewSubtitles) EVT_MENU(Menu_File_Save_Subtitles, FrameMain::OnSaveSubtitles) EVT_MENU(Menu_File_Save_Subtitles_As, FrameMain::OnSaveSubtitlesAs) @@ -296,6 +297,8 @@ void FrameMain::OnMenuOpen (wxMenuEvent &event) { if (curMenu == fileMenu) { // Rebuild recent RebuildRecentList(_T("Recent sub"),RecentSubs,Menu_File_Recent); + + MenuBar->Enable(Menu_File_Open_Subtitles_From_Video,VideoContext::Get()->HasSubtitles()); } // View menu @@ -811,6 +814,11 @@ void FrameMain::OnOpenSubtitlesCharset(wxCommandEvent& WXUNUSED(event)) { } } +/// @brief Open subtitles from the currently open video file +void FrameMain::OnOpenSubtitlesVideo(wxCommandEvent&) { + LoadSubtitles(VideoContext::Get()->videoName); +} + /// @brief Save subtitles as diff --git a/aegisub/src/mkv_wrap.cpp b/aegisub/src/mkv_wrap.cpp index e06b57a4a..11d37e06b 100644 --- a/aegisub/src/mkv_wrap.cpp +++ b/aegisub/src/mkv_wrap.cpp @@ -508,9 +508,31 @@ void MatroskaWrapper::GetSubtitles(AssFile *target) { } } +bool MatroskaWrapper::HasSubtitles(wxString const& filename) { + char err[2048]; + MkvStdIO input(filename); + if (!input.fp) return false; + MatroskaFile* file = mkv_Open(&input, err, sizeof(err)); + if (!file) return false; + // Find tracks + int tracks = mkv_GetNumTracks(file); + for (int track = 0; track < tracks; track++) { + TrackInfo *trackInfo = mkv_GetTrackInfo(file, track); + if (trackInfo->Type == 0x11) { + wxString CodecID = wxString(trackInfo->CodecID, *wxConvCurrent); + if (CodecID == _T("S_TEXT/SSA") || CodecID == _T("S_TEXT/ASS") || CodecID == _T("S_TEXT/UTF8")) { + mkv_Close(file); + return true; + } + } + } + + mkv_Close(file); + return false; +} ////////////////////////////// LOTS OF HAALI C CODE DOWN HERE /////////////////////////////////////// diff --git a/aegisub/src/mkv_wrap.h b/aegisub/src/mkv_wrap.h index 512043e5e..062ceecc6 100644 --- a/aegisub/src/mkv_wrap.h +++ b/aegisub/src/mkv_wrap.h @@ -174,6 +174,7 @@ public: unsigned int GetFrameCount() { return timecodes.size(); } wxArrayInt GetKeyFrames(); void GetSubtitles(AssFile *target); + static bool HasSubtitles(wxString const& filename); /// DOCME diff --git a/aegisub/src/video_context.cpp b/aegisub/src/video_context.cpp index c1f26c76b..a9c18f246 100644 --- a/aegisub/src/video_context.cpp +++ b/aegisub/src/video_context.cpp @@ -62,6 +62,7 @@ #include "ass_style.h" #include "ass_time.h" #include "audio_display.h" +#include "mkv_wrap.h" #include "options.h" #include "standard_paths.h" #include "subs_edit_box.h" @@ -95,27 +96,26 @@ VideoContext *VideoContext::instance = NULL; /// @brief Constructor /// -VideoContext::VideoContext() { - // Set GL context - glContext = NULL; - ownGlContext = false; - - // Set options - audio = NULL; - provider = NULL; - subsProvider = NULL; - curLine = NULL; - loaded = false; - keyFramesLoaded = false; - overKeyFramesLoaded = false; - frame_n = 0; - length = 0; - fps = 0; - arType = 0; - arValue = 1.0; - isPlaying = false; - nextFrame = -1; - keepAudioSync = true; +VideoContext::VideoContext() +: glContext(NULL) +, ownGlContext(false) +, audio(NULL) +, provider(NULL) +, subsProvider(NULL) +, curLine(NULL) +, loaded(false) +, keyFramesLoaded(false) +, overKeyFramesLoaded(false) +, frame_n(0) +, length(0) +, fps(0) +, arType(0) +, arValue(1.) +, isPlaying(false) +, nextFrame(-1) +, keepAudioSync(true) +, hasSubtitles(false) +{ } /// @brief Destructor @@ -274,6 +274,11 @@ void VideoContext::SetVideo(const wxString &filename) { wxString warning = provider->GetWarning().c_str(); if (!warning.IsEmpty()) wxMessageBox(warning,_T("Warning"),wxICON_WARNING | wxOK); + hasSubtitles = false; + if (filename.Right(4).Lower() == L".mkv") { + hasSubtitles = MatroskaWrapper::HasSubtitles(filename); + } + UpdateDisplays(true); } diff --git a/aegisub/src/video_context.h b/aegisub/src/video_context.h index 6491a0899..54fbd8e91 100644 --- a/aegisub/src/video_context.h +++ b/aegisub/src/video_context.h @@ -174,6 +174,8 @@ private: /// DOCME int arType; + bool hasSubtitles; + void OnPlayTimer(wxTimerEvent &event); public: @@ -215,6 +217,9 @@ public: /// @return bool IsPlaying() { return isPlaying; } + /// @brief Does the video file loaded have muxed subtitles that we can load? + bool HasSubtitles() {return hasSubtitles; } + /// @brief DOCME /// @param sync /// @return