Skip to content

Commit bd86a2d

Browse files
committed
refactor: generate a keyframe every 3 seconds.
1 parent e465512 commit bd86a2d

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

.changeset/long-beers-compete.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@webav/av-recorder': patch
3+
'@webav/av-cliper': patch
4+
---
5+
6+
refactor: generate a keyframe every 3 seconds.

packages/av-cliper/src/combinator.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ export class Combinator {
315315
outputAudio,
316316
hasVideoTrack: this.#hasVideoTrack,
317317
timeSlice,
318+
fps,
318319
});
319320

320321
let ts = 0;
@@ -418,10 +419,13 @@ function createAVEncoder(opts: {
418419
outputAudio?: boolean;
419420
hasVideoTrack: boolean;
420421
timeSlice: number;
422+
fps: number;
421423
}) {
422424
const { ctx, cvs, outputAudio, remux, hasVideoTrack, timeSlice } = opts;
423425
const { width, height } = cvs;
424426
let frameCnt = 0;
427+
// 3s 一个 GOP
428+
const gopSize = Math.floor(3 * opts.fps);
425429

426430
const audioTrackBuf = createAudioTrackBuf(1024);
427431

@@ -437,8 +441,7 @@ function createAVEncoder(opts: {
437441
});
438442

439443
remux.encodeVideo(vf, {
440-
// todo: 3s 1 GoP
441-
keyFrame: frameCnt % 150 === 0,
444+
keyFrame: frameCnt % gopSize === 0,
442445
});
443446
ctx.resetTransform();
444447
ctx.clearRect(0, 0, width, height);

packages/av-recorder/src/av-recorder.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ class RecoderPauseCtrl {
177177
// 触发暂停的时间,用于计算暂停持续了多久
178178
#pauseTime = 0;
179179

180-
constructor(readonly expectFPS: number) {}
180+
// 间隔多少帧生成一个关键帧
181+
#gopSize = 30;
182+
183+
constructor(readonly expectFPS: number) {
184+
this.#gopSize = Math.floor(expectFPS * 3);
185+
}
181186

182187
start() {
183188
this.#offsetTime = performance.now();
@@ -221,7 +226,7 @@ class RecoderPauseCtrl {
221226
frame.close();
222227
return {
223228
vf,
224-
opts: { keyFrame: this.#frameCnt % 30 === 0 },
229+
opts: { keyFrame: this.#frameCnt % this.#gopSize === 0 },
225230
};
226231
}
227232

0 commit comments

Comments
 (0)