Skip to content

Instantly share code, notes, and snippets.

@pjobson
Created April 24, 2023 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjobson/bbe922bf66ac472469b6becd8223dd6e to your computer and use it in GitHub Desktop.
Save pjobson/bbe922bf66ac472469b6becd8223dd6e to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// Script for extracting all files from an MKV container.
const fs = require('fs');
const path = require('path');
const process = require('process');
const shellescape = require('shell-escape');
const child_process = require('child_process');
const MKVINFO = '/usr/bin/mkvmerge -J';
const MKVEXTRACT = '/usr/bin/mkvextract';
const mkvfile = process.argv[2];
const outpath = path.resolve(process.argv[3] || '.');
const init = () => {
if (!mkvfile) {
console.log('Missing mkv file.');
process.exit(0);
}
let videoTracks = [];
const infoCmd = `${MKVINFO} ${shellescape([mkvfile])}`;
const infoJSON = JSON.parse(child_process.execSync(infoCmd).toString());
let mkvExtExec = `${MKVEXTRACT} tracks ${shellescape([mkvfile])}`;
infoJSON.tracks.forEach(trk => {
const ext = trk.codec.replace(/\//g,'-');
const id = trk.id;
const lang = trk.properties.language;
let trackName = '';
switch (trk.type) {
case 'video':
trackName += `${trk.id}:${shellescape([`${outpath}/${trk.id}.video`])}`;
break;
case 'subtitles':
trackName += `${trk.id}:${shellescape([`${outpath}/${trk.id}.${trk.properties.language}.subtitle`])}`;
break;
case 'audio':
trackName += `${trk.id}:${shellescape([`${outpath}/${trk.id}.${trk.properties.audio_channels}channels.${trk.type}.${trk.properties.language}`])}`;
break;
default:
trackName += `${trk.id}:${shellescape([`${outpath}/${trk.id}.${trk.type}.${trk.properties.language}`])}`;
};
mkvExtExec += ` ${trackName}`;
});
child_process.execSync(mkvExtExec);
};
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment