I was interested in viewing this video of a news conference (USENIX 2016) on my trip home on Metro North Train, NYC => CT. The trip is about an hour an 10 minutes from Manhattan’s Grand Central Terminal to Milford CT, express train that is. My concern was that I would have choppy internet service on the way since I recently updated my laptop and the built in Verizon Mobile card was not activated yet. I would need to use my ATT iPhone as a hotspot, which proved to be very shakey at times. A colleague of mine recommended a website for making youtube videos available for offline viewing. The name of this site was:
Right off the rip I was concerned that this site was infested with malware and any other bullshit associated with a free video ripping service. I used the site and was able to create a download of the video I was interested in, however who knows how sick my Windows based machine just got. I could of contracted anything from this site.
I thought about this and said, there has to be a better way, or a python lib for this, and low and behold a search came up with PYTUBE:
https://github.com/nficano/pytube
This library had some interesting features and literally blew away the keepvid site in regards to flexibility. Here is some explaining of what this library can do. Please have a look at the examples below, I will do my best to narrate them.
Here I use PIP to install the PYTUBE lib, you can ignore the DEPRECATION: warning for my outdated python that blares at you for being such an idiot.
1 2 3 4 5 6 7 |
[root@jasonralph ~]# pip install pytube DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6 Collecting pytube Using cached pytube-6.1.8.tar.gz Installing collected packages: pytube Running setup.py install for pytube ... done Successfully installed pytube-6.1.8 |
Next up you can see that I am setting a variable yt(this is the video you want to download). Using python’s Pretty Print Lib you can run the pprint(yt.get_videos() method to see what formats are available for download.
Please have a look at the comments in the code for a bit more details in regards to what is going on, in this example I am using the filename Pulp_Fiction.mp4 for my filename I want to be when downloaded.
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 |
[jralph@jasonralph ~]$ cat py_video_downloader.py from pytube import YouTube from pprint import pprint yt = YouTube("http://www.youtube.com/watch?v=Ik-RsDGPI5Y") pprint(yt.get_videos()) print(yt.filename) yt.set_filename('Pulp_Fiction.mp4') # Notice that the list is ordered by lowest resolution to highest. If you # wanted the highest resolution available for a specific file type, you # can simply do: print(yt.filter('mp4')[-1]) # <Video: H.264 (.mp4) - 720p> # You can also get all videos for a given resolution pprint(yt.filter(resolution='720p')) video = yt.get('mp4', '720p') # NOTE: get() can only be used if and only if one object matches your criteria. # for example: pprint(yt.videos) video.download('/home/jralph/') |
Ok so here is what it looks like when you execute the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[23:32:59] jralph@jasonralph:~ $ python py_video_downloader.py [<Video: MPEG-4 Visual (.3gp) - 144p - Simple>, <Video: MPEG-4 Visual (.3gp) - 240p - Simple>, <Video: Sorenson H.263 (.flv) - 240p - N/A>, <Video: H.264 (.mp4) - 360p - Baseline>, <Video: H.264 (.mp4) - 720p - High>, <Video: VP8 (.webm) - 360p - N/A>] Pulp Fiction - Dancing Scene <Video: H.264 (.mp4) - 720p - High> [<Video: H.264 (.mp4) - 720p - High>] /usr/lib/python2.6/site-packages/pytube/api.py:141: DeprecationWarning: videos property deprecated. Use `get_videos()` instead. "instead.", DeprecationWarning) [<Video: MPEG-4 Visual (.3gp) - 144p - Simple>, <Video: MPEG-4 Visual (.3gp) - 240p - Simple>, <Video: Sorenson H.263 (.flv) - 240p - N/A>, <Video: H.264 (.mp4) - 360p - Baseline>, <Video: H.264 (.mp4) - 720p - High>, <Video: VP8 (.webm) - 360p - N/A>] |
As you can see we have a new filename with the video we asked for to watch without a streaming internet connection, here is a ls to show:
1 2 3 4 5 6 |
[23:33:02] jralph@jasonralph:~ $ ls -ltr total 32352 drwxr-xr-x 2 root root 4096 Mar 20 22:50 image_staging drwxr-xr-x 2 root root 4096 Mar 20 22:51 JR.ORG_SITE_BACKUPS -rwxrwxr-x 1 jralph jralph 691 Apr 3 00:34 py_video_downloader.py -rw-rw-r-- 1 jralph jralph 33113243 Apr 3 23:33 Pulp_Fiction.mp4.mp4 |
As always, I am sure there are better ways to do this and I am sure there is cleaner code. Most of this code was taken right from the authors site who is a badass, here is his link:
https://github.com/nficano/pytube
Hope you liked,
J$0N