Skip to content

Commit

Permalink
add public stream method
Browse files Browse the repository at this point in the history
  • Loading branch information
fadri1 committed Dec 29, 2023
1 parent e11f257 commit 5626398
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions framefusion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ImageData } from 'canvas';
import type { Stream } from '@danim/beamcoder';
import { BeamcoderExtractor } from './src/backends/beamcoder.js';

export type Frame = {
Expand Down Expand Up @@ -33,6 +34,8 @@ export interface Extractor {

get height(): number;

get stream(): Stream;

getFrameAtTime(targetTime: number): Promise<Frame>;

getImageDataAtTime(targetTime: number): Promise<ImageData>;
Expand Down
6 changes: 6 additions & 0 deletions src/BaseExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { ImageData } from 'canvas';
import type { Stream } from '@danim/beamcoder';

import type {
ExtractorArgs,
Frame,
Extractor
} from '../framefusion';


export class BaseExtractor implements Extractor {
static async create(args: ExtractorArgs): Promise<Extractor> {
throw new Error('Not implemented');
Expand Down Expand Up @@ -34,6 +36,10 @@ export class BaseExtractor implements Extractor {
throw new Error('Not implemented');
}

get stream(): Stream {
throw new Error('Not implemented');
}

async seekToPTS(targetPts: number) {
throw new Error('Not implemented');
}
Expand Down
22 changes: 15 additions & 7 deletions src/backends/beamcoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type {
Decoder,
Filterer,
Frame,
VideoInputParam
VideoInputParam,
Stream
} from '@danim/beamcoder';
import beamcoder from '@danim/beamcoder';
import type { ImageData } from 'canvas';
Expand Down Expand Up @@ -194,7 +195,7 @@ export class BeamcoderExtractor extends BaseExtractor implements Extractor {
throw new Error(`File has no ${STREAM_TYPE_VIDEO} stream!`);
}
this.#filterer = await createFilter({
stream: this.#demuxer.streams[this.#streamIndex],
stream: this.stream,
outputPixelFormat: COLORSPACE_RGBA,
});
}
Expand All @@ -217,21 +218,28 @@ export class BeamcoderExtractor extends BaseExtractor implements Extractor {
* This is the duration of the first video stream in the file expressed in seconds.
*/
get duration(): number {
return this.ptsToTime(this.#demuxer.streams[this.#streamIndex].duration);
return this.ptsToTime(this.stream.duration);
}

/**
* Width in pixels
*/
get width(): number {
return this.#demuxer.streams[this.#streamIndex].codecpar.width;
return this.stream.codecpar.width;
}

/**
* Height in pixels
*/
get height(): number {
return this.#demuxer.streams[this.#streamIndex].codecpar.height;
return this.stream.codecpar.height;
}

/**
* beamcoder Stream
*/
get stream(): Stream {
return this.#demuxer.streams[this.#streamIndex];
}

/**
Expand Down Expand Up @@ -269,15 +277,15 @@ export class BeamcoderExtractor extends BaseExtractor implements Extractor {
* Get the presentation timestamp (PTS) for a given time in seconds
*/
_timeToPTS(time: number) {
const time_base = this.#demuxer.streams[this.#streamIndex].time_base;
const time_base = this.stream.time_base;
return time * time_base[1] / time_base[0];
}

/**
* Get the time in seconds from a given presentation timestamp (PTS)
*/
ptsToTime(pts: number) {
const time_base = this.#demuxer.streams[this.#streamIndex].time_base;
const time_base = this.stream.time_base;
return pts * time_base[0] / time_base[1];
}

Expand Down

0 comments on commit 5626398

Please sign in to comment.