Skip to content

[Feature] External Chapters #1257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/mpc-hc/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12388,6 +12388,64 @@ void CMainFrame::OpenFile(OpenFileData* pOFD)
SetPlaybackMode(PM_FILE);
}

void CMainFrame::SetupExternalChapters()
{
// .XCHP (eXternal CHaPters) file format:
// - UTF-8 text file.
// - Located in same folder as the audio/video file, and has same base filename.
// - It will override chapter metadata that is embedded in the file.
// - Each line defines a chapter: timecode, optionally followed by a space and chapter title.
// - Timecode must be in this format: HH:MM:SS.ddd

CPlaylistItem* pli = m_wndPlaylistBar.GetCur();
if (!pli) {
return;
}

CString fn = m_wndPlaylistBar.GetCurFileName(true);
if (PathUtils::IsURL(fn)) {
return;
}

CPath cp(fn);
cp.RenameExtension(_T(".xchp"));
if (!cp.FileExists()) {
return;
}
fn = cp.m_strPath;

CWebTextFile f(CTextFile::UTF8);
f.SetFallbackEncoding(CTextFile::ANSI);

CString str;
if (!f.Open(fn) || !f.ReadString(str)) {
return;
}

f.Seek(0, CFile::SeekPosition::begin);

while (f.ReadString(str)) {
REFERENCE_TIME rt = 0;
CString name = "";

if (str.GetLength() > 11) {
int lHour = 0;
int lMinute = 0;
int lSecond = 0;
int lMillisec = 0;
if (_stscanf_s(str.Left(12), _T("%02d:%02d:%02d.%03d"), &lHour, &lMinute, &lSecond, &lMillisec) == 4) {
rt = ((((lHour * 60) + lMinute) * 60 + lSecond) * MILLISECONDS + lMillisec) * (UNITS / MILLISECONDS);
if (str.GetLength() > 12) {
name = str.Mid(12);
name.Trim();
}
m_pCB->ChapAppend(rt, name);
}
}
}
m_pCB->ChapSort();
}

void CMainFrame::SetupChapters()
{
// Release the old chapter bag and create a new one.
Expand All @@ -12396,6 +12454,12 @@ void CMainFrame::SetupChapters()
m_pCB.Release();
m_pCB = DEBUG_NEW CDSMChapterBag(nullptr, nullptr);

SetupExternalChapters();
if (m_pCB->ChapGetCount() > 0) {
UpdateSeekbarChapterBag();
return;
}

CInterfaceList<IBaseFilter> pBFs;
BeginEnumFilters(m_pGB, pEF, pBF);
pBFs.AddTail(pBF);
Expand Down
2 changes: 2 additions & 0 deletions src/mpc-hc/MainFrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1260,4 +1260,6 @@ class CMainFrame : public CFrameWnd, public CDropClient
public:
afx_msg void OnSettingChange(UINT uFlags, LPCTSTR lpszSection);
afx_msg void OnMouseHWheel(UINT nFlags, short zDelta, CPoint pt);
private:
void SetupExternalChapters();
};