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([') 2 11 Answers
I found out a really cool python module that allows you to download videos from youtube easily. TO install it:
pip install pytubeNow, You can download your video like this -
from pytube import YouTube
yt = YouTube("")
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-dlNow Download Videos:
from __future__ import unicode_literals
import youtube_dl
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([')More info on ytdl in python here.
13downloading videos from youtube in python 3.x for the reference you can check
from pytube import YouTube
import os
def downloadYouTube(videourl, path): yt = YouTube(videourl) yt = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first() if not os.path.exists(path): os.makedirs(path) yt.download(path)
downloadYouTube(' './videos/FindingNemo1') 1 You should put it inside ydl_opts:
ydl_opts = { 'outtmpl': os.path.join(download_path, '%(title)s-%(id)s.%(ext)s'),
}In your case, download_path should be 'C:/Users/Desktop'. Use %(title)s.%(ext)s instead of %(title)s-%(id)s.%(ext)s if you prefer a file name without video ID.
Or you can just os.chdir(path) to change the directory to where you want the download to be before you start your download.
from __future__ import unicode_literals
import youtube_dl
import os
ydl_opts = {}
os.chdir('C:/Users/Desktop')
with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([') 3 -> Uninstall pytube (if is present)
pip uninstall pytube-> Install:
pip install pytube3-> Modify extract.py in environment:
lib/pytube/extract.py-> Find and edit line:
parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)-> change it to:
parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)-> Use pytube:
from pytube import YouTube
import os
def downloadYoutube(vid_url, path): yt = YouTube(vid_url) yt = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first() if not os.path.exists(path): os.makedirs(path) yt.download(path)
url = input('Input url:\n')
path = input('Path to store file:\n')
downloadYoutube(url, path) Path = "The Path That You Want"
Location = '%s \%(extractor)s-%(id)s-%(title)s.%(ext)s'.replace("%s ", Path)
ytdl_format_options = {
'outtmpl': Location
}
with youtube_dl.YoutubeDL(ytdl_format_options) as ydl: ydl.download([')I personally don't know the library very well, but here is my knowledge the youtube_dl have ytdl_format_options it gives you the options to add some I don't know what it called but let say parameters like above outtmp1 give you the option to specify the location, id, title, or quiet to see the log or not and there is so much more. almost everything you can get it from this URL:
1youtube_dl has a giant list of options:
But I don't see any that control the output directory. So you can move the file afterward. For that, see: How to move a file in Python.
1It will save the file where your .py application is in. for example, if your .py program is in your desktop folder and you run your app from the desktop, the output will save on your desktop. the only thing you need is to save your .py file in Desktop and then open a command line and go in Desktop using cd command after it run your .py file using python YOURAPP.py but if you want to download it and then save it in another place, you need to download it like you do now (in your temporary place) then moves it via file libraries in python.
from __future__ import unicode_literals
import youtube_dl
ydl_opts = { 'outtmpl': 'yourPathToDirectory/%(title)s.%(ext)s',
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([')ydl_opts is the what we need to play with. We can declare multiple attributes/parameters inside this for customisation. For this example, I've added a single attribute outtmpl which is used to define custom directory. We can further customize this to declare filename, etc.
It looks like pytube has changed a little bit since the top answer for the question was originally posted. Here is an updated example for working with PyTube v11.0.2 (January 9th, 2022):
'''
HOW TO: Download YouTube Videos Programmatically with Python & pytube
$ pip install pytube
$ python3 download.py
'''
from pytube import YouTube
def on_progress(stream, chunk, bytes_remaining): print("Downloading...")
def on_complete(stream, file_path): print("Download Complete")
yt = YouTube( "", on_progress_callback=on_progress, on_complete_callback=on_complete, use_oauth=False, allow_oauth_cache=True )
yt.streams.filter(file_extension='mp4', res="720p").first().download() 1 I did some changes in the answer of @orlov_dumitru and it worked fine.
#!/usr/bin/env python
import os
import sys
from pytube import YouTube
def downloadYoutube(vid_url, path): yt = YouTube(vid_url) yt = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first() if not os.path.exists(path): os.makedirs(path) yt.download(path)
# video url
url = sys.argv[1]
# path to where you want to save the video
path = sys.argv[2]
downloadYoutube(url, path)
exit() I guess you are a bit confused, try this code, end to end
from __future__ import unicode_literals import youtube_dl import urllib import shutil ydl_opts = {} with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([') #Moving your source file to destination folder source_file = 'C:\Users\Sharmili Nag\Aahatein - Agnee (splitsvilla 4 theme song) Best audio quality-n06H7OcPd-g.mp4' destination_folder = 'C:\Users\Sharmili Nag\Desktop\Aahatein - Agnee (splitsvilla 4 theme song) Best audio quality-n06H7OcPd-g.mp4' shutil.move(source_file, destination_folder)In case this code worked out for you, kindly mark the answer as correct.