Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download YouTube video using Python to a certain directory

I have tried the following code to download a video in YouTube and it is working, but I want to save the video at a particular location. Now it is saving the video in C:/Users/Download. If I want to save the video in the desktop, what changes do I need in the code?

from __future__ import unicode_literals import youtube_dl import urllib import shutil ydl_opts = {} with youtube_dl.YoutubeDL(ydl_opts) as ydl:     ydl.download(['https://www.youtube.com/watch?v=n06H7OcPd-g']) 
like image 641
Sharmili Nag Avatar asked Nov 21 '16 05:11

Sharmili Nag


People also ask

How do I download a YouTube video from a certain point?

Click the Video Trim icon, which is the icon of a photo with a pencil at the top-center. Drag the left guide to the place you want your clip to begin, and the right guide to where you want your clip to end. Click Save as at the top. Choose a saving location (and name, if you want) and click Save.

How do I use Pytube in Python?

Using pytube in a Python script To download a video using the library in a script, you'll need to import the YouTube class from the library and pass an argument of the video URL. From there, you can access the streams and download them.


Video Answer


1 Answers

I found out a really cool python module that allows you to download videos from youtube easily. TO install it:

pip install pytube 

Now, You can download your video like this -

from pytube import YouTube yt = YouTube("https://www.youtube.com/watch?v=n06H7OcPd-g") yt = yt.get('mp4', '720p') yt.download('/path/to/download/directory') 

Boom, Now you can easily scrape such videos using Python easily; Now, We Drink!

Update 1:

Thanks to @Chiramisu's comment, You can use the following one-liner to download the highest quality video:

YouTube('video_url').streams.first().download('save_path') 

For Windows, please specify path with double backslashes ex:

YouTube('video_url').streams.first().download('C:\\Users\\username\\save_path') 

Update 2:

If pytube doesn't seem to work for you, try using youtube-dl:

pip install --upgrade youtube-dl 

Now Download Videos:

from __future__ import unicode_literals import youtube_dl  ydl_opts = {} with youtube_dl.YoutubeDL(ydl_opts) as ydl:     ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc']) 

More info on ytdl in python here.

like image 159
Daksh Miglani Avatar answered Oct 08 '22 22:10

Daksh Miglani