Skip to content

Commit

Permalink
Use WebAssembly, refactoring
Browse files Browse the repository at this point in the history
- Refactor build process.
- Use WebAssembly instead of asm.js.
- Bundle using rollup.
- Add convenient API that works in browser and Node.js.
- Add simple browser example.
- Add mediainfo.js Node CLI.
  • Loading branch information
buzz committed Mar 19, 2020
1 parent d0df9b6 commit 7759697
Show file tree
Hide file tree
Showing 18 changed files with 389 additions and 116 deletions.
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
ThumbsDB
\#*#
.#*
ZenLib
MediaInfoLib
Shared
mediainfojs.o
npm-debug.log
/node_modules
npm-debug.log
/build
/dist
27 changes: 0 additions & 27 deletions build-deps.sh

This file was deleted.

9 changes: 0 additions & 9 deletions build.sh

This file was deleted.

11 changes: 11 additions & 0 deletions examples/browser-simple/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
env: {
browser: true,
},
globals: {
MediaInfo: 'readable',
},
parserOptions: {
sourceType: 'script',
},
}
34 changes: 34 additions & 0 deletions examples/browser-simple/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fileinput = document.getElementById('fileinput')
const output = document.getElementById('output')

const onChangeFile = (mediainfo) => {
const file = fileinput.files[0]
if (file) {
output.value = 'Working…'
mediainfo
.analyzeData({
readData: (chunkSize, offset) =>
new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = (event) => {
if (event.target.error) {
reject(event.target.error)
}
resolve(new Uint8Array(event.target.result))
}
reader.readAsArrayBuffer(file.slice(offset, offset + chunkSize))
}),
getSize: () => file.size,
})
.then((result) => {
output.value = result
})
.catch((error) => {
output.value = `An error occured:\n${error.stack}`
})
}
}

MediaInfo({ format: 'text' }).then((mediainfo) => {
fileinput.addEventListener('change', () => onChangeFile(mediainfo))
})
39 changes: 39 additions & 0 deletions examples/browser-simple/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>mediainfo.js simple demo</title>
<style>
html, body{
margin:0;
padding:0;
}
body * {
box-sizing: border-box;
}
#wrapper {
display: flex;
flex-direction: column;
height: 100%;
padding: 8px;
position: absolute;
width: 100%;
}
#fileinput {
padding-bottom: 8px;
}
#output {
height: 100%;
}
</style>
</head>
<body>
<div id="wrapper">
<input type="file" id="fileinput" name="fileinput" />
<textarea id="output"></textarea>
</div>
<script type="text/javascript" src="../../dist/mediainfo.js"></script>
<script type="text/javascript" src="./example.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions examples/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env node

const { open } = require('fs').promises
const MediaInfo = require('../dist/mediainfo.js')

const main = async (filePath) => {
let fileHandle
let mediainfo
try {
fileHandle = await open(filePath, 'r')
mediainfo = await MediaInfo({ format: 'text' })
const result = await mediainfo.analyzeData({
readData: async (size, offset) => {
const buffer = new Uint8Array(size)
await fileHandle.read(buffer, 0, size, offset)
return buffer
},
getSize: async () => (await fileHandle.stat()).size,
})
console.log(result)
} catch (error) {
console.error(error)
} finally {
fileHandle && (await fileHandle.close())
mediainfo && mediainfo.close()
}
}

const filePath = process.argv[2]
if (!filePath) {
console.log(`Usage: ${process.argv[1]} FILENAME`)
} else {
main(filePath)
}
57 changes: 0 additions & 57 deletions mediainfo.js

This file was deleted.

Binary file removed mediainfo.js.mem
Binary file not shown.
89 changes: 89 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.2",
"description": "Emscripten port of MediaInfoLib displays information about video and audio files.",
"author": "buzz",
"main": "mediainfo.js",
"main": "dist/mediainfo.js",
"repository": {
"type": "git",
"url": "https://github.com/buzz/mediainfo.js"
Expand All @@ -25,12 +25,17 @@
"build": "bash scripts/build.sh web",
"clean": "rimraf build dist vendor"
},
"bin": {
"mediainfo.js": "./examples/cli.js"
},
"dependencies": {},
"devDependencies": {
"@rollup/plugin-commonjs": "^11.0.2",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
"prettier": "^1.19.1",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"rollup": "^2.1.0"
}
}
7 changes: 0 additions & 7 deletions post.js

This file was deleted.

9 changes: 0 additions & 9 deletions pre.js

This file was deleted.

12 changes: 12 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'path'
import commonjs from '@rollup/plugin-commonjs';

export default {
input: path.resolve(__dirname, 'src', 'index.js'),
output: {
file: path.resolve(__dirname, 'dist', 'mediainfo.js'),
format: 'umd',
name: 'MediaInfo'
},
plugins: [commonjs()]
};
Loading

0 comments on commit 7759697

Please sign in to comment.