[PYTHON] Introducing youtube-dl, a video download tool that runs on the command line, and its zsh completion function.

Introducing a tool youtube-dl that works on the command line and downloads videos from video sites such as Youtube. Also, since there are so many options that I can't remember, I wrote a complementary function for zsh, so I will introduce it.


How to use

From the terminal

youtube-dl "URL"

If you execute, you can download it if it is a compatible site.


How to install

With Python's package management module pip

sudo pip install youtube-dl

You can install it with. However, note that the Python module name is underscored as youtube_dl in the specification.

It seems easy to install using Homebrew on Mac.

brew install youtube-dl

If you want to build from source, please see the original home.

Corresponding site

Supported sites are

youtube-dl --extractor-description

You can see it by running it, but it seems that it currently supports 621 sites. Of course, youtube, Nico Nico Douga, twitch and other famous places are covered. Below are the ones with links to the sites for each. … There are many suspicious sites, so please be careful when you visit.

-List of supported sites

Specifying options

Also, as you can see from the help, you can run it with various options. The simplest way to use it is simply without options as mentioned at the beginning.

youtube-dl "URL"

It is a method to paste the url directly as. On Youtube, you can also specify the ID under watch = in the URL. In addition, you can download all the videos registered in the playlist just by specifying the playlist.

Specifying the download format

You can also specify the format of the video and download it, you can display the supported download format with -F, and specify the format from these with -f and execute it I can.

Specify the save file name in the template

