youtube-dl howto - install and quick start guide

Install

sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl

sudo chmod a+rx /usr/local/bin/youtube-dl

List formats

$ youtube-dl -F <url>

Download

$ youtube-dl -f <format> <url>

Yandex.Music

$ youtube-dl --cookies ./cookies.txt -f <format> <url>

To get cookies.txt file you need an extension for Google Chrome, called "Get Cookies.txt"

Pytube(fix)

pip install pytubefix

then:

import os

from pytubefix import YouTube
from pytubefix.cli import on_progress

url = "https://www.youtube.com/watch?v=xxxxxxxxxxx"

def resolutions(video: YouTube):
    res = []
    streams = self.video.streams.\
                    filter(type='video').\
                    order_by('resolution').\
                    desc()
    for stream in streams:
        if stream.resolution not in res:
            res.append(stream.resolution)
    return res 

def combine(audio: str, video: str, output: str) -> None:

    if os.path.exists(output):
        os.remove(output)

    code = os.system(
        f'ffmpeg -i "{video}" -i "{audio}" -c copy "{output}"')

    if code != 0:
        raise SystemError(code)


def download(url: str, res = None):

    yt = YouTube(
        proxies={},
        url=url,
        on_progress_callback=on_progress,
    )

    if res == None:
        video_stream = yt.streams.\
         filter(type='video').\
         order_by('resolution').\
         desc().first()
    else:
        video_stream = yt.streams.\
          filter(resolution=res, progressive=False).desc().first()

    audio_stream = yt.streams.\
        filter(mime_type='audio/mp4').\
        order_by('filesize').\
        desc().first()

    print('Information:')
    print("\tTitle:", yt.title)
    print("\tAuthor:", yt.author)
    print("\tDate:", yt.publish_date)
    print("\tResolution:", video_stream.resolution)
    print("\tViews:", yt.views)
    print("\tLength:", round(yt.length/60), "minutes")
    print("\tFilename of the video:", video_stream.default_filename)
    print("\tFilesize of the video:", round(
        video_stream.filesize / 1000000), "MB")

    print('Download video...')
    video_stream.download()
    print('\nDownload audio...')
    audio_stream.download()

    combine(audio_stream.default_filename, video_stream.default_filename,
            f'{yt.title}.mp4')

res = '720p' #1080p 360p etc
download(url, res)

See Also

http://ytdl-org.github.io/youtube-dl/download.html

Pytubefix part1

Pytubefix part2


Создано: 15/02/2023 12:40, Изменено: 04/01/2025 22:30, Просмотров: 68
Назад