From ebfa2bcf444454234bf559bf2f5efba3ecfec518 Mon Sep 17 00:00:00 2001 From: Maxime Baconnais Date: Fri, 10 Jan 2020 21:34:58 +0100 Subject: [PATCH 1/2] URL support --- .gitignore | 2 ++ README.md | 6 +--- examples/stream.ts | 6 ++-- package.json | 3 ++ src/index.ts | 69 ++++++++++++++++++++++++++++++++++++---------- 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 816ae80..b7cad50 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ dist node_modules tmp +**/*.js +package-lock.json \ No newline at end of file diff --git a/README.md b/README.md index 6ed973a..2e98cd8 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,11 @@ Node implementation of [MediaArea's MediaInfo](https://mediaarea.net/en/MediaInfo) WebAssembly binary. ## Install - ```sh $ npm install --save node-mediainfo ``` ## Usage - ```ts // Using ES6 imports import mediainfo from 'node-mediainfo'; @@ -20,7 +18,7 @@ import mediainfo from 'node-mediainfo'; const mediainfo = require('node-mediainfo'); async function main() { - const result = await mediainfo('path to media file'); + const result = await mediainfo(''); console.log(result); } @@ -32,6 +30,4 @@ main(); - Package: [npmjs.com/package/node-mediainfo](https://www.npmjs.com/package/node-mediainfo) ## License - - This package uses [MediaInfo](http://mediaarea.net/MediaInfo) library, Copyright (c) 2002-2019 [MediaArea.net SARL](mailto:Info@MediaArea.net) diff --git a/examples/stream.ts b/examples/stream.ts index ecd9c47..8dd00af 100644 --- a/examples/stream.ts +++ b/examples/stream.ts @@ -1,9 +1,11 @@ import mediainfo from '../src'; -mediainfo('').then( +mediainfo('').then( (response) => { console.log(JSON.stringify( response, null, 2, )); }, -); +).catch((err) => { + console.error(err); +}); diff --git a/package.json b/package.json index f82da17..3106d53 100644 --- a/package.json +++ b/package.json @@ -26,5 +26,8 @@ "build": "npm run types && tsc", "prepare": "npm run build", "types": "tsc --outDir tmp src/build.ts --lib es2017 && node tmp/build" + }, + "dependencies": { + "request": "^2.88.0" } } diff --git a/src/index.ts b/src/index.ts index 491ffef..214c944 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ -import { PathLike, createReadStream, statSync } from 'fs'; +import { PathLike, createReadStream, stat } from 'fs'; +import * as Request from 'request'; import { Audio, General, Image, Menu, Other, Text, Video, } from './types'; @@ -26,20 +27,59 @@ async function Init() { }); } -export = async function MediaInfo(path: PathLike): Promise { - if (!MediaInfoModule) { - await Init(); +async function GetSize(path: PathLike, headers = {}) { + return new Promise((resolve, reject) => { + if (path.toString().indexOf('http') === 0) { + return Request({ + method: 'HEAD', + url: path, + headers + }).on('response', (res) => { + resolve(parseInt(res.headers['content-length'], 10) || 0); + }).on('error', (err) => { + reject(err); + }) + } + stat(path, (err, stats) => { + if (err) { + return reject(err); + } + resolve(stats.size || 0) + }) + }) +} + +function GetStream(path: PathLike, start = 0, length = -1, headers = {}) { + if (path.toString().indexOf('http') === 0) { + return Request({ + url: path, + headers: { + ...headers, + Range: `bytes=${start}-${length}` + } + }) } - return new Promise((resolve) => { - const stream = createReadStream(path, { - highWaterMark: 1024 * 1024, - }); - const { size } = statSync(path); + return createReadStream(path, { + highWaterMark: length, + start, + }); +} + +export default function MediaInfo(path: PathLike, headers = {}): Promise { + return new Promise(async (resolve) => { + + if (!MediaInfoModule) { + await Init(); + } + + let seekTo: number; + + const stream = GetStream(path, 0, 1024 * 1024, headers); + const size = await GetSize(path); const MI = new MediaInfoModule.MediaInfo(); MI.Open_Buffer_Init(size, 0); - let seekTo: number; stream.on('data', (chunk) => { MI.Open_Buffer_Continue(chunk); @@ -48,15 +88,14 @@ export = async function MediaInfo(path: PathLike): Promise { if (seekTo !== -1) { MI.Open_Buffer_Init(size, seekTo); - stream.close(); + if (stream.close) { + stream.close(); + } } }); stream.on('close', () => { - const newstream = createReadStream(path, { - highWaterMark: 1024 * 1024, - start: seekTo, - }); + const newstream = GetStream(path, seekTo, 1024 * 1024, headers); newstream.on('data', (chunk) => { MI.Open_Buffer_Continue(chunk); From 3e3eb84a8d3051358f82d813d5e5890c316c3194 Mon Sep 17 00:00:00 2001 From: Maxime Baconnais Date: Sat, 11 Jan 2020 11:55:39 +0100 Subject: [PATCH 2/2] Fix build --- examples/stream.ts | 2 +- package.json | 1 + src/index.ts | 18 +++++++++--------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/examples/stream.ts b/examples/stream.ts index 8dd00af..38c2e54 100644 --- a/examples/stream.ts +++ b/examples/stream.ts @@ -1,6 +1,6 @@ import mediainfo from '../src'; -mediainfo('').then( +mediainfo('http://dl5.webmfiles.org/big-buck-bunny_trailer.webm').then( (response) => { console.log(JSON.stringify( response, null, 2, diff --git a/package.json b/package.json index 3106d53..50ea324 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "types": "tsc --outDir tmp src/build.ts --lib es2017 && node tmp/build" }, "dependencies": { + "@types/request": "^2.48.4", "request": "^2.88.0" } } diff --git a/src/index.ts b/src/index.ts index 214c944..d1c981e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { PathLike, createReadStream, stat } from 'fs'; -import * as Request from 'request'; +import Request = require('request'); import { Audio, General, Image, Menu, Other, Text, Video, } from './types'; @@ -32,11 +32,11 @@ async function GetSize(path: PathLike, headers = {}) { if (path.toString().indexOf('http') === 0) { return Request({ method: 'HEAD', - url: path, + url: path.toString(), headers - }).on('response', (res) => { + }).on('response', (res: any) => { resolve(parseInt(res.headers['content-length'], 10) || 0); - }).on('error', (err) => { + }).on('error', (err: Error) => { reject(err); }) } @@ -49,10 +49,10 @@ async function GetSize(path: PathLike, headers = {}) { }) } -function GetStream(path: PathLike, start = 0, length = -1, headers = {}) { +function GetStream(path: PathLike, start = 0, length = -1, headers = {}): any { if (path.toString().indexOf('http') === 0) { return Request({ - url: path, + url: path.toString(), headers: { ...headers, Range: `bytes=${start}-${length}` @@ -81,14 +81,14 @@ export default function MediaInfo(path: PathLike, headers = {}): Promise { + stream.on('data', (chunk: any) => { MI.Open_Buffer_Continue(chunk); seekTo = MI.Open_Buffer_Continue_Goto_Get(); // console.log('SeekTo', seekTo); if (seekTo !== -1) { MI.Open_Buffer_Init(size, seekTo); - if (stream.close) { + if (typeof (stream.close) !== 'undefined') { stream.close(); } } @@ -97,7 +97,7 @@ export default function MediaInfo(path: PathLike, headers = {}): Promise { const newstream = GetStream(path, seekTo, 1024 * 1024, headers); - newstream.on('data', (chunk) => { + newstream.on('data', (chunk: any) => { MI.Open_Buffer_Continue(chunk); seekTo = MI.Open_Buffer_Continue_Goto_Get(); // console.log('SeekTo', seekTo);