anaconda ref) https://anaconda.org/conda-forge/youtube-dl
conda install -c conda-forge youtube-dl
ref) https://github.com/ytdl-org/youtube-dl/blob/master/README.md
sudo -H pip install --upgrade youtube-dl
import
import youtube_dl as ydl
The video DL has a ydl.main () method and a ydl._real_main () method. As you can see from the source, the former is a wrapper for the latter, which additionally handles exceptions. The latter method is used here for the reasons described below. You can specify various arguments ( Refer to the original git ), but the option specifier and its Note that if you put a space between the actual condition, that space will also be included in the actual condition.
myURL = "https://www.nicovideo.jp/watch/so23335421" #URL of the video you want to save
myOUT = "-oout.mp4" #Specify output video file name
def main():
try:
ydl._real_main([myURL, myOUT])
except ydl.DownloadError:
main()
main()
When I download a Nico Nico video of 4 minutes or more, I get an error of ydl.DownloadError class (actually it is an error of urlib, and youtube-dl at the time of writing seems to be unable to avoid this error <a href=https //obel.hatenablog.jp/entry/20191102/1572693444> [Reference] ) Actually, if you repeat the method, you can continue DL, and when DL is completed, no error will occur. Therefore, while ydl._real_main () keeps throwing ydl.DownloadError, it is possible to avoid seeing the error by continuing to execute ydl._real_main () recursively.
Since the youtube_dl module makes heavy use of sys.exit, you may have to change the source if you want to work properly with jupyter notebook or in combination with other programs? <a href=https://hira98.hatenablog .com / entry / 2019/07/02/210000> If you call it with a try block, it may be possible .
Recommended Posts