Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use youtube-dl from a python program?

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 ...

like image 795
JulienFr Avatar asked Aug 05 '13 09:08

JulienFr


People also ask

How do I use youtube-dl in Python?

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!

Does youtube-dl need Python?

About youtube-dl youtube-dl is a command-line program to download videos from YouTube.com and a few more sites. It requires the Python interpreter (2.6, 2.7, or 3.2+), and it is not platform-specific.

How do you use youtube-dl?

The simplest way to use YouTube-dl is to give it the URL of a youtube video. Go to a video on YouTube that you want to download. Select the text of the URL in the address bar, and copy it to your clipboard by pressing Ctrl + C . If the URL has a "&" in it (a playlist, for example), only copy the URL up to the & symbol.

Is Youtubedl illegal?

Downloading videos from YouTube is in breach of YouTube's Terms of Service, and the company could sue you. YouTube has shown no desire to penalize users for downloading videos. Downloading copyrighted videos without permission is a criminal act.


1 Answers

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) 
like image 190
jaimeMF Avatar answered Sep 22 '22 13:09

jaimeMF