aboutsummaryrefslogtreecommitdiff
path: root/src/nushell/scripts/youtube.nu
blob: 5423b9d65b69b0684665c48ec03e957dc54b6ab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env nu

#
# ~~~ nushell youtube downloader
#

#
# ~~~ help command

# A YouTube helper program
export def yt []: nothing -> nothing {
    help yt
}

#
# ~~~ download a video

# Download a YouTube video with x265
export def "yt video" [
    video: string,  # A YouTube video URL
    title: string   # The filename of the video
]: nothing -> nothing {

    # download video
    yt-dlp --config-location $"($env.HOME)/.config/yt-dlp/video.conf" -o $"($title)-original" $video

    # convert video
    ffmpeg -i $"($title)-original.mp4" -vcodec libx265 -crf 28 $"($title).mp4"

    # remove original
    rm $"($title)-original.mp4"
}

#
# ~~~ download thumbnail

# Download a YouTube video's thumbnail
export def "yt thumbnail" [
    video: string   # A YouTube video ID
]: nothing -> nothing {

    let URL = $"https://i.ytumg.com/vi/($video)/maxresdefault."

    # try .webp
    try {

        http get $"($URL).webp" | save $"($video).webp"

    } catch {

        # try .png
        try {
            
            http get $"($URL).png" | save $"($video).png"

        # try .jpg
        } catch {

            http get $"($URL).jpg" | save $"($video).jpg"

        }
    }
}