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AAS,EAAO,KAAK,KAAK,EAAQ,UACrC,KAAK,EACV,IAEwB,mBAAb,EACT,OAAO,EAGT,EACG,GAAG,QAAS,GACZ,KAAK,QAAO,SAAU,GACrB,EAAS,KAAM,EACjB,IACJ","file":"/npm/rip-subtitles@0.0.1/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/rip-subtitles@0.0.1/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","'use strict';\n\nvar probe = require('node-ffprobe');\nvar assign = require('object-assign');\nvar spawn = require('child_process').spawn;\nvar through = require('through2');\nvar concat = require('concat-stream');\nvar isVideo = require('is-video');\n\nvar defaults = {\n  lang: 'eng',\n  format: 'srt'\n};\n\n/**\n * Gets stream info for supplied video then tries to locate\n * the desired subtitle stream and spits it out in the specified format\n *\n * @throws {TypeError} if filename is not a valid video\n * @param {string} filename - path to the video\n * @param {object=} options - subtitle options\n * @param {string} options.lang - desired language\n * @param {string} options.format - desired format\n * @param {function=} callback - callback function\n * @return {stream} stream - only if a callback is not provided\n */\nmodule.exports = function (filename, options, callback) {\n  if (!isVideo(filename)) {\n    throw new TypeError('invalid parameter `filename`');\n  }\n\n  if (typeof options !== 'object') {\n    callback = options;\n    options = {};\n  }\n\n  options = assign({}, defaults, options);\n\n  var stream = through();\n\n  getSubtitleIndex(filename, options.lang, function (err, index) {\n    if (err) {\n      stream.emit('error', err);\n      return stream.end();\n    }\n\n    getSubtitleStream(filename, index, options.format)\n      .on('error', stream.emit.bind(stream, 'error'))\n      .pipe(stream);\n  });\n\n  if (typeof callback !== 'function') {\n    return stream;\n  }\n\n  stream\n    .on('error', callback)\n    .pipe(concat(function (subtitles) {\n      callback(null, subtitles);\n    }));\n};\n\n/**\n * Attempts to get the index of the subtitle stream\n * The callback will pass back an error if we can't find it\n *\n * @param {string} filename - video file name\n * @param {string} lang - language of desired subtitle\n * @param {function} callback - rly\n */\nfunction getSubtitleIndex (filename, lang, callback) {\n  probe(filename, function (err, data) {\n    if (err) {\n      return callback(err);\n    }\n\n    var streams = data.streams.filter(function (stream) {\n      var isSubtitle = stream.codec_type === 'subtitle';\n      var hasLang = stream['TAG:language'] === lang;\n\n      return isSubtitle && hasLang;\n    });\n\n    if (!streams.length) {\n      return callback(new Error('subtitle not found for lang \"' + lang + '\"'));\n    }\n\n    callback(null, data.streams.indexOf(streams[0]));\n  });\n}\n\n/**\n * Streams the subtitle file in the desired format\n *\n * @param {string} filename - video filename\n * @param {number} index - index of desired subtitle stream\n * @param {string} format - subtitle format (srt, ass, etc.)\n * @return {stream}\n */\nfunction getSubtitleStream (filename, index, format) {\n  var stream = through();\n\n  var ffmpeg = spawn('ffmpeg', [\n    '-i', filename,\n    '-an', '-vn',\n    '-map', '0:' + index,\n    '-c:s:0', format,\n    '-f', format,\n    'pipe:1'\n  ]);\n\n  ffmpeg.on('error', stream.emit.bind(stream, 'error'));\n  ffmpeg.on('exit', stream.end.bind(stream));\n\n  ffmpeg.stdout.pipe(stream);\n\n  return stream;\n}\n"]}