Then, you can specify the file name to save with -o, but by writing a template here, the title of the video, the title of the playlist, the number in the playlist, which site you downloaded from, the uploader Etc. can be included in the file name of the video. For more information (ry

Download multiple specified videos

Also, it may be useful to remember that you can put all the files you want to download in one text file and later

youtube-dl -a dl_list

If so, it will download all the videos in this file.

Add the ignore-errors option for time-consuming downloads

When downloading a lot of videos, such as when downloading from a playlist, adding the -i option is convenient because it ignores errors that occur in the middle and continues processing the next video.

Also, video files that have stopped processing for some reason, such as a network malfunction, are saved with the extension .part, so use the youtube-dl command again to save with the same file name. When executed, the download will resume from the middle. However, please note that if you interrupt the process with Ctrl-c etc., you may have to start over again.

Streaming playback without downloading

If you want to stream it instead of downloading it, use the -o option to output it to standard output and pipe it to mplayer.

youtube-dl URL -o - | mplayer -

If you use a media player such as mplayer or vlc that can play the video from the URL, you can play it as it is. In mpv, youtube-dl hook is prepared, so you can play it just by pasting the URL of the video as it is. (The above method may not be able to play on the contrary)

zsh completion function

Automatically generated

Among the scripts prepared in youtube-dl, there is a script that automatically generates the zsh completion function.

Download these two and in the same directory, look at line 12 of zsh-completion.py

ZSH_COMPLETION_TEMPLATE = "zsh-completion.in"

Change to

python zsh-completion.py

Will create a file called youtube-dl.zsh in the same directory. If you put this in the directory where you put the zsh completion function, when youtube-dl is executed, all long option candidates will be displayed, and for some options, the file name and directory It will also be complemented.

A little tweak

However, I feel like I have to remember what the options I often use as it is, so here are some of my own modifications. Please download and use it appropriately. If you would like to add or write something better, I would appreciate it if you could let me know in some way.

#-------- youtube-dl completion
# put this script in your .zshrc
# generated by:
# https://github.com/rg3/youtube-dl/blob/master/devscripts/zsh-completion.in
# https://github.com/rg3/youtube-dl/blob/master/devscripts/zsh-completion.py
# and modified manually by Shotaro Fujimoto (https://github.com/ssh0)

__youtube_dl() {
  local curcontext="$curcontext" fileopts diropts cur prev
  typeset -A opt_args
  local ddir="/media/shotaro/STOCK/Videos"
  case $cur in
      _arguments '*: :(::ytfavorites ::ytrecommended ::ytsubscriptions ::ytwatchlater ::ythistory)'
      if [[ ${prev} =~ ${fileopts} ]]; then
      elif [[ ${prev} =~ ${diropts} ]]; then
        _path_files -/
      elif [[ ${prev} == "--recode-video" ]]; then
        _arguments '*: :(mp4 flv ogg webm mkv)'
      elif [[ ${prev} == "--audio-format" ]]; then
        _arguments '*: :(best aac vorbis mp3 m4a opus wav)'
      elif [[ ${prev} == "--convert-subtitle" ]]; then
        _arguments '*: :(srt ass vtt)'
      elif [[ ${prev} =~ "-o|--output" ]]; then
        _arguments "*: :(\
          '${ddir}/%(title)s.%(ext)s' \
          '${ddir}/%(autonumber)s - %(title)s.%(ext)s' \
          '${ddir}/%(extractor)s/%(title)s.%(ext)s' \
          '-' \
      elif [[ ${prev} == "--proxy" ]]; then
        _arguments "*: :( `echo ${http_proxy}` )"
        _arguments -S\
          '-h[Print help]: :' \
          '-U[Update the program (run with root)]: :' \
          '-i[Continue on download errors]' \
          '(-6)-4[Make all connections via IPv4]' \
          '(-4)-6[Make all connections via IPv6]' \
          '-r[Maximum download rate in bytes per second (e.g. 50K or 4.2M)]' \
          '-R[Number of retries (default is 10), or "infinite"]' \
          '-a[File containg URLs to download ('-' for stdin)]' \
          '-o[Output filename template. (See manpage)]' \
          '-q[Activate quiet mode]' \
          '-s[Do not download the video and do not write anything to disk]' \
          '-g[Simulate, quiet but print URL]' \
          '-e[Simulate, quiet but print title]' \
          '-j[Simulate, quiet but print JSON information]' \
          '-J[Simulate, quiet but print JSON information for each command-line argument]' \
          '-f[Video format code, see the "FORMAT SELECTION" for all the info]' \
          '-F[List all available formats]' \
          '-u[Login with this account ID]' \
          '-p[Account pass word. If this option is left out, youtube-dl will ask interactively]' \
          '-2[Two-factor auth code]' \
          '-n[Use .netrc authentication data]' \
          '-x[Convert video files to audio-only files]' \
          '-k[Keep the video file on disk after the post-processing; the video is erased by default]' \
          '*: :(--help --version --update --ignore-errors --abort-on-error --dump-user-agent --list-extractors --extractor-descriptions --default-search --ignore-config --flat-playlist --no-color --proxy --socket-timeout --source-address --force-ipv3 --force-ipv6 --cn-verification-proxy --playlist-start --playlist-end --playlist-items --match-title --reject-title --max-downloads --min-filesize --max-filesize --date --datebefore --dateafter --min-views --max-views --match-filter --no-playlist --yes-playlist --age-limit --download-archive --include-ads --rate-limit --retries --buffer-size --no-resize-buffer --test --playlist-reverse --xattr-set-filesize --hls-prefer-native --external-downloader --external-downloader-args --batch-file --id --output --autonumber-size --restrict-filenames --no-overwrites --continue --no-continue --no-part --no-mtime --write-description --write-info-json --write-annotations --load-info --cookies --cache-dir --no-cache-dir --rm-cache-dir --write-thumbnail --write-all-thumbnails --list-thumbnails --quiet --no-warnings --simulate --skip-download --get-url --get-title --get-id --get-thumbnail --get-description --get-duration --get-filename --get-format --dump-json --dump-single-json --print-json --newline --no-progress --console-title --verbose --dump-pages --write-pages --youtube-print-sig-code --print-traffic --call-home --no-call-home --no-check-certificate --prefer-insecure --user-agent --referer --add-header --bidi-workaround --sleep-interval --format --all-formats --prefer-free-formats --list-formats --youtube-include-dash-manifest --youtube-skip-dash-manifest --merge-output-format --write-sub --write-auto-sub --all-subs --list-subs --sub-format --sub-lang --username --password --twofactor --netrc --video-password --extract-audio --audio-format --audio-quality --recode-video --keep-video --no-post-overwrites --embed-subs --embed-thumbnail --add-metadata --metadata-from-title --xattrs --fixup --prefer-avconv --prefer-ffmpeg --ffmpeg-location --exec --convert-subtitles)'

compdef __youtube_dl youtube-dl

Put this in .zshrc

local ddir=""

If you change the part of to the location of the directory where you want to download the video, you can complete it with zsh. If you read the zsh settings again, type youtube-dl and press the TAB key to see the explanation for the one-letter option, and add the information you want to add for some options. It has become like. (For example, output template, proxy address, etc.)


youtube-dl Super convenient. There are similar browser add-ons, but there were some problems, such as the fact that they had to be flash, and that the videos downloaded in economy mode became economy when using Nico Nico video. In that respect, this is convenient because you only need to know the URL of the site, and you do not have to bother to launch a browser. However, if you access it all at once, it will be a nuisance to the site, so behave sensible around there. Also, make sure that the site is approved for download by the right holder.

How to write the completion function of zsh seems to be difficult because there are many meta characters etc., but if you remember this, the work in the terminal will be much faster, so the one you actively use is complemented I want to write a function.

Recommended Posts

Introducing youtube-dl, a video download tool that runs on the command line, and its zsh completion function.
The story of making a tool that runs on Mac and Windows at the game development site
[auto-ohin] Introducing auto-ohin, a command line tool that can automatically stamp all at once [electronic stamp]
[Django] Hit a command you made from within the process that runs on manage.py.
A note that runs an external program in Python and parses the resulting line
[Python] I tried to make a simple program that works on the command line using argparse.
[EC2] How to install and download chromedriver from the command line
Python standard module that can be used on the command line
Get, test, and submit test cases on the command line in the AtCoder contest