Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
VSFilter: Improve the external subtitles naming.
Browse files Browse the repository at this point in the history
ISR: Reuse subtitle name on reload.
  • Loading branch information
kasper93 committed Nov 17, 2013
1 parent 4ec32d7 commit 82bbcaf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/Subtitles/RTS.cpp
Expand Up @@ -2885,5 +2885,5 @@ STDMETHODIMP CRenderedTextSubtitle::Reload()
if (!FileExists(m_path)) {
return E_FAIL;
}
return !m_path.IsEmpty() && Open(m_path, DEFAULT_CHARSET) ? S_OK : E_FAIL;
return !m_path.IsEmpty() && Open(m_path, DEFAULT_CHARSET, m_name) ? S_OK : E_FAIL;
}
85 changes: 77 additions & 8 deletions src/Subtitles/STS.cpp
Expand Up @@ -22,6 +22,7 @@
#include "stdafx.h"
#include "STS.h"
#include <atlbase.h>
#include "atl/atlrx.h"

#include "RealTextParser.h"
#include <fstream>
Expand Down Expand Up @@ -2532,7 +2533,7 @@ void CSimpleTextSubtitle::CreateSegments()
*/
}

bool CSimpleTextSubtitle::Open(CString fn, int CharSet, CString name)
bool CSimpleTextSubtitle::Open(CString fn, int CharSet, CString name, CString videoName)
{
Empty();

Expand All @@ -2541,14 +2542,82 @@ bool CSimpleTextSubtitle::Open(CString fn, int CharSet, CString name)
return false;
}

