How to Download YouTube Videos on Android with youtube-dl

Youtube-dl is a Windows, Mac OS and GNU/Linux tool that makes downloading of Internet videos quite easily. It implements a powerful download engine and can download and manipulate audio and video files from several websites. As its name suggests, it originally started as a tool to download YouTube videos on Android, but it was later expanded by the community to support more than a hundred different websites. 

It is Open Source, small in size and enables downloading media with a single command.

Between others, “youtube-dl” commands support downloading from the following services:
  • Youtube
  • ADN – Anime Digital Network
  • Audiomack
  • BBC
  • Dailymotion
  • Discovery
  • Twitch
  • Vimeo
A list of all supported websites can be found here.

Thanks to the work of the Open Source community, it is possible to run youtube-dl on Android through Termux. Once installed, all of its operations will be completely supported on the Android device, including extracting audio from video files (eg. video clips, live concerts) and choosing which streams to download between the different audio and video formats available on the website.

How to Set it Up
  1. Install Termux
  2. Install the Termux:API Plugin. Termux needs this plugin to gain access to the Android device’s storage.
  3. Enable Storage access to Termux. Open the Termux App and enter the following command:
  4. termux-setup-storage
  5. Your device might throw a dialog requesting storage access permission for the Termux App.
  6. Install required packages
  7. Inside Termux, enter the following command:pkg install -y ffmpeg python
  8. This will install python and ffmpeg dependencies. Next, install youtube-dl with the following command:pip install youtube-dl
How to use youtube-dl
youtube-dl allows full media manipulation using one single command. We can initiate a simple download with the following command:youtube-dl media_url

Where media_url is the URL of the media file. For example, to download the trailer of the movie Pulp Fiction from YouTube, we would issue this command:youtube-dl

youtube-dl will automatically extract the web page information and download the media file. On current versions, youtube-dl automatically downloads video and audio streams of the best available quality when no additional options are passed to it.

Selection of Video and Audio Format
Youtube-dl provides various configuration options for the downloaded video format. First of all, passing the -F parameter will display all formats available for download:youtube-dl -F

This will provide an output like the following:

First column displays the format code, second column the media extension and the third the resolution (for video streams). Supposing we need to download the best available (the last one) video stream and the 160k opus audio stream, we need to give the following command:youtube-dl -f 22+251

where the -f parameter instructs youtube-dl to select the format codes 22 and 251, which represent the video and audio stream respectively.

Selection based on conditionals

Youtube-dl provides an interesting functionality for users that need greater control. Video and audio can be filtered by putting a condition in brackets, as in -f “[height=720]” (selects and downloads a video with 720 pixels height), or -f “[filesize>10M]” (selects and downloads media with file size greater than 10 MB). Fields that can be used in conditionals are the following:
  • filesize: The number of bytes, if known in advance
  • width: Width of the video, if known
  • height: Height of the video, if known
  • tbr: Average bit-rate of audio and video in KBit/s
  • abr: Average audio bit-rate in KBit/s
  • vbr: Average video bit-rate in KBit/s
  • asr: Audio sampling rate in Hertz
  • fps: Frame rate
So, the following command would select and download video files with frame rates higher than 25, height greater or equal to 720 pixels and audio sampling rate greater than 44200 Hz:youtube-dl -f "[fps>25][height>=720][asr>44200]" media_url

If no available audio/video streams cover the given criteria, youtube-dl will display an error message and exit. Filtering using conditionals might be useful for older Android devices, which can not easily decode video files with high resolutions.

Extracting and Converting Audio
To extract audio from media files, the -x command line parameter should be passed. It is also possible to select the extracted audio format, with the –audio-formatparameter. Lastly, we can configure the quality of audio with –audio-quality. Let’s take a look at the following command:youtube-dl -x --audio-format flac --audio-quality 0

The above command will extract the audio from the Pulp Fiction trailer, convert it to flac format and use the best audio conversion quality. Available options for –audio-format are “best”,”flac”,”aac”,”mp3″, “m4a”, “opus”, “vorbis”, or “wav”. Youtube-dl will use “best” option by default if the user does not set the –audio-format parameter. Values for –audio-quality can be between 0 and 9 for best and worst audio quality, respectively.

Youtube-dl has grown into a highly capable tool over the years. Its configuration options list is huge and can be heavily tweaked based on personal needs. There is, for example, support for downloading files from playlists. If you are interested in learning more about the command line parameters of youtube-dl, you can visit the official documentation site, or enter the following command in Termux:youtube-dl -h

RaphBlog.Com.NG. Powered by Blogger.