Generate YouTube Download commands for yt-dlp terminal

Enhance downloading capabilities on YouTube with playlist and channel support.

目前為 2024-07-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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');
})();