fn.Replace('\\', '/');
if (name.IsEmpty()) {
name = fn.Left(fn.ReverseFind('.'));
name = name.Mid(name.ReverseFind('/') + 1);
int len = name.GetLength();
int pos = name.ReverseFind('.') + 1;
if ((len - pos) > 1) {
name = name.Mid(pos);
CString lang;
bool bHearingImpaired = false;

// The filename of the subtitle file
int iExtStart = fn.ReverseFind('.');
if (iExtStart < 0) {
iExtStart = fn.GetLength();
}
CString subName = fn.Left(iExtStart).Mid(fn.ReverseFind('\\') + 1);

if (!videoName.IsEmpty()) {
// The filename of the video file
iExtStart = videoName.ReverseFind('.');
if (iExtStart < 0) {
iExtStart = videoName.GetLength();
}
CString videoExt = videoName.Mid(iExtStart + 1).MakeLower();
videoName = videoName.Left(iExtStart).Mid(videoName.ReverseFind('\\') + 1);

CString subNameNoCase = CString(subName).MakeLower();
CString videoNameNoCase = CString(videoName).MakeLower();

// Check if the subtitle filename starts with the video filename
// so that we can try to find a language info right after it
if (subNameNoCase.Find(videoNameNoCase) == 0) {
int iVideoNameEnd = videoName.GetLength();
// Get ride of the video extension if it's in the subtitle filename
if (subNameNoCase.Find(videoExt, iVideoNameEnd) == iVideoNameEnd + 1) {
iVideoNameEnd += 1 + videoExt.GetLength();
}
subName = subName.Mid(iVideoNameEnd);

CAtlRegExp<CAtlRECharTraits> re;
CAtlREMatchContext<CAtlRECharTraits> mc;
if (REPARSE_ERROR_OK == re.Parse(_T("^[.\\-_ ]+{[^.\\-_ ]+}([.\\-_ ]+{[^.\\-_ ]+})?"), FALSE) && re.Match(subName, &mc)) {
LPCTSTR s, e;
mc.GetMatch(0, &s, &e);
lang = ISO639XToLanguage(CStringA(s, int(e - s)), true);
if (!lang.IsEmpty()) {
mc.GetMatch(1, &s, &e);
bHearingImpaired = (CString(s, int(e - s)).CompareNoCase(_T("hi")) == 0);
}
}
}
}

// If we couldn't find any info yet, we try to find the language at the end of the filename
if (lang.IsEmpty()) {
CAtlRegExp<CAtlRECharTraits> re;
CAtlREMatchContext<CAtlRECharTraits> mc;
if (REPARSE_ERROR_OK == re.Parse(_T(".*?[.\\-_ ]+{[^.\\-_ ]+}([.\\-_ ]+{[^.\\-_ ]+})?$"), FALSE) && re.Match(subName, &mc)) {
LPCTSTR s, e;
mc.GetMatch(0, &s, &e);
lang = ISO639XToLanguage(CStringA(s, int(e - s)), true);

mc.GetMatch(1, &s, &e);
CStringA str(s, int(e - s));

if (!lang.IsEmpty() && str.CompareNoCase("hi") == 0) {
bHearingImpaired = true;
} else {
lang = ISO639XToLanguage(str, true);
}
}
}

name = fn.Mid(fn.ReverseFind('\\') + 1);
if (name.GetLength() > 100) { // Cut some part of the filename if it's too long
name.Format(_T("%s...%s"), name.Left(50).TrimRight(_T(".-_ ")), name.Right(50).TrimLeft(_T(".-_ ")));
}
if (!lang.IsEmpty()) {
name.AppendFormat(_T(" [%s]"), lang);
if (bHearingImpaired) {
name.Append(_T(" [hearing impaired]"));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Subtitles/STS.h
Expand Up @@ -161,7 +161,7 @@ class CSimpleTextSubtitle : public CAtlArray<STSEntry>

void Append(CSimpleTextSubtitle& sts, int timeoff = -1);

bool Open(CString fn, int CharSet, CString name = _T(""));
bool Open(CString fn, int CharSet, CString name = _T(""), CString videoName = _T(""));
bool Open(CTextFile* f, int CharSet, CString name);
bool Open(BYTE* data, int len, int CharSet, CString name);
bool SaveAs(CString fn, exttype et, double fps = -1, int delay = 0, CTextFile::enc = CTextFile::DEFAULT_ENCODING);
Expand Down
79 changes: 2 additions & 77 deletions src/mpc-hc/MainFrm.cpp
Expand Up @@ -30,7 +30,6 @@
#include <afxglobals.h>
#include <afxpriv.h>
#include <atlconv.h>
#include <atlrx.h>
#include <atlsync.h>

#include "mpc-hc_config.h"
Expand Down Expand Up @@ -13762,82 +13761,8 @@ bool CMainFrame::LoadSubtitle(CString fn, ISubStream** actualStream /*= nullptr*

CAutoPtr<CRenderedTextSubtitle> pRTS(DEBUG_NEW CRenderedTextSubtitle(&m_csSubLock, &s.subtitlesDefStyle, s.fUseDefaultSubtitlesStyle));

// The filename of the video file
CString videoName = m_wndPlaylistBar.GetCurFileName();
int iExtStart = videoName.ReverseFind('.');
if (iExtStart < 0) {
iExtStart = videoName.GetLength();
}
CString videoExt = videoName.Mid(iExtStart + 1).MakeLower();
videoName = videoName.Left(iExtStart).Mid(videoName.ReverseFind('\\') + 1);

// The filename of the subtitle file
iExtStart = fn.ReverseFind('.');
if (iExtStart < 0) {
iExtStart = fn.GetLength();
}
CString subName = fn.Left(iExtStart).Mid(fn.ReverseFind('\\') + 1);
CString subExt = fn.Mid(iExtStart + 1);

CString subNameNoCase = CString(subName).MakeLower();
CString videoNameNoCase = CString(videoName).MakeLower();

CString name, lang;
bool bHearingImpaired = false;
// Check if the subtitle filename starts with the video filename
// so that we can try to find a language info right after it
if (GetPlaybackMode() == PM_FILE && subNameNoCase.Find(videoNameNoCase) == 0) {
int iVideoNameEnd = videoName.GetLength();
// Get ride of the video extension if it's in the subtitle filename
if (subNameNoCase.Find(videoExt, iVideoNameEnd) == iVideoNameEnd + 1) {
iVideoNameEnd += 1 + videoExt.GetLength();
}
subName = subName.Mid(iVideoNameEnd);

CAtlRegExp<CAtlRECharTraits> re;
CAtlREMatchContext<CAtlRECharTraits> mc;
if (REPARSE_ERROR_OK == re.Parse(_T("^[.\\-_ ]+{[^.\\-_ ]+}([.\\-_ ]+{[^.\\-_ ]+})?"), FALSE) && re.Match(subName, &mc)) {
LPCTSTR s, e;
mc.GetMatch(0, &s, &e);
lang = ISO639XToLanguage(CStringA(s, int(e - s)), true);
if (!lang.IsEmpty()) {
mc.GetMatch(1, &s, &e);
bHearingImpaired = (CString(s, int(e - s)).CompareNoCase(_T("hi")) == 0);
}
}
}

// If we couldn't find any info yet, we try to find the language at the end of the filename
if (lang.IsEmpty()) {
CAtlRegExp<CAtlRECharTraits> re;
CAtlREMatchContext<CAtlRECharTraits> mc;
if (REPARSE_ERROR_OK == re.Parse(_T(".*?[.\\-_ ]+{[^.\\-_ ]+}([.\\-_ ]+{[^.\\-_ ]+})?$"), FALSE) && re.Match(subName, &mc)) {
LPCTSTR s, e;
mc.GetMatch(0, &s, &e);
lang = ISO639XToLanguage(CStringA(s, int(e - s)), true);

mc.GetMatch(1, &s, &e);
CStringA str(s, int(e - s));

if (!lang.IsEmpty() && str.CompareNoCase("hi") == 0) {
bHearingImpaired = true;
} else {
lang = ISO639XToLanguage(str, true);
}
}
}

name = fn.Mid(fn.ReverseFind('\\') + 1);
if (name.GetLength() > 100) { // Cut some part of the filename if it's too long
name.Format(_T("%s...%s"), name.Left(50).TrimRight(_T(".-_ ")), name.Right(50).TrimLeft(_T(".-_ ")));
}
if (!lang.IsEmpty()) {
name.AppendFormat(_T(" [%s]"), lang);
if (bHearingImpaired) {
name.Append(_T(" [hearing impaired]"));
}
}
if (pRTS && pRTS->Open(fn, DEFAULT_CHARSET, name) && pRTS->GetStreamCount() > 0) {
CString videoName = GetPlaybackMode() == PM_FILE ? m_wndPlaylistBar.GetCurFileName() : _T("");
if (pRTS && pRTS->Open(fn, DEFAULT_CHARSET, _T(""), videoName) && pRTS->GetStreamCount() > 0) {
pSubStream = pRTS.Detach();
}
}
Expand Down

0 comments on commit 82bbcaf

Please sign in to comment.