opusenc-js.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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('--serial [serialno]',
  12. 'Set the serial number for the stream instead of using random. Meant for debugging.',
  13. toInt, null )
  14. .option('--framesize [size]',
  15. 'Set maximum frame size in milliseconds (2.5, 5, 10, 20, 40, 60, default: 20)',
  16. toInt, 20 )
  17. .option('--raw-rate [N]',
  18. 'Set sampling rate for raw input (default: 48000)',
  19. toInt, 48000 )
  20. .option('--raw-chan [N]',
  21. 'Set number of channels for raw input (default: 2)',
  22. toInt, 2 )
  23. .arguments( '<input> <output>' )
  24. .parse( process.argv );
  25. if( program.args.length !== 2 )
  26. program.help();
  27. // Create the encoder based on the command parameters.
  28. var framebytes = program.rawRate * program.framesize / 1000;
  29. var encoder = new opus.Encoder( program.rawRate, program.rawChan, framebytes );
  30. // Open the output streams.
  31. var input_raw = fs.createReadStream( program.args[ 0 ] );
  32. var output = fs.createWriteStream( program.args[ 1 ] );
  33. var oggEncoder = new ogg.Encoder();
  34. input_raw.pipe( encoder ).pipe( oggEncoder.stream( program.serial ) )
  35. oggEncoder.pipe( output )