From 3a8ed600ac6cb8637c76e5a6d6eb893a0f360056 Mon Sep 17 00:00:00 2001 From: molikuner Date: Wed, 6 May 2020 09:28:17 +0200 Subject: [PATCH] Fix auto rename for duplicate video titles (#118) * Fix `makeUniqueTitle` was not working with (custom) output format * Add option to skip already existing files * Update README to include --skip option Co-authored-by: molikuner --- README.md | 6 ++++-- src/CommandLineParser.ts | 8 +++++++- src/Utils.ts | 3 ++- src/destreamer.ts | 23 +++++++++++++++-------- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 60d0fa8..e479c3e 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,10 @@ Options: --acodec Re-encode audio track. Specify FFmpeg codec (e.g. libopus) or set to "none" to disable audio. [string] [default: "copy"] - --format Output container format (mkv, mp4, mov, anything that FFmpeg supports) - [string] [default: "mkv"] + --format Output container format (mkv, mp4, mov, anything that + FFmpeg supports) [string] [default: "mkv"] + --skip Skip download if file already exists + [boolean] [default: false] ``` We default to `.mkv` for the output container. If you prefer something else (like `mp4`), pass `--format mp4`. diff --git a/src/CommandLineParser.ts b/src/CommandLineParser.ts index c8ccbe1..7c9f8d9 100644 --- a/src/CommandLineParser.ts +++ b/src/CommandLineParser.ts @@ -79,6 +79,12 @@ export const argv = yargs.options({ type: 'string', default: 'mkv', demandOption: false + }, + skip: { + describe: 'Skip download if file already exists', + type: 'boolean', + default: false, + demandOption: false } }) /** @@ -207,4 +213,4 @@ function windowsFileExtensionBadBehaviorFix(argv: any) { } return true; -} \ No newline at end of file +} diff --git a/src/Utils.ts b/src/Utils.ts index a8776f6..aed9a45 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -4,6 +4,7 @@ import { execSync } from 'child_process'; import colors from 'colors'; import fs from 'fs'; import path from 'path'; +import { argv } from './CommandLineParser'; function sanitizeUrls(urls: string[]) { const rex = new RegExp(/(?:https:\/\/)?.*\/video\/[a-z0-9]{8}-(?:[a-z0-9]{4}\-){3}[a-z0-9]{12}$/, 'i'); @@ -126,7 +127,7 @@ export function makeUniqueTitle(title: string, outDir: string) { let ntitle = title; let k = 0; - while (fs.existsSync(outDir + path.sep + ntitle + '.mp4')) + while (!argv.skip && fs.existsSync(outDir + path.sep + ntitle + '.' + argv.format)) ntitle = title + ' - ' + (++k).toString(); return ntitle; diff --git a/src/destreamer.ts b/src/destreamer.ts index 288c515..66436a5 100644 --- a/src/destreamer.ts +++ b/src/destreamer.ts @@ -218,7 +218,8 @@ async function downloadVideo(videoUrls: string[], outputDirectories: string[], s ])); const ffmpegOutput = new FFmpegOutput(outputPath, new Map([ argv.acodec === 'none' ? ['an', null] : ['c:a', argv.acodec], - argv.vcodec === 'none' ? ['vn', null] : ['c:v', argv.vcodec] + argv.vcodec === 'none' ? ['vn', null] : ['c:v', argv.vcodec], + ['n', null] ])); const ffmpegCmd = new FFmpegCommand(); @@ -255,17 +256,23 @@ async function downloadVideo(videoUrls: string[], outputDirectories: string[], s } }); - ffmpegCmd.on('error', (error: any) => { - cleanupFn(); - - console.log(`\nffmpeg returned an error: ${error.message}`); - process.exit(ERROR_CODE.UNK_FFMPEG_ERROR); - }); - process.on('SIGINT', cleanupFn); // let the magic begin... await new Promise((resolve: any, reject: any) => { + ffmpegCmd.on('error', (error: any) => { + if (argv.skip && error.message.includes('exists') && error.message.includes(outputPath)) { + pbar.update(video.totalChunks); // set progress bar to 100% + console.log(colors.yellow(`\nFile already exists, skipping: ${outputPath}`)); + resolve(); + } else { + cleanupFn(); + + console.log(`\nffmpeg returned an error: ${error.message}`); + process.exit(ERROR_CODE.UNK_FFMPEG_ERROR); + } + }); + ffmpegCmd.on('success', (data:any) => { pbar.update(video.totalChunks); // set progress bar to 100% console.log(colors.green(`\nDownload finished: ${outputPath}`));