Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Better chapters from description parsing

Chocobozzze24716c571eb
diff --git a/packages/core-utils/src/string/chapters.ts b/packages/core-utils/src/string/chapters.ts
index 2f8105f3da..fc8588e6af 100644
--- a/packages/core-utils/src/string/chapters.ts
+++ b/packages/core-utils/src/string/chapters.ts
@@ -6,23 +6,21 @@ export function parseChapters (text: string, maxTitleLength: number) {
if (!text) return []
const lines = text.split(/\r?\n|\r|\n/g)
- let foundChapters = false
const chapters: { timecode: number, title: string }[] = []
+ let lastTimecode: number
for (const line of lines) {
const matched = line.match(timecodeRegex)
- if (!matched) {
- // Stop chapters parsing
- if (foundChapters) break
-
- continue
- }
-
- foundChapters = true
+ if (!matched) continue
const timecodeText = matched[1]
const timecode = timeToInt(timecodeText)
+
+ if (lastTimecode !== undefined && timecode <= lastTimecode) continue
+
+ lastTimecode = timecode
+
const title = line.replace(matched[0], '')
chapters.push({ timecode, title: title.slice(0, maxTitleLength) })
diff --git a/packages/tests/src/server-helpers/core-utils.ts b/packages/tests/src/server-helpers/core-utils.ts
index c3bec176dc..514caeb760 100644
--- a/packages/tests/src/server-helpers/core-utils.ts
+++ b/packages/tests/src/server-helpers/core-utils.ts
@@ -14,7 +14,6 @@ import { VideoResolution } from '@peertube/peertube-models'
import { objectConverter, parseBytes, parseDurationToMs, parseSemVersion } from '@peertube/peertube-server/core/helpers/core-utils.js'
describe('Parse Bytes', function () {
-
it('Should pass on valid value', function () {
// just return it
expect(parseBytes(-1024)).to.equal(-1024)
@@ -55,7 +54,6 @@ describe('Parse Bytes', function () {
})
describe('Parse duration', function () {
-
it('Should pass when given valid value', function () {
expect(parseDurationToMs(35)).to.equal(35)
expect(parseDurationToMs(-35)).to.equal(-35)
@@ -71,7 +69,6 @@ describe('Parse duration', function () {
})
describe('Time to int', function () {
-
it('Should correctly parse time to int', function () {
expect(timeToInt(undefined)).to.equal(0)
expect(timeToInt('')).to.equal(0)
@@ -93,7 +90,6 @@ describe('Time to int', function () {
})
describe('Object', function () {
-
it('Should convert an object', function () {
function keyConverter (k: string) {
return snakeCase(k)
@@ -143,7 +139,6 @@ describe('Object', function () {
})
describe('Bitrate', function () {
-
it('Should get appropriate max bitrate', function () {
const tests = [
{ resolution: VideoResolution.H_144P, ratio: 16 / 9, fps: 24, min: 200, max: 400 },
@@ -177,7 +172,6 @@ describe('Bitrate', function () {
})
describe('Ratio', function () {
-
it('Should have the correct aspect ratio in landscape', function () {
expect(buildAspectRatio({ width: 1920, height: 1080 })).to.equal(1.7778)
expect(buildAspectRatio({ width: 1000, height: 1000 })).to.equal(1)
@@ -190,7 +184,6 @@ describe('Bitrate', function () {
})
describe('Parse semantic version string', function () {
-
it('Should parse Node.js version string', function () {
const actual = parseSemVersion('v18.16.0')
@@ -241,7 +234,6 @@ describe('Parse semantic version string', function () {
})
describe('Extract chapters', function () {
-
it('Should not extract chapters', function () {
expect(parseChapters('my super description\nno?', 100)).to.deep.equal([])
expect(parseChapters('m00:00 super description\nno?', 100)).to.deep.equal([])
@@ -267,7 +259,13 @@ describe('Extract chapters', function () {
])
expect(parseChapters('hi\n\n00:01:30 chapter 1\n00:01:35 chapter 2\nhi\n00:01:40 chapter 3', 100)).to.deep.equal([
{ timecode: 90, title: 'chapter 1' },
- { timecode: 95, title: 'chapter 2' }
+ { timecode: 95, title: 'chapter 2' },
+ { timecode: 100, title: 'chapter 3' }
+ ])
+ expect(parseChapters('hi\n\n00:01:30 chapter 1\n00:01:35 chapter 2\nhi\n00:01:32 chapter 3\n00:01:40 chapter 4', 100)).to.deep.equal([
+ { timecode: 90, title: 'chapter 1' },
+ { timecode: 95, title: 'chapter 2' },
+ { timecode: 100, title: 'chapter 4' }
])
})