Enhance downloading capabilities on YouTube with playlist and channel support.
当前为
// ==UserScript==
// @name Generate YouTube Download commands for yt-dlp terminal
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Enhance downloading capabilities on YouTube with playlist and channel support.
// @author ChatGPT
// @match *://*.youtube.com/*
// @grant GM_setClipboard
// @grant GM_registerMenuCommand
// @license MIT
// ==/UserScript==
(function() {
'use strict';
function isPlaylist() {
return window.location.href.includes("list=");
}
function isChannel() {
return window.location.href.includes("/channel/") || window.location.href.includes("/user/");
}
const ytDlpCommand = (mode, quality = '') => {
const url = window.location.href;
let command = "yt-dlp ";
let isPL = isPlaylist();
let isCH = isChannel();
if (isPL || isCH) {
command += isPL ? "--yes-playlist " : "";
command += isCH ? "--download-archive channel_archive.txt " : ""; // Using an archive file to avoid re-downloads
}
switch (mode) {
case 'audio':
command += `--extract-audio --audio-format m4a --audio-quality 0 -o "%(playlist)s/%(playlist_index)s - %(uploader)s - %(title)s (via yt-dlp).%(ext)s" -f "bestaudio[ext=m4a]/bestaudio/bestvideo+bestaudio" "${url}"`;
break;
case 'video':
let videoQuality = 'bestvideo+bestaudio';
if (quality) {
videoQuality = `bestvideo[height<=${quality}]+bestaudio/best`;
}
command += `-f "${videoQuality}" --merge-output-format mkv -o "%(playlist)s/%(playlist_index)s - %(uploader)s - %(title)s %(height)sp (via yt-dlp).%(ext)s" "${url}"`;
break;
case 'comments':
case 'chat':
let fileType = (mode === 'comments') ? "comments and description" : "live chat";
command += `--write-${mode} -o "%(playlist)s/%(playlist_index)s - %(uploader)s - %(title)s ${fileType} (via yt-dlp).%(ext)s" "${url}"`;
break;
}
// Remove playlist-specific parts if not a playlist page
if (!isPL) {
command = command.replace(/%(playlist)s\/%(playlist_index)s - /g, "");
command = command.replace("%(playlist)s/%(playlist_index)s - ", "");
}
// Remove channel-specific parts if not a channel page
if (!isCH) {
command = command.replace(/--download-archive channel_archive\.txt /g, "");
}
GM_setClipboard(command);
alert("Command copied to clipboard:\n" + command);
};
// Registering menu commands
GM_registerMenuCommand("Download Audio (m4a)", () => ytDlpCommand('audio'), 'a');
GM_registerMenuCommand("Download Comments", () => ytDlpCommand('comments'), 'c');
GM_registerMenuCommand("Download Chat", () => ytDlpCommand('chat'), 'ch');
GM_registerMenuCommand("Download Video (Best)", () => ytDlpCommand('video'), 'v');
GM_registerMenuCommand("Download Video (4K)", () => ytDlpCommand('video', '2160'), '4');
GM_registerMenuCommand("Download Video (1080p)", () => ytDlpCommand('video', '1080'), '1');
GM_registerMenuCommand("Download Video (720p)", () => ytDlpCommand('video', '720'), '7');
GM_registerMenuCommand("Download Video (480p)", () => ytDlpCommand('video', '480'), '4');
})();