How to use youtube-dl from a python program?

PythonYoutube Dl

Python Problem Overview


I would like to access the result of the following shell command,

youtube-dl -g "www.youtube.com/..."

to print its output direct url to a file, from within a python program. This is what I have tried:

import youtube-dl
fromurl="www.youtube.com/..."
geturl=youtube-dl.magiclyextracturlfromurl(fromurl)

Is that possible? I tried to understand the mechanism in the source but got lost: youtube_dl/__init__.py, youtube_dl/youtube_DL.py, info_extractors ...

Python Solutions


Solution 1 - Python

It's not difficult and actually documented:

import youtube_dl

ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'})

with ydl:
    result = ydl.extract_info(
        'http://www.youtube.com/watch?v=BaW_jenozKc',
        download=False # We just want to extract the info
    )

if 'entries' in result:
    # Can be a playlist or a list of videos
    video = result['entries'][0]
else:
    # Just a video
    video = result

print(video)
video_url = video['url']
print(video_url)

Solution 2 - Python

For simple code, may be i think

import os
os.system('youtube-dl [OPTIONS] URL [URL...]')

Above is just running command line inside python.

Other is mentioned in the documentation Using youtube-dl on python Here is the way

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'])

Solution 3 - Python

Here is a way.

We set-up options' string, in a list, just as we set-up command line arguments. In this case opts=['-g', 'videoID']. Then, invoke youtube_dl.main(opts). In this way, we write our custom .py module, import youtube_dl and then invoke the main() function.

Solution 4 - Python

from __future__ import unicode_literals 
import youtube_dl

ydl_opts = {} 
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['Your youtube url'])

You can use 'format', 'continue', 'outtmpl' in ydl_opts As example;

ydl_opts= {
           'format: '22',
           'continue': True;
           'outtmpl': '%(uploader)s - %(title)s.%(ext)s'
           'progress_hooks': [my_hook],
          }

def my_hook(d):
    if d['status'] == 'downloading':
        print('Downloading video!')
    if d['status'] == 'finished':
        print('Downloaded!')

When you need to stop playlist downloading, Just add this code into ydl_opts.

'noplaylist': True;

Solution 5 - Python

Usage: python3 AudioFromYtVideo.py link outputName

import os
from sys import argv

try:
    if argv[1] and argv[2]:
        pass
except:
    print("Input: python3 [programName] [url] [outputName]")    

os.system('youtube-dl -x --audio-format mp3 -o '+argv[2]+' '+argv[1])

Solution 6 - Python

If youtube-dl is a terminal program, you can use the subprocess module to access the data you want.

Check out this link for more details: https://stackoverflow.com/questions/89228/calling-an-external-command-in-python

Solution 7 - Python

I would like this

from subprocess import call

command = "youtube-dl https://www.youtube.com/watch?v=NG3WygJmiVs -c"
call(command.split(), shell=False)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulienFrView Question on Stackoverflow
Solution 1 - PythonjaimeMFView Answer on Stackoverflow
Solution 2 - Pythonuser13415013View Answer on Stackoverflow
Solution 3 - PythoncogitoergosumView Answer on Stackoverflow
Solution 4 - PythonDARKSOULView Answer on Stackoverflow
Solution 5 - PythonIGNACIO DEL CORROView Answer on Stackoverflow
Solution 6 - Pythonuser2286078View Answer on Stackoverflow
Solution 7 - PythonKrishna DeepakView Answer on Stackoverflow