-
-
Notifications
You must be signed in to change notification settings - Fork 106
Implement ID3v2 chapter support #2560
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
Draft
Borewit
wants to merge
1
commit into
master
Choose a base branch
from
implement-id3v2-chapter-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−4
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type { IGetToken } from 'strtok3'; | ||
| import * as Token from 'token-types'; | ||
|
|
||
| export interface IChapterInfo { | ||
| startTime: number; | ||
| endTime: number; | ||
| startOffset: number; | ||
| endOffset: number; | ||
| } | ||
|
|
||
| /** | ||
| * Data portion of `CHAP` sub frame | ||
| */ | ||
| export const ChapterInfo: IGetToken<IChapterInfo> = { | ||
| len: 16, | ||
|
|
||
| get: (buf: Uint8Array, off: number): IChapterInfo => { | ||
| return { | ||
| startTime: Token.UINT32_BE.get(buf, off), | ||
| endTime: Token.UINT32_BE.get(buf, off + 4), | ||
| startOffset: Token.UINT32_BE.get(buf, off + 8), | ||
| endOffset: Token.UINT32_BE.get(buf, off + 12) | ||
| }; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ import type { TagType } from '../common/GenericTagTypes.js'; | |
| import { FrameParser, Id3v2ContentError, type ITextTag } from './FrameParser.js'; | ||
| import { ExtendedHeader, ID3v2Header, type ID3v2MajorVersion, type IID3v2header } from './ID3v2Token.js'; | ||
|
|
||
| import type { ITag, IOptions, AnyTagValue } from '../type.js'; | ||
| import type { ITag, IOptions, AnyTagValue, IChapter } from '../type.js'; | ||
| import type { INativeMetadataCollector, IWarningCollector } from '../common/MetadataCollector.js'; | ||
|
|
||
| import { getFrameHeaderLength, readFrameHeader } from './FrameHeader.js'; | ||
|
|
@@ -101,7 +101,11 @@ export class ID3v2Parser { | |
|
|
||
| this.headerType = (`ID3v2.${id3Header.version.major}`) as TagType; | ||
|
|
||
| return id3Header.flags.isExtendedHeader ? this.parseExtendedHeader() : this.parseId3Data(id3Header.size); | ||
| await (id3Header.flags.isExtendedHeader ? this.parseExtendedHeader() : this.parseId3Data(id3Header.size)); | ||
|
|
||
| // Post process | ||
| const chapters = ID3v2Parser.mapId3v2Chapters(this.metadata.native[this.headerType], this.metadata.format.sampleRate); | ||
| this.metadata.setFormat('chapters', chapters); | ||
| } | ||
|
|
||
| public async parseExtendedHeader(): Promise<void> { | ||
|
|
@@ -168,6 +172,68 @@ export class ID3v2Parser { | |
| return tags; | ||
| } | ||
|
|
||
| /** | ||
| * Convert parsed ID3v2 chapter frames (CHAP / CTOC) to generic `format.chapters`. | ||
| * | ||
| * This function expects the `native` tags already to contain parsed `CHAP` and `CTOC` frame values, | ||
| * as produced by `FrameParser.readData`. | ||
| */ | ||
| private static mapId3v2Chapters( | ||
| id3Tags: ITag[], | ||
| sampleRate?: number | ||
| ): IChapter[] | undefined { | ||
|
|
||
| const chapFrames = id3Tags.filter(t => t.id === 'CHAP') as any[] | undefined; | ||
| if (!chapFrames?.length) return; | ||
|
|
||
| const tocFrames = id3Tags.filter(t => t.id === 'CTOC') as any[] | undefined; | ||
| const topLevelToc = tocFrames?.find(t => t.value.flags?.topLevel); | ||
|
|
||
| const chapterById = new Map<string, any>(); | ||
| for (const chap of chapFrames) { | ||
| chapterById.set(chap.value.label, chap.value); | ||
| } | ||
|
|
||
| const orderedIds: string[] | undefined = | ||
| topLevelToc?.value.childElementIds; | ||
|
|
||
| const chapters: IChapter[] = []; | ||
|
|
||
| const source = orderedIds ?? [...chapterById.keys()]; | ||
|
|
||
| for (const id of source) { | ||
| const chap = chapterById.get(id); | ||
| if (!chap) continue; | ||
|
|
||
| const frames = chap.frames; | ||
| const title = frames.get('TIT2'); | ||
| if (!title) continue; // title is required | ||
| const image = frames.get('APIC'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Support WXXX (User defined URL link frame) in chapter metadata |
||
|
|
||
| const start = chap.info.startTime; | ||
| const timeScale = 1000; | ||
|
|
||
| const sampleOffset = sampleRate | ||
| ? Math.round((start / timeScale) * sampleRate) | ||
| : 0; | ||
|
|
||
| chapters.push({ | ||
| id, | ||
| title, | ||
| start, | ||
| timeScale, | ||
| sampleOffset, | ||
| image | ||
| }); | ||
| } | ||
|
|
||
| // If no ordered CTOC, sort by time | ||
| if (!orderedIds) { | ||
| chapters.sort((a, b) => a.start - b.start); | ||
| } | ||
|
|
||
| return chapters.length ? chapters : undefined; | ||
| } | ||
| } | ||
|
|
||
| function makeUnexpectedMajorVersionError(majorVer: number): never { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
ChapterInfo. get(uint8Array, fzero + 1)instead of offset. At this point offset is still 0, but ChapterInfo starts after the null-terminated label.