UNPKG

videory

Version:

CLI for transcoding videos

87 lines (68 loc) 2.4 kB
import dotenv from "dotenv" import args from "args" import {findAndUpdate} from "./hound.js" import {logAndExit, pause} from "./helpers.js" import {schedule} from "./transcode.js" import {removeStalledTranscodings, initializeDb, findNotTranscodedVideos} from "./db.js" import logNode from "log-node"; import PrettyError from "pretty-error" import log from "log"; const debug = log.get('videory:index') logNode(); const pe = new PrettyError() const searchExt = ["MP4", "MOV"] dotenv.config(); args .option("in", "Input directory transcode videos from") .option("out", "Output directory to transcode videos to") .option("allow-versions", "Create file versions if output files already exists") .option("memory", "Use in-memory database instead of a filesystem file") .option("watch", "Do not exit after initial run and watch input directory for changes") ; const flags = args.parse(process.argv); const d_in = flags.i; const d_out = flags.o || d_in; const inMemoryDb = flags.m const watch = flags.w if (!d_in) { throw pe.render(new Error("missing 'in' flag")); } if (!d_out) { throw pe.render(new Error("missing 'out' flag")); } if (flags.allowVersions) { global.allowVersions = true; } (async function () { debug.notice("Initializing the database..."); //init sqlite await initializeDb(inMemoryDb) .catch(logAndExit(1)); //remove videos that started to transcode and did not finish for some reason await removeStalledTranscodings(); debug.notice("Indexing videos..."); watchLoop: while (true) { // search fs for videos await findAndUpdate([d_in], searchExt) .catch(logAndExit(2)); debug.notice("Transcoding videos..."); //start transcode let videosToTranscode = 0; transcodeLoop: do { videosToTranscode = (await findNotTranscodedVideos()).length; // transcode a batch of videos at a time await schedule(d_out).next(); } while (videosToTranscode) if (watch) { // wait and continue process await pause(); } else { // exit process break watchLoop; } } debug.notice("Finished successfully"); process.exit(0); })();