How to directly download YouTube videos
www.hackoooo.blogspot.in
Youtube-dl is a fantastic little command line utility that lets you download videos from YouTube and a couple of other video hosting sites.
Youtube-dl is a fantastic little command line utility that lets you download videos from YouTube and a couple of other video hosting sites. Although the command line interface is off-putting at first, this tiny little app packs in quite a punch and has a host of excellent options available at a few strokes of the keyboard. We’re providing a primer to get you started with its basics along with a couple of interesting recipes.
Installation
For *nix systems, the installation of the command-line utility is quite straightforward. Most of the systems come equipped with Python. Youtube-dl requires either 2.6, 2.7 or 3.3+, you can pick up the version of your choice from Python’s official download page: http://www.python.org/getit/. Enter the following command in your terminal to get the executable.
sudo wget http://youtube-dl.org/
downloads/2013.02.02/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+x /usr/local/bin/youtube-dl
Installing the tool on a Windows PC is even simpler, the executable itself contains a Python runtime so you can run it directly. You can download the Windows exe from youtube-dl’s download page: http://youtube-dl.org/downloads.
Youtube-dl in action
You would want to add the folder containing the youtube-dl executable to the PATH environment variable so that you can access it from anywhere inside your system.
The code is updated regularly, so once you’ve downloaded it you can update the application anytime by using the command
youtube-dl -U
Usage
The basic usage of the program is invoking it from the command line along with options and video URL or identifier,
for example:
youtube-dl http://www.youtube.com/watch?v=dQw4w9WgXcQ
By default it saves the downloaded videos to the same directory from which the program was called with the video id as the saved filename. You can change this behaviour by adding the -t option, which saves the video with title as the filename. You can also use to the -o option to accomplish this which also allows you to change the path of the saved files. The -o option also allows you to define a template for the output file names, it comes in handy when storing, say, a playlist. The basic usage when downloading single files doesn’t entail the use of template arguments but it may contain the special sequences that will be replaced for each downloaded video. These sequences are of the form %(NAME)s, Note that the ‘s’ is compulsory.
You can replace NAME in the template sequence above with any of the ones mentioned below in any order and combination of your choice.
• id: The sequence will be replaced by video identifier.
• url: The sequence will be replaced by the video URL.
• uploader: The sequence will be replaced by the nickname of the person who uploaded the video.
• upload_date: The sequence will be replaced by the upload date in YYYYMMDD format.
• title: The sequence will be replaced by the actual title of the video.
• ext: The sequence will be replaced by the appropriate extension (like flv or mp4).
• epoch: The sequence will be replaced by the Unix epoch when creating the file.
• autonumber: The sequence will be replaced by a five-digit number that will be increased with each download, starting at zero.
In case you want to avoid using special characters or spaces in your saved file names you can add the --restrict-filenames option.
Limiting the file download speed and selecting video quality
Video quality and subtitles
As a default, youtube-dl downloads the highest available quality video that is reported by the website, which will be either 1080p or 720p. You can use the -F option to list all the available quality and formats for a given video, viz. writing the following command youtube-dl -F http://www.youtube.com/watch?v=0tHqov_UHYA prints the following:
Available formats:
22 : mp4 [720x1280]
45 : webm [720x1280]
35 : flv [480x854]
44 : webm [480x854]
34 : flv [360x640]
18 : mp4 [360x640]
43 : webm [360x640]
5 : flv [240x400]
17 : mp4 [144x176]
You will then have to run the command again specifying the code format that you want to download. Continuing the above mentioned example, if one wants to download the 720p version of the video the new command would be:
youtube-dl -f22 http://www.youtube.com/watch?v=0tHqov_UHYA
Subtitles are currently available for YouTube links only, to download a video’s closed caption to an .srt file, just add --write-srt option to the command to save the subtitle along with the video. For YouTube you can also mention the language in which you want to download the subtitles just use the --srt-lang option followed by IETF language tags like ‘en’ or ‘fr’ to download the subtitles in that particular language.
Batching and playlists
If you want to make yourself a YouTube mixtape or download a set of videos from different websites, you can batch download them using youtube-dl. All you need to do is add all the URLs to a file, each one on a new line and save the file with a .txt extension. You can then ask youtube-dl to read the URL from a particular file using the --batch-file option, like this:
youtube-dl --batch-file /path/to/batch/file.txt
In case you want to enter the URLs one-by-one directly into the command line itself you can write ‘-’ instead of the path to the batch file to instruct youtube-dl to read from stdin.
Youtube-dl has this particularly nifty feature which allows you to download entire playlists from YouTube. Infact it supports downloading playlists, channels, all videos for a particular user and even search results. You can do this by just providing the relevant link to youtube-dl, it’ll handle everything else for you.
Auto resume and rate limiting
By default, youtube-dl resumes file download from where it was interrupted. You can change this behaviour by adding the --no-continue option to restart downloading a file from beginning when interrupted. You may also want to add --no-overwrites option to ask youtube-dl not to overwrite files, but create no ones.
If you’re sharing your bandwidth with other terminals, uou can also control the download speed for youtube-dl thereby limiting the bandwidth it consumes, by passing the --rate-limit option along with the speed you want to limit it to. This is particularly helpful on slower connections. Eg.
youtube-dl --rate-limit 60k http://www.youtube.com/watch?v=vQVeaIHWWck
Youtube-dl works even if you’re behind a proxy. You can instruct it to download the videos through the proxy by setting the environment variable http_proxy to the proxy url, it should look something like thishttp://proxy_machine_ip_or_name:port.
Extract audio
Youtube-dl allows you to strip the audio from the downloaded file and store it as an audio file in numerous formats, making it very easy to download entire playlists as MP3 files. To do so just pass -x option to convert the video files to audio-only files. Mindwell this requires ffmpeg or avconv and ffprobe or avprobe, these are generally found on nearly all modern Linux OSes. If you don’t have it installed on your system, you can grab the latest version from your distro’s package manager. Windows users, don’t fret, as you can download the latest ffmpeg build for Windows from the following URL: http://ffmpeg.zeranoe.com/builds/.
You can control the audio format the downloaded file is saved in by using the --audio-format option. It will default to ‘best’ which will download the best quality available for download. You have an option to save in either of AAC, Vorbis, MP3, M4A, OPUS and WAV formats. You can control the audio quality by passing the --audio-quality option where you
can control the ffmpeg/avconv audio quality spec. by inserting a value between 0(better) to 9(worse) for variable bitrate (VBR) or you can go ahead and specify a bitrate viz. 128k, which is the default.
One thing to look out here is that it deletes the video files after converting them to audio. You can instruct youtube-dl not to do so by adding the -k option. Along with the -k option you can also instruct it to transcode the video into specific format by passing the --recode-video option followed by the format. The video formats currently supported are MP4, FLV, OGG and WebM.
youtube-dl -x --audio-format best --audio-qualtiy 9 -k --recode-video webmhttp://www.youtube.com/watch?v=SqDPZOtKtMc&playnext=1&list=PL8E392AEF40B0D93B&feature=results_main
Auto-download channel updates
The first thing we require here is a shell script or a bat file which will contain the command which will run youtube-dl. Open up your favourite text editor and enter the following command into it:
youtube-dl -t --playlist-start 1 --playlist-end 1 -w --no-continue <<URL of your choice>>
This command will download only the first file in the playlist or channel. We’ve also added the -w command which doesn’t allow overwrites. This makes sure that you only get the most recent video.
Channel update script in action
The only downside here is that you need to make sure that you include the video id in the output filename to make sure that you don’t overwrite files.
Now save the file as .sh in Linux or .bat in Windows. You can now go about automating the downloading in two ways:
1. Cron or Schtasks :
In Linux, fire up the crontab using the crontab -e command. This is your cron job editor. Now a cron job is laid out like this:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
In our case, we want to run it at 12.01 PM every day, the cron entry would look like this:
01 12 * * * /path/to/our/script
It would be prudent to split downloads from different channels//playlists in different files, otherwise the commands would be executed sequentially if they’re added in the same file on different line. You can tweak the script run time to the one that suits you. You can read up more on cron here:https://help.ubuntu.com/community/CronHowto
Windows uses schtasks to schedule tasks and you can follow the comprehensive documentation here:http://technet.microsoft.com/en-us/library/cc725744.aspx to run the .bat file at the time that you desire.
Based on WxPython Widget Toolkit, you don’t need to install it
2. System startup :
Instead of fetching videos at a particular time of the day, you can look for new videos only when you boot your system. On Windows you can use schtasks itself, the resulting command would look something like this:
schtasks /create /tn “youtube-dl” /tr pathtobatfile /sc onstart
It’s a bit convoluted to implement this in Linux, you would want to start the script once you log into the system. On Gnome based systems you can source the path to the script to ~/.gnomerc file, which makes sure that the command is executed each time you login. On KDE based systems you can source the path from ~/.config/autostart file. You could also do this in your .bashrc but then it’ll run every time you open your terminal.
GUI
If you’re not comfortable using youtube-dl in its native command line format, you can use the GUI developed by Freddy Wijaya. The code is quite old and has not been updated since August 2012. You’ll miss out on some of the better features that the later versions of youtube-dl have to offer. Head over to it’s download page at Google Code and grab the latest version. The GUI is currently available only for Windows and Linux, sorry Apple users.
0 comments:
Post a Comment