dnx-ytdown 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # ytdown
2
+
3
+ A simple command-line tool to download YouTube videos, music, or playlists using `yt-dlp`.
4
+
5
+ ## Prerequisites
6
+
7
+ You must have **[yt-dlp](https://github.com/yt-dlp/yt-dlp)** installed on your system and available in your system's PATH.
8
+
9
+ This tool is a wrapper around `yt-dlp` and will not work without it.
10
+
11
+ ## Installation
12
+
13
+ Install the tool globally using npm:
14
+
15
+ ```bash
16
+ npm install -g .
17
+ ```
18
+ *(Note: Since this is a local project, you install it with `.`)*
19
+
20
+ To publish and install from the registry, you would use:
21
+ ```bash
22
+ npm install -g ytdown
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ You can use `ytdown` directly from your command line after global installation, or you can use it without installation via `npx`.
28
+
29
+ ### Synopsis
30
+
31
+ ```bash
32
+ ytdown <link> [options]
33
+ ```
34
+
35
+ ### Arguments
36
+
37
+ - `<link>`: **(Required)** The URL of the YouTube video, music, or playlist you want to download.
38
+
39
+ ### Options
40
+
41
+ - `--format`, `-f`: The format to download.
42
+ - Choices: `mp3`, `mp4`
43
+ - Default: `mp4`
44
+ - `--output`, `-o`: The directory where files will be saved.
45
+ - Default: The current working directory.
46
+ - `--help`, `-h`: Show the help message.
47
+
48
+ ### Examples
49
+
50
+ **1. Download a video as MP4 (default format):**
51
+
52
+ ```bash
53
+ ytdown "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
54
+ ```
55
+
56
+ **2. Download a video as MP3 and save it to a specific folder:**
57
+
58
+ ```bash
59
+ ytdown "https://www.youtube.com/watch?v=dQw4w9WgXcQ" --format mp3 --output "/home/user/Music/MyDownloads"
60
+ ```
61
+
62
+ **3. Download a full playlist as MP3:**
63
+
64
+ ```bash
65
+ ytdown "https://www.youtube.com/playlist?list=PL..." --format mp3
66
+ ```
67
+
68
+ **4. Use with `npx` (no global installation needed):**
69
+
70
+ This command assumes you are running it from within the project directory.
71
+
72
+ ```bash
73
+ npx . "https://www.youtube.com/watch?v=dQw4w9WgXcQ" --format mp3
74
+ ```
package/index.js ADDED
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env node
2
+
3
+ import yargs from 'yargs';
4
+ import { hideBin } from 'yargs/helpers';
5
+ import { spawn, exec } from 'child_process';
6
+ import path from 'path';
7
+ import process from 'process';
8
+
9
+ // First, check if yt-dlp is installed
10
+ exec('yt-dlp --version', (error, stdout, stderr) => {
11
+ if (error) {
12
+ console.error("Error: 'yt-dlp' is not installed or not in your PATH.");
13
+ console.error("Please install it to use this tool. See: https://github.com/yt-dlp/yt-dlp");
14
+ process.exit(1);
15
+ }
16
+ // If check is successful, run the main program
17
+ main();
18
+ });
19
+
20
+ function main() {
21
+ yargs(hideBin(process.argv))
22
+ .command(
23
+ '$0 <link>',
24
+ 'Download a YouTube video, music, or playlist.',
25
+ (yargs) => {
26
+ return yargs.positional('link', {
27
+ describe: 'URL of the video, music, or playlist',
28
+ type: 'string',
29
+ });
30
+ },
31
+ (argv) => {
32
+ if (argv.link) {
33
+ download(argv.link, argv.format, argv.output);
34
+ } else {
35
+ yargs.showHelp();
36
+ }
37
+ }
38
+ )
39
+ .option('format', {
40
+ alias: 'f',
41
+ describe: 'Download format',
42
+ choices: ['mp3', 'mp4'],
43
+ default: 'mp4',
44
+ })
45
+ .option('output', {
46
+ alias: 'o',
47
+ describe: 'Output directory',
48
+ type: 'string',
49
+ default: process.cwd(), // Defaults to the current working directory
50
+ })
51
+ .help()
52
+ .alias('help', 'h')
53
+ .parse();
54
+ }
55
+
56
+ function download(link, format, outputDir) {
57
+ console.log(`Starting download...`);
58
+ console.log(` - URL: ${link}`);
59
+ console.log(` - Format: ${format}`);
60
+ console.log(` - Output Dir: ${outputDir}`);
61
+
62
+ const args = [
63
+ '-i', // Ignore errors
64
+ '--no-overwrites',
65
+ '--extractor-args', 'youtube:player_client=default',
66
+ ];
67
+
68
+ // Add format-specific arguments
69
+ if (format === 'mp3') {
70
+ args.push(
71
+ '-x', // Extract audio
72
+ '--audio-format', 'mp3',
73
+ '--embed-thumbnail', // Embed thumbnail
74
+ '--add-metadata' // Add metadata
75
+ );
76
+ } else { // for mp4
77
+ args.push(
78
+ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best'
79
+ );
80
+ }
81
+
82
+ // Define the output template
83
+ const outputTemplate = path.join(outputDir, '%(title)s.%(ext)s');
84
+ args.push('-o', outputTemplate);
85
+
86
+ // Add the URL to the arguments
87
+ args.push(link);
88
+
89
+ console.log(`\nExecuting: yt-dlp ${args.join(' ')}\n`);
90
+
91
+ const ytdlp = spawn('yt-dlp', args);
92
+
93
+ ytdlp.stdout.on('data', (data) => {
94
+ process.stdout.write(data.toString());
95
+ });
96
+
97
+ ytdlp.stderr.on('data', (data) => {
98
+ process.stderr.write(data.toString());
99
+ });
100
+
101
+ ytdlp.on('close', (code) => {
102
+ if (code === 0) {
103
+ console.log('\nDownload completed successfully.');
104
+ } else {
105
+ console.error(`\nProcess exited with code ${code}. There might have been an error.`);
106
+ }
107
+ });
108
+
109
+ ytdlp.on('error', (err) => {
110
+ console.error('Failed to start the yt-dlp process. Is it installed and in your PATH?', err);
111
+ });
112
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "dnx-ytdown",
3
+ "version": "1.0.0",
4
+ "description": "A command-line tool to download YouTube videos, music, or playlists as MP4 or MP3.",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "bin": {
8
+ "dnx-ytdown": "./index.js"
9
+ },
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "keywords": [
14
+ "youtube",
15
+ "download",
16
+ "mp3",
17
+ "mp4",
18
+ "cli",
19
+ "yt-dlp"
20
+ ],
21
+ "author": "",
22
+ "license": "ISC",
23
+ "dependencies": {
24
+ "yargs": "^18.0.0"
25
+ }
26
+ }