Skip to content
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

LU-3510 Adding duration support for webm #61

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lumen5/framefusion",
"version": "1.0.6",
"version": "1.0.7",
"type": "module",
"scripts": {
"docs": "typedoc framefusion.ts",
Expand Down
16 changes: 15 additions & 1 deletion src/backends/beamcoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const VERBOSE = false;
*/
const RGBA_PIXEL_SIZE = 4;

// Convert a duration string in the format HH:MM:SS to seconds
// Example: 00:01:30 -> 90
function convertDurationToSeconds(duration) {
const [hours, minutes, seconds] = duration.split(':').map(parseFloat);
return hours * 3600 + minutes * 60 + seconds;
}

const createDecoder = ({
demuxer,
streamIndex,
Expand Down Expand Up @@ -242,7 +249,14 @@ 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);
const stream = this.#demuxer.streams[this.#streamIndex];
if (stream.duration !== null) {
return this.ptsToTime(stream.duration);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, when I was playing with libav, I noticed that duration is negative sometimes. are we sure it's not the case here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duration is "null" in this case.

}
if (stream?.metadata?.DURATION) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's reliable. it is just a dictionary
https://ffmpeg.org/doxygen/7.0/structAVStream.html#a4e04af7a5a4d8298649850df798dd0bc
Have you tried this?

https://stackoverflow.com/a/27208276
it seems like beamocoder executes similar code under the hood, so, the duration should be available somewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that stream.duration is null in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we can call duration on the parent object which seems to return the correct value. I'm going to use that instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly , beamcoder executes code from stack overflow and it expands avformat context

return convertDurationToSeconds(stream.metadata.DURATION);
}
return 0;
}

/**
Expand Down
15 changes: 14 additions & 1 deletion test/framefusion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('FrameFusion', () => {
await extractor.dispose();
});

it('can get duration', async() => {
it('can get duration from mp4', async() => {
// Arrange
const extractor = await BeamcoderExtractor.create({
inputFileOrUrl: 'https://storage.googleapis.com/lumen5-prod-images/countTo60.mp4',
Expand All @@ -73,6 +73,19 @@ describe('FrameFusion', () => {
await extractor.dispose();
});

it.only('can get duration from webm', async() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it.only('can get duration from webm', async() => {
it('can get duration from webm', async() => {

// Arrange
const extractor = await BeamcoderExtractor.create({
inputFileOrUrl: 'https://storage.googleapis.com/lumen5-prod-video/anita-6uTzyZtNRKztypC.webm',
});

// Act and Assert
expect(extractor.duration).to.equal(115);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@animanathome can we use test data which we inspect in this repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean with that @stepancar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work for you locally?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@animanathome I mean, can we put that video to this repo directly so it would be easy to call ffprobe


// Cleanup
await extractor.dispose();
});

it('can get duration when audio stream is longer than video stream', async() => {
// Arrange
const extractor = await BeamcoderExtractor.create({
Expand Down
Loading