-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide dimensions for MP4 (#145)
* feat: provide dimensions for MP4 (wip) * chore(test): increase coverage * chore(test): increase coverage * chore(test): increase coverage to 100% * chore(test): simplify and extend
- Loading branch information
1 parent
c772986
commit dea5031
Showing
11 changed files
with
647 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,49 @@ | ||
/* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Generic Atom | ||
*/ | ||
export class Atom { | ||
constructor(parent, buf, type, pos) { | ||
this.parent = parent; | ||
this.buf = buf; | ||
this.type = type; | ||
this.pos = pos || 0; | ||
this.children = []; | ||
|
||
parent?.children.push(this); | ||
} | ||
|
||
/** | ||
* Assert that an atom has at least that length. | ||
* | ||
* @param {String} name name | ||
* @param {Number} num number of bytes expected | ||
*/ | ||
assertLength(name, num) { | ||
const { buf, pos } = this; | ||
if (buf.length < num) { | ||
throw new Error(`[${name}] ${this.getPath()} (${pos}): atom should contain at least ${num} bytes, found: ${buf.length}`); | ||
} | ||
} | ||
|
||
getPath() { | ||
const { parent, type } = this; | ||
return `${parent?.getPath() || ''}/${type}`; | ||
} | ||
|
||
toString() { | ||
const { buf, type, pos } = this; | ||
return `${type}: [${pos}-${pos + buf.length}]`; | ||
} | ||
} |
This file contains 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,28 @@ | ||
/* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint-disable no-bitwise */ | ||
|
||
import { Atom } from './Atom.js'; | ||
|
||
/** | ||
* File type atom. | ||
*/ | ||
export class FTYPAtom extends Atom { | ||
parseContent(context) { | ||
const { buf } = this; | ||
const { name } = context; | ||
|
||
this.assertLength(name, 4); | ||
context.filetype = buf.toString('ascii', 0, 4); | ||
} | ||
} |
This file contains 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,32 @@ | ||
/* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint-disable no-bitwise */ | ||
|
||
import { Atom } from './Atom.js'; | ||
|
||
/** | ||
* Handler reference atom. | ||
*/ | ||
export class HDLRAtom extends Atom { | ||
parseContent(context) { | ||
const { buf, pos } = this; | ||
const { tracks, name } = context; | ||
|
||
this.assertLength(name, 24); | ||
if (tracks.length === 0) { | ||
throw new Error(`[${name}] ${this.getPath()} (${pos}): no tracks added by 'tkhd'`); | ||
} | ||
const track = tracks[tracks.length - 1]; | ||
track.subtype = buf.toString('ascii', 8, 12); | ||
} | ||
} |
This file contains 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,35 @@ | ||
/* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
import { Atom } from './Atom.js'; | ||
|
||
/** | ||
* Movie header atom. | ||
*/ | ||
export class MVHDAtom extends Atom { | ||
parseContent(context) { | ||
const { buf, pos } = this; | ||
const { movie, name, log } = context; | ||
|
||
this.assertLength(name, 100); | ||
|
||
const version = buf.readUInt8(); | ||
let timescale = buf.readInt32BE(12); | ||
if (timescale <= 0) { | ||
log.warn(`[${name}] ${this.getPath()} (${pos + 12}): invalid time scale ${timescale}, defaulting to 1`); | ||
timescale = 1; | ||
} | ||
const duration = version === 1 | ||
? Number(buf.readBigUInt64BE(16)) | ||
: buf.readUInt32BE(16); | ||
movie.duration = Math.round(duration / timescale); | ||
} | ||
} |
Oops, something went wrong.