Use youtube_dl as a python module. appendix) Nico Nico Douga HTTP 403 error

Installation

anaconda ref) https://anaconda.org/conda-forge/youtube-dl

conda install -c conda-forge youtube-dl

pip (unverified)

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

Video DL

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.

Remarks

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

Use youtube_dl as a python module. appendix) Nico Nico Douga HTTP 403 error
Use blender as a python module
Use pymol as a python library
Error when installing a module with Python pip
Use the e-paper module as a to-do list
Use a custom error page in python / tornado
Create a Python module
[Python] Use JSON format data as a dictionary type object
[Python] Use a string sequence
Use HTTP cache in Python
Use Remotte as a user
Use fabric as is in python (fabric3)
A pretty sloppy Python error solution
Use embeddable Python as Vim's Python 3 interface
Launch a Python script as a service
If you want a singleton in python, think of the module as a singleton
I get a Python No module named'encodings' error with the aws command