get-video-duration
Version:
Get the duration of a video file
53 lines • 2.66 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVideoDurationInSeconds = void 0;
const ffprobe = require("@ffprobe-installer/ffprobe");
const execa_1 = require("execa");
const is_stream_1 = require("is-stream");
const getFFprobeWrappedExecution = (input, ffprobePath) => {
const params = ["-v", "error", "-show_format", "-show_streams"];
const overridenPath = ffprobePath || ffprobe.path;
if (typeof input === "string") {
return (0, execa_1.execa)(overridenPath, [...params, input]);
}
if ((0, is_stream_1.isStream)(input)) {
return (0, execa_1.execa)(overridenPath, [...params, "-i", "pipe:0"], {
reject: false,
input,
});
}
throw new Error("Given input was neither a string nor a Stream");
};
/**
* Returns a promise that will be resolved with the duration of given video in
* seconds.
*
* @param input Stream or URL or path to file to be used as
* input for `ffprobe`.
* @param [ffprobePath] Optional. Path to `ffprobe` binary. Do not provide any
* value for this parameter unless you need to override the path to `ffprobe`.
* Defaults to the path provided by `@ffprobe-installer/ffprobe`, which works in
* most environments.
*
* @return Promise that will be resolved with given video duration in
* seconds.
*/
const getVideoDurationInSeconds = (input, ffprobePath) => __awaiter(void 0, void 0, void 0, function* () {
const { stdout } = yield getFFprobeWrappedExecution(input, ffprobePath);
const matched = stdout === null || stdout === void 0 ? void 0 : stdout.match(/duration="?(\d*\.\d*)"?/);
if (matched === null || matched === void 0 ? void 0 : matched[1])
return parseFloat(matched === null || matched === void 0 ? void 0 : matched[1]);
throw new Error("No duration found!");
});
exports.getVideoDurationInSeconds = getVideoDurationInSeconds;
exports.default = getVideoDurationInSeconds;
//# sourceMappingURL=index.js.map