opusdec-js.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env node
  2. "use strict";
  3. var opus = require('../');
  4. var ogg = require('ogg');
  5. var fs = require('fs');
  6. var program = require('commander');
  7. var packagejson = require('../package.json');
  8. function toInt( n ) { return parseInt( n ); }
  9. program
  10. .version( packagejson.version )
  11. .option('--bitrate [N.nnn]',
  12. 'Set target bitrate in kbit/sec (6-256 per channel, default: 64)',
  13. toInt, 64 )
  14. .option('--rate [N]',
  15. 'Set sampling rate for raw input (default: 48000)',
  16. toInt, 48000 )
  17. .option('--channels [N]',
  18. 'Set sampling channels for raw input (default: 2)',
  19. toInt, 48000 )
  20. .arguments( '<input> <output>' )
  21. .parse( process.argv );
  22. if( program.args.length !== 2 )
  23. program.help();
  24. // Open the output streams.
  25. var input_raw = fs.createReadStream( program.args[ 0 ] );
  26. var output = fs.createWriteStream( program.args[ 1 ] );
  27. var oggDecoder = new ogg.Decoder();
  28. oggDecoder.on( 'stream', function( stream ) {
  29. // Create the decoder based on the command parameters.
  30. var decoder = new opus.Decoder( program.rate, program.channels );
  31. decoder.on( 'format', function( format ) {
  32. decoder.pipe( output );
  33. });
  34. decoder.on( 'error', function( err ) {
  35. console.error( err );
  36. });
  37. stream.pipe( decoder );
  38. });
  39. input_raw.pipe( oggDecoder )