Skip to content

Instantly share code, notes, and snippets.

@czaux
Last active June 30, 2020 06:55
Show Gist options
  • Save czaux/3af94d8f867b730be2fd265b0c0380c1 to your computer and use it in GitHub Desktop.
Save czaux/3af94d8f867b730be2fd265b0c0380c1 to your computer and use it in GitHub Desktop.
'use strict';
const fs = require('fs');
const fsp = fs.promises;
const MediaInfo = require('mediainfo.js');
const path = require('path');
async function readMediaFile(filePath) {
let fileHandle;
let mediainfo;
try {
fileHandle = await fsp.open(filePath, 'r');
mediainfo = await MediaInfo({ chunkSize: 1024*1024, format: 'JSON', cover_data: true });
const getSize = async () => (await fileHandle.stat()).size
const readChunk = async (size, offset) => {
const buffer = new Uint8Array(size)
await fileHandle.read(buffer, 0, size, offset)
return buffer
}
const result = await mediainfo.analyzeData(getSize, readChunk);
console.log(result)
} catch (error) {
console.error(error)
} finally {
fileHandle && (await fileHandle.close())
mediainfo && mediainfo.close()
}
}
module.exports.readMediaFile = readMediaFile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment