Jungwoong Kim

YouTube 다운로더 만들기 (macOS)

0. 소개

가끔씩은 유튜브나 다른 사이트에서 좋은 노래들나 영상을 찾아서 다운로드를 하고 싶을 때가 있다. 그럴 때마다 인터넷에 있는 “Youtube Downloader” 사이트들에 들어가서 다운을 받곤 했는데, 광고가 너무 많아서 정신없고 바이러스를 끼워팔기 하는게 아닐까 하는 걱정도 했다.

그때 나를 구원한 툴이 youtube-dl 이었다. youtube-dl은 Python 기반의 프로그램으로, YouTube를 포함해 여러 웹사이트의 영상 파일을 영상 또는 음성 파일로 다운로드하는 기능을 가지고 있다.

광고 없고 간단한 Youtube 다운로더를 사용할 수 있어서 좋았지만, 어떤 Cli 프로그램이든 마찬가지로 영상 다운로드 할 때마다 매번 스크립트를 작성해야 한다는 번거로움이 있었다. 그래서, shell script를 통해 필요한 정보만 입력하면 알맞은 youtube-dl 스크립트를 생성해주는 툴을 만들어 보았다.

1. Code

function youtube_magic() {
    read "filetype?Enter the type of file: "
    read "Multi?Multiple URLS?(Y/N): "

    if [ "$Multi" = "Y" ]
    then
        echo "Write one URL per line. Finish with ctrl+D."
        if [ "$filetype" = "mp3" ]
        then
            youtube-dl -x --audio-format mp3 --audio-quality 0 -a - -o '~/Music/youtube-dl/%(title)s.%(ext)s'
        elif [ "$filetype" = "mp4" ]
        then
            youtube-dl --format mp4 -f best -a - -o '~/Movies/youtube-dl/%(title)s.%(ext)s'
        fi
    elif [ "$Multi" = "N" ]
    then
        read "URL?Enter the URL of video: "
        if [ "$filetype" = "mp3" ]
        then
            youtube-dl -x --audio-format mp3 --audio-quality 0 $URL -o '~/Music/youtube-dl/%(title)s.%(ext)s'
        elif [ "$filetype" = "mp4" ]
        then
            youtube-dl --format mp4 -f best $URL -o '~/Movies/youtube-dl/%(title)s.%(ext)s'
        fi
    else
        echo "Something Broke!"
    fi
}

아래와 같이 원하는 영상을 mp3 또는 mp4파일로 저장할 수 있다.

$ youtube_magic
Enter the type of file: mp4
Multiple URLS?(Y/N): N
Enter the URL of video: https://www.youtube.com/watch?v=d_mLFHLSULw
[youtube] d_mLFHLSULw: Downloading webpage
[youtube] d_mLFHLSULw: Downloading js player 5799986b
[download] Destination: ~/Movies/youtube-dl/Luciano Pavarotti - O sole mio.mp4
[download] 100% of 10.85MiB in 00:01

2. 시연 